- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1558.java
73 lines (68 loc) · 2.15 KB
/
_1558.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
packagecom.fishercoder.solutions.secondthousand;
publicclass_1558 {
publicstaticclassSolution1 {
publicintminOperations(int[] nums) {
intops = 0;
while (!allZero(nums)) {
if (allEvenAndNonZeroes(nums)) {
nums = half(nums);
ops++;
} elseif (hasOdds(nums)) {
int[] result = newint[nums.length];
for (inti = 0; i < nums.length; i++) {
if (nums[i] % 2 != 0) {
result[i] = nums[i] - 1;
ops++;
} else {
result[i] = nums[i];
}
}
nums = result;
} else {
int[] result = newint[nums.length];
for (inti = 0; i < nums.length; i++) {
if (nums[i] != 0) {
result[i] = nums[i] / 2;
} else {
result[i] = nums[i];
}
}
nums = result;
ops++;
}
}
returnops;
}
privatebooleanhasOdds(int[] nums) {
for (inti : nums) {
if (i % 2 != 0) {
returntrue;
}
}
returnfalse;
}
privateint[] half(int[] nums) {
int[] result = newint[nums.length];
for (inti = 0; i < nums.length; i++) {
result[i] = nums[i] / 2;
}
returnresult;
}
privatebooleanallEvenAndNonZeroes(int[] nums) {
for (inti : nums) {
if (i % 2 != 0 || i == 0) {
returnfalse;
}
}
returntrue;
}
privatebooleanallZero(int[] nums) {
for (inti : nums) {
if (i != 0) {
returnfalse;
}
}
returntrue;
}
}
}