Skip to content

Commit 2568c80

Browse files
add 3189
1 parent c1448d7 commit 2568c80

File tree

3 files changed

+54
-1
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+54
-1
lines changed

paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3194.java) | | Easy |
66
| 3192 | [Minimum Operations to Make Binary Array Elements Equal to One II](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3192.java) | | Medium |
77
| 3191 | [Minimum Operations to Make Binary Array Elements Equal to One I](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3191.java) | | Medium |
8-
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3190.java) | | Easy |
8+
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3190.java) | | Easy |
9+
| 3189 | [Minimum Moves to Get a Peaceful Board](https://leetcode.com/problems/minimum-moves-to-get-a-peaceful-board/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java) | | Medium | Greedy
910
| 3186 | [Maximum Total Damage With Spell Casting](https://leetcode.com/problems/maximum-total-damage-with-spell-casting/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3186.java) | | Medium | DP
1011
| 3185 | [Count Pairs That Form a Complete Day II](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3185.java) | | Medium |
1112
| 3184 | [Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3184.java) | | Easy |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _3189 {
6+
public static class Solution1 {
7+
/**
8+
* Greedy is the way to go for this problem.
9+
*/
10+
public int minMoves(int[][] rooks) {
11+
Arrays.sort(rooks, (a, b) -> a[0] - b[0]);
12+
int len = rooks.length;
13+
int moves = 0;
14+
for (int i = 0; i < len; i++) {
15+
int[] rook = rooks[i];
16+
//move each rook to row i
17+
moves += Math.abs(rook[0] - i);
18+
}
19+
Arrays.sort(rooks, (a, b) -> a[1] - b[1]);
20+
for (int i = 0; i < len; i++) {
21+
int[] rook = rooks[i];
22+
//move each rook to its column i
23+
moves += Math.abs(rook[1] - i);
24+
}
25+
return moves;
26+
}
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3189;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3189Test {
10+
private static _3189.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3189.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.minMoves(new int[][]{
20+
{0, 0}, {1, 0}, {1, 1}
21+
}));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)