Skip to content

Commit 81fea39

Browse files
add 3912
1 parent 35b8baa commit 81fea39

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

  • paginated_contents/algorithms/4th_thousand
  • src

‎paginated_contents/algorithms/4th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| 3921 | [Score Validator](https://leetcode.com/problems/score-validator/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3921.java) | | Easy |
2020
| 3917 | [Count Indices With Opposite Parity](https://leetcode.com/problems/count-indices-with-opposite-parity/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3917.java) | | Easy |
2121
| 3913 | [Sort Vowels by Frequency](https://leetcode.com/problems/sort-vowels-by-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3913.java) | | Easy |
22+
| 3912 | [Valid Elements in an Array](https://leetcode.com/problems/valid-elements-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3912.java) | | Easy |
2223
| 3908 | [Valid Digit Number](https://leetcode.com/problems/valid-digit-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3908.java) | | Easy |
2324
| 3701 | [Compute Alternating Sum](https://leetcode.com/problems/compute-alternating-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3701.java) | | Easy |
2425
| 3662 | [Filter Characters by Frequency](https://leetcode.com/problems/filter-characters-by-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3662.java) | | Easy |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _3912 {
7+
public static class Solution1 {
8+
public List<Integer> findValidElements(int[] nums) {
9+
boolean[] valid = new boolean[nums.length];
10+
valid[0] = true;
11+
valid[nums.length - 1] = true;
12+
int leftMax = nums[0];
13+
for (int i = 1; i < nums.length; i++) {
14+
if (nums[i] > leftMax) {
15+
leftMax = nums[i];
16+
valid[i] = true;
17+
}
18+
}
19+
int rightMax = nums[nums.length - 1];
20+
for (int i = nums.length - 2; i >= 0; i--) {
21+
if (nums[i] > rightMax) {
22+
rightMax = nums[i];
23+
valid[i] = true;
24+
}
25+
}
26+
List<Integer> result = new ArrayList<>();
27+
for (int i = 0; i < nums.length; i++) {
28+
if (valid[i]) {
29+
result.add(nums[i]);
30+
}
31+
}
32+
return result;
33+
}
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3912;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class _3912Test {
13+
private _3912.Solution1 solution1;
14+
15+
@BeforeEach
16+
public void setup() {
17+
solution1 = new _3912.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(new ArrayList<>(Arrays.asList(1, 2, 4, 3, 2)), solution1.findValidElements(new int[]{1, 2, 4, 2, 3, 2}));
23+
}
24+
}

0 commit comments

Comments
 (0)