Skip to content

Commit fa75bc2

Browse files
committedNov 22, 2024
[LEET-3354] add 3354
1 parent a87e53d commit fa75bc2

File tree

3 files changed

+78
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+78
-0
lines changed
 

‎paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3354 | [Make Array Elements Equal to Zero](https://leetcode.com/problems/make-array-elements-equal-to-zero/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3354.java) | | Easy |
34
| 3349 | [Adjacent Increasing Subarrays Detection I](https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3349.java) | | Easy |
45
| 3340 | [Check Balanced String](https://leetcode.com/problems/check-balanced-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3340.java) | | Easy |
56
| 3330 | [Find the Original Typed String I](https://leetcode.com/problems/find-the-original-typed-string-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3330.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _3354 {
6+
public static class Solution1 {
7+
public int countValidSelections(int[] nums) {
8+
int count = 0;
9+
for (int i = 0; i < nums.length; i++) {
10+
if (nums[i] == 0) {
11+
if (isValidWithMoveDirection(nums, i, true)) {
12+
count++;
13+
}
14+
if (isValidWithMoveDirection(nums, i, false)) {
15+
count++;
16+
}
17+
}
18+
}
19+
return count;
20+
}
21+
22+
private boolean isValidWithMoveDirection(int[] nums, int index, boolean moveLeft) {
23+
int[] copy = Arrays.copyOf(nums, nums.length);
24+
while (index >= 0 && index < nums.length) {
25+
if (moveLeft) {
26+
if (copy[index] > 0) {
27+
copy[index]--;
28+
moveLeft = !moveLeft;
29+
index++;
30+
} else {
31+
index--;
32+
}
33+
} else {
34+
if (copy[index] > 0) {
35+
copy[index]--;
36+
moveLeft = !moveLeft;
37+
index--;
38+
} else {
39+
index++;
40+
}
41+
}
42+
}
43+
for (int num : copy) {
44+
if (num != 0) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3354;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3354Test {
10+
private _3354.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3354.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.countValidSelections(new int[]{1, 0, 2, 0, 3}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.countValidSelections(new int[]{16, 13, 10, 0, 0, 0, 10, 6, 7, 8, 7}));
25+
}
26+
}

0 commit comments

Comments
 (0)
Failed to load comments.