- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3354.java
51 lines (48 loc) · 1.54 KB
/
_3354.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
packagecom.fishercoder.solutions.fourththousand;
importjava.util.Arrays;
publicclass_3354 {
publicstaticclassSolution1 {
publicintcountValidSelections(int[] nums) {
intcount = 0;
for (inti = 0; i < nums.length; i++) {
if (nums[i] == 0) {
if (isValidWithMoveDirection(nums, i, true)) {
count++;
}
if (isValidWithMoveDirection(nums, i, false)) {
count++;
}
}
}
returncount;
}
privatebooleanisValidWithMoveDirection(int[] nums, intindex, booleanmoveLeft) {
int[] copy = Arrays.copyOf(nums, nums.length);
while (index >= 0 && index < nums.length) {
if (moveLeft) {
if (copy[index] > 0) {
copy[index]--;
moveLeft = !moveLeft;
index++;
} else {
index--;
}
} else {
if (copy[index] > 0) {
copy[index]--;
moveLeft = !moveLeft;
index--;
} else {
index++;
}
}
}
for (intnum : copy) {
if (num != 0) {
returnfalse;
}
}
returntrue;
}
}
}