Skip to content

Commit 5cf77f4

Browse files
authored
Added tasks 780, 781, 782.
1 parent c1af4ef commit 5cf77f4

File tree

9 files changed

+292
-0
lines changed

9 files changed

+292
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0701_0800.s0780_reaching_points;
2+
3+
// #Hard #Math
4+
5+
public class Solution {
6+
public boolean reachingPoints(int sx, int sy, int tx, int ty) {
7+
while (tx >= sx && ty >= sy) {
8+
if (tx > ty) {
9+
if (ty == sy) {
10+
// ty==sy
11+
return (tx - sx) % sy == 0;
12+
} else {
13+
// ty > sy
14+
tx %= ty;
15+
}
16+
} else if (sx == tx) {
17+
// ty >= tx
18+
return (ty - sy) % sx == 0;
19+
} else {
20+
// (tx > sx)
21+
ty %= tx;
22+
}
23+
}
24+
return false;
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
780\. Reaching Points
2+
3+
Hard
4+
5+
Given four integers `sx`, `sy`, `tx`, and `ty`, return `true` _if it is possible to convert the point_ `(sx, sy)` _to the point_ `(tx, ty)` _through some operations__, or_ `false` _otherwise_.
6+
7+
The allowed operation on some point `(x, y)` is to convert it to either `(x, x + y)` or `(x + y, y)`.
8+
9+
**Example 1:**
10+
11+
**Input:** sx = 1, sy = 1, tx = 3, ty = 5
12+
13+
**Output:** true
14+
15+
**Explanation:**
16+
17+
One series of moves that transforms the starting point to the target is:
18+
(1, 1) -> (1, 2)
19+
(1, 2) -> (3, 2)
20+
(3, 2) -> (3, 5)
21+
22+
**Example 2:**
23+
24+
**Input:** sx = 1, sy = 1, tx = 2, ty = 2
25+
26+
**Output:** false
27+
28+
**Example 3:**
29+
30+
**Input:** sx = 1, sy = 1, tx = 1, ty = 1
31+
32+
**Output:** true
33+
34+
**Constraints:**
35+
36+
* <code>1 <= sx, sy, tx, ty <= 10<sup>9</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0701_0800.s0781_rabbits_in_forest;
2+
3+
// #Medium #Array #Hash_Table #Math #Greedy
4+
5+
public class Solution {
6+
public int numRabbits(int[] answers) {
7+
int[] counts = new int[1001];
8+
for (int element : answers) {
9+
counts[element]++;
10+
}
11+
int answer = counts[0];
12+
for (int i = 1; i <= 1000; i++) {
13+
if (counts[i] > 0) {
14+
int rabbitsInPartialGroup = counts[i] % (i + 1);
15+
int rabbitsInCompleteGroups = counts[i] - rabbitsInPartialGroup;
16+
answer += rabbitsInCompleteGroups;
17+
if (rabbitsInPartialGroup > 0) {
18+
answer += (i + 1);
19+
}
20+
}
21+
}
22+
return answer;
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
781\. Rabbits in Forest
2+
3+
Medium
4+
5+
There is a forest with an unknown number of rabbits. We asked n rabbits **"How many rabbits have the same color as you?"** and collected the answers in an integer array `answers` where `answers[i]` is the answer of the <code>i<sup>th</sup></code> rabbit.
6+
7+
Given the array `answers`, return _the minimum number of rabbits that could be in the forest_.
8+
9+
**Example 1:**
10+
11+
**Input:** answers = [1,1,2]
12+
13+
**Output:** 5
14+
15+
**Explanation:**
16+
17+
The two rabbits that answered "1" could both be the same color, say red.
18+
The rabbit that answered "2" can't be red or the answers would be inconsistent.
19+
Say the rabbit that answered "2" was blue.
20+
Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
21+
The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
22+
23+
**Example 2:**
24+
25+
**Input:** answers = [10,10,10]
26+
27+
**Output:** 11
28+
29+
**Constraints:**
30+
31+
* `1 <= answers.length <= 1000`
32+
* `0 <= answers[i] < 1000`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0701_0800.s0782_transform_to_chessboard;
2+
3+
// #Hard #Array #Math #Matrix #Bit_Manipulation
4+
5+
public class Solution {
6+
public int movesToChessboard(int[][] board) {
7+
int n = board.length;
8+
int colToMove = 0;
9+
int rowToMove = 0;
10+
int rowOneCnt = 0;
11+
int colOneCnt = 0;
12+
for (int i = 0; i < n; i++) {
13+
for (int j = 0; j < n; j++) {
14+
if (((board[0][0] ^ board[i][0]) ^ (board[i][j] ^ board[0][j])) == 1) {
15+
return -1;
16+
}
17+
}
18+
}
19+
for (int i = 0; i < n; i++) {
20+
rowOneCnt += board[0][i];
21+
colOneCnt += board[i][0];
22+
if (board[i][0] == i % 2) {
23+
rowToMove++;
24+
}
25+
if (board[0][i] == i % 2) {
26+
colToMove++;
27+
}
28+
}
29+
if (rowOneCnt < n / 2 || rowOneCnt > (n + 1) / 2) {
30+
return -1;
31+
}
32+
if (colOneCnt < n / 2 || colOneCnt > (n + 1) / 2) {
33+
return -1;
34+
}
35+
if (n % 2 == 1) {
36+
// we cannot make it when ..ToMove is odd
37+
if (colToMove % 2 == 1) {
38+
colToMove = n - colToMove;
39+
}
40+
if (rowToMove % 2 == 1) {
41+
rowToMove = n - rowToMove;
42+
}
43+
} else {
44+
colToMove = Math.min(colToMove, n - colToMove);
45+
rowToMove = Math.min(rowToMove, n - rowToMove);
46+
}
47+
return (colToMove + rowToMove) / 2;
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
782\. Transform to Chessboard
2+
3+
Hard
4+
5+
You are given an `n x n` binary grid `board`. In each move, you can swap any two rows with each other, or any two columns with each other.
6+
7+
Return _the minimum number of moves to transform the board into a **chessboard board**_. If the task is impossible, return `-1`.
8+
9+
A **chessboard board** is a board where no `0`'s and no `1`'s are 4-directionally adjacent.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/29/chessboard1-grid.jpg)
14+
15+
**Input:** board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
16+
17+
**Output:** 2
18+
19+
**Explanation:**
20+
21+
One potential sequence of moves is shown.
22+
The first move swaps the first and second column.
23+
The second move swaps the second and third row.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/06/29/chessboard2-grid.jpg)
28+
29+
**Input:** board = [[0,1],[1,0]]
30+
31+
**Output:** 0
32+
33+
**Explanation:** Also note that the board with 0 in the top left corner, is also a valid chessboard.
34+
35+
**Example 3:**
36+
37+
![](https://assets.leetcode.com/uploads/2021/06/29/chessboard3-grid.jpg)
38+
39+
**Input:** board = [[1,0],[1,0]]
40+
41+
**Output:** -1
42+
43+
**Explanation:** No matter what sequence of moves you make, you cannot end with a valid chessboard.
44+
45+
**Constraints:**
46+
47+
* `n == board.length`
48+
* `n == board[i].length`
49+
* `2 <= n <= 30`
50+
* `board[i][j]` is either `0` or `1`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0701_0800.s0780_reaching_points;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void reachingPoints() {
11+
assertThat(new Solution().reachingPoints(1, 1, 3, 5), equalTo(true));
12+
}
13+
14+
@Test
15+
void reachingPoints2() {
16+
assertThat(new Solution().reachingPoints(1, 1, 2, 2), equalTo(false));
17+
}
18+
19+
@Test
20+
void reachingPoints3() {
21+
assertThat(new Solution().reachingPoints(1, 1, 1, 1), equalTo(true));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0701_0800.s0781_rabbits_in_forest;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void numRabbits() {
11+
assertThat(new Solution().numRabbits(new int[] {1, 1, 2}), equalTo(5));
12+
}
13+
14+
@Test
15+
void numRabbits2() {
16+
assertThat(new Solution().numRabbits(new int[] {10, 10, 10}), equalTo(11));
17+
}
18+
19+
@Test
20+
void numRabbits3() {
21+
assertThat(new Solution().numRabbits(new int[] {1, 0, 1, 0, 0}), equalTo(5));
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0701_0800.s0782_transform_to_chessboard;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void movesToChessboard() {
11+
assertThat(
12+
new Solution()
13+
.movesToChessboard(
14+
new int[][] {
15+
{0, 1, 1, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}, {1, 0, 0, 1}
16+
}),
17+
equalTo(2));
18+
}
19+
20+
@Test
21+
void movesToChessboard2() {
22+
assertThat(new Solution().movesToChessboard(new int[][] {{0, 1}, {1, 0}}), equalTo(0));
23+
}
24+
25+
@Test
26+
void movesToChessboard3() {
27+
assertThat(new Solution().movesToChessboard(new int[][] {{1, 0}, {1, 0}}), equalTo(-1));
28+
}
29+
}

0 commit comments

Comments
 (0)