-
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
package com.fishercoder.solutions.fourththousand;
import java.util.Arrays;
public class _3354 {
public static class Solution1 {
public int countValidSelections(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
if (isValidWithMoveDirection(nums, i, true)) {
count++;
}
if (isValidWithMoveDirection(nums, i, false)) {
count++;
}
}
}
return count;
}
private boolean isValidWithMoveDirection(int[] nums, int index, boolean moveLeft) {
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 (int num : copy) {
if (num != 0) {
return false;
}
}
return true;
}
}
}