Skip to content

Commit e5982a9

Browse files
[LEET-3471] add 3471
1 parent 0d31b5a commit e5982a9

File tree

3 files changed

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

3 files changed

+58
-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+
| 3471 | [Find the Largest Almost Missing Integer](https://leetcode.com/problems/find-the-largest-almost-missing-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3471.java) | | Easy |
34
| 3396 | [Minimum Number of Operations to Make Elements in Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java) | | Easy |
45
| 3392 | [Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3392.java) | | Easy |
56
| 3386 | [Button with Longest Push Time](https://leetcode.com/problems/button-with-longest-push-time/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3386.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.TreeMap;
6+
7+
public class _3471 {
8+
public static class Solution1 {
9+
public int largestInteger(int[] nums, int k) {
10+
TreeMap<Integer, Integer> map = new TreeMap<>();
11+
for (int num : nums) {
12+
map.put(num, 0);
13+
}
14+
for (int i = 0; i <= nums.length - k; i++) {
15+
Set<Integer> set = new HashSet<>();
16+
for (int j = i; j < i + k; j++) {
17+
if (set.add(nums[j])) {
18+
map.put(nums[j], map.getOrDefault(nums[j], 0) + 1);
19+
}
20+
}
21+
}
22+
int ans = -1;
23+
for (int key : map.keySet()) {
24+
if (map.get(key) == 1) {
25+
ans = key;
26+
}
27+
}
28+
return ans;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3471;
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 _3471Test {
10+
private _3471.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3471.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(7, solution1.largestInteger(new int[]{3, 9, 2, 1, 7}, 3));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0, solution1.largestInteger(new int[]{0, 0}, 2));
25+
}
26+
}

0 commit comments

Comments
 (0)