Skip to content

Commit 973ef65

Browse files
add 1775
1 parent f3672ac commit 973ef65

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _If you like this project, please leave me a star._ ★
1313
|1781|[Sum of Beauty of All Substrings](https://leetcode.com/problems/sum-of-beauty-of-all-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1781.java) ||Medium|HashTable, String|
1414
|1780|[Check if Number is a Sum of Powers of Three](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1780.java) ||Medium|Math, Backtracking, Recursion|
1515
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1779.java) ||Easy|Array|
16+
|1775|[Equal Sum Arrays With Minimum Number of Operations](https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1775.java) ||Medium|Greedy|
1617
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java) ||Medium|Greedy|
1718
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1773.java) ||Easy|Array, String|
1819
|1772|[Sort Features by Popularity](https://leetcode.com/problems/sort-features-by-popularity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1772.java) ||Medium|HashTable, Sort|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1775 {
6+
public static class Solution1 {
7+
public int minOperations(int[] nums1, int[] nums2) {
8+
int[] longer = nums1.length > nums2.length ? nums1 : nums2;
9+
int[] shorter = nums1.length > nums2.length ? nums2 : nums1;
10+
if (longer.length > shorter.length * 6) {
11+
/**This is the impossible case that we'll rule out first.*/
12+
return -1;
13+
}
14+
Arrays.sort(longer);
15+
Arrays.sort(shorter);
16+
int i = 0;
17+
int j = 0;
18+
int diff = 0;
19+
while (i < longer.length || j < shorter.length) {
20+
if (i < longer.length) {
21+
diff += longer[i++];
22+
}
23+
if (j < shorter.length) {
24+
diff -= shorter[j++];
25+
}
26+
}
27+
int minOps = 0;
28+
i = 0;
29+
j = shorter.length - 1;
30+
if (diff < 0) {
31+
/**if diff is negative, this means we'll need to decrease numbers in the shorter array and increase the numbers in the longer array to make the diff to be zero
32+
* and each time, we'll be greedy: take the bigger delta from two of the arrays.*/
33+
while (diff < 0) {
34+
if (i < longer.length && j >= 0) {
35+
if (6 - longer[i] < shorter[j] - 1) {
36+
diff += shorter[j--] - 1;
37+
} else {
38+
diff += 6 - longer[i++];
39+
}
40+
} else if (i < longer.length) {
41+
diff += 6 - longer[i++];
42+
} else {
43+
diff += shorter[j--] - 1;
44+
}
45+
minOps++;
46+
}
47+
return minOps;
48+
} else if (diff > 0) {
49+
/**if diff is positive, this means we'll need to decrease the numbers in the longer array and increase the numbers in the shorter array to make the diff to be zero
50+
* and each time, we'll be greedy: take the bigger delta from two of the arrays.*/
51+
i = longer.length - 1;
52+
j = 0;
53+
while (diff > 0) {
54+
if (i >= 0 && j < shorter.length) {
55+
if (longer[i] - 1 > 6 - shorter[j]) {
56+
diff -= longer[i--] - 1;
57+
} else {
58+
diff -= 6 - shorter[j++];
59+
}
60+
} else if (i >= 0) {
61+
diff -= longer[i--] - 1;
62+
} else {
63+
diff -= 6 - shorter[j++];
64+
}
65+
minOps++;
66+
}
67+
return minOps;
68+
} else {
69+
return minOps;
70+
}
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1775;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1775Test {
10+
private static _1775.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1775.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.minOperations(new int[]{1, 2, 3, 4, 5, 6}, new int[]{1, 1, 2, 2, 2, 2}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.minOperations(new int[]{1, 1, 1, 1, 1, 1, 1}, new int[]{6}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(3, solution1.minOperations(new int[]{6, 6}, new int[]{1}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)