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

Commit1a945a6

Browse files
add 1558
1 parent19ac520 commit1a945a6

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1558|[Minimum Numbers of Function Calls to Make Target Array](https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1558.java)||Medium|Greedy|
1112
|1557|[Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1557.java)||Medium|Graph|
1213
|1556|[Thousand Separator](https://leetcode.com/problems/thousand-separator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1556.java)||Easy|String|
1314
|1551|[Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1551.java)|[:tv:](https://youtu.be/A-i2sxmBqAA)|Medium|Math|
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_1558 {
6+
publicstaticclassSolution1 {
7+
publicintminOperations(int[]nums) {
8+
Arrays.sort(nums);
9+
intops =0;
10+
while (!allZero(nums)) {
11+
if (allEvenAndNonZeroes(nums)) {
12+
nums =half(nums);
13+
ops++;
14+
}elseif (hasOdds(nums)) {
15+
int[]result =newint[nums.length];
16+
for (inti =0;i <nums.length;i++) {
17+
if (nums[i] %2 !=0) {
18+
result[i] =nums[i] -1;
19+
ops++;
20+
}else {
21+
result[i] =nums[i];
22+
}
23+
}
24+
nums =result;
25+
}else {
26+
int[]result =newint[nums.length];
27+
for (inti =0;i <nums.length;i++) {
28+
if (nums[i] !=0) {
29+
result[i] =nums[i] /2;
30+
}else {
31+
result[i] =nums[i];
32+
}
33+
}
34+
nums =result;
35+
ops++;
36+
}
37+
}
38+
returnops;
39+
}
40+
41+
privatebooleanhasOdds(int[]nums) {
42+
for (inti :nums) {
43+
if (i %2 !=0) {
44+
returntrue;
45+
}
46+
}
47+
returnfalse;
48+
}
49+
50+
privateint[]half(int[]nums) {
51+
int[]result =newint[nums.length];
52+
for (inti =0;i <nums.length;i++) {
53+
result[i] =nums[i] /2;
54+
}
55+
returnresult;
56+
}
57+
58+
privatebooleanallEvenAndNonZeroes(int[]nums) {
59+
for (inti :nums) {
60+
if (i %2 !=0 ||i ==0) {
61+
returnfalse;
62+
}
63+
}
64+
returntrue;
65+
}
66+
67+
privatebooleanallZero(int[]nums) {
68+
for (inti :nums) {
69+
if (i !=0) {
70+
returnfalse;
71+
}
72+
}
73+
returntrue;
74+
}
75+
}
76+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1558;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1558Test {
10+
privatestatic_1558.Solution1solution1;
11+
privatestaticint[]nums;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1558.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
nums =newint[]{1,5};
21+
assertEquals(5,solution1.minOperations(nums));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
nums =newint[]{2,2};
27+
assertEquals(3,solution1.minOperations(nums));
28+
}
29+
30+
@Test
31+
publicvoidtest3() {
32+
nums =newint[]{4,2,5};
33+
assertEquals(6,solution1.minOperations(nums));
34+
}
35+
36+
@Test
37+
publicvoidtest4() {
38+
nums =newint[]{3,2,2,4};
39+
assertEquals(7,solution1.minOperations(nums));
40+
}
41+
42+
@Test
43+
publicvoidtest5() {
44+
nums =newint[]{2,4,8,16};
45+
assertEquals(8,solution1.minOperations(nums));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp