Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit973ef65

Browse files
add 1775
1 parentf3672ac commit973ef65

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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|
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_1775 {
6+
publicstaticclassSolution1 {
7+
publicintminOperations(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+
inti =0;
17+
intj =0;
18+
intdiff =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+
intminOps =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+
}elseif (i <longer.length) {
41+
diff +=6 -longer[i++];
42+
}else {
43+
diff +=shorter[j--] -1;
44+
}
45+
minOps++;
46+
}
47+
returnminOps;
48+
}elseif (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+
}elseif (i >=0) {
61+
diff -=longer[i--] -1;
62+
}else {
63+
diff -=6 -shorter[j++];
64+
}
65+
minOps++;
66+
}
67+
returnminOps;
68+
}else {
69+
returnminOps;
70+
}
71+
}
72+
}
73+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1775;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1775Test {
10+
privatestatic_1775.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1775.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(3,solution1.minOperations(newint[]{1,2,3,4,5,6},newint[]{1,1,2,2,2,2}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(-1,solution1.minOperations(newint[]{1,1,1,1,1,1,1},newint[]{6}));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(3,solution1.minOperations(newint[]{6,6},newint[]{1}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp