Skip to content

Commit 5ddc852

Browse files
add 1133
1 parent 345447f commit 5ddc852

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _If you like this project, please leave me a star._ ★
3131
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java) | | | |Easy||
3232
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
3333
|1134|[Armstrong Number](https://leetcode.com/problems/armstrong-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1134.java) | | | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4)|Easy||
34+
|1133|[Largest Unique Number](https://leetcode.com/problems/largest-unique-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1133.java) | | | |Easy||
3435
|1128|[Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1128.java) | | | [:tv:](https://www.youtube.com/watch?v=7EpEEHAAxyw)|Easy||
3536
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
3637
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | | | |Easy||
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.TreeMap;
4+
5+
/**
6+
* 1133. Largest Unique Number
7+
*
8+
* Given an array of integers A, return the largest integer that only occurs once.
9+
* If no integer occurs once, return -1.
10+
*
11+
* Example 1:
12+
* Input: [5,7,3,9,4,9,8,3,1]
13+
* Output: 8
14+
* Explanation:
15+
* The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it's the answer.
16+
*
17+
* Example 2:
18+
* Input: [9,9,8,8]
19+
* Output: -1
20+
* Explanation:
21+
* There is no number that occurs only once.
22+
*
23+
* Note:
24+
* 1 <= A.length <= 2000
25+
* 0 <= A[i] <= 1000
26+
* */
27+
public class _1133 {
28+
public static class Solution1 {
29+
public int largestUniqueNumber(int[] A) {
30+
TreeMap<Integer, Integer> treeMap = new TreeMap<>((a, b) -> b - a);
31+
for (int num : A) {
32+
treeMap.put(num, treeMap.getOrDefault(num, 0) + 1);
33+
}
34+
for (int key : treeMap.keySet()) {
35+
if (treeMap.get(key) == 1) {
36+
return key;
37+
}
38+
}
39+
return -1;
40+
}
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1133;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1133Test {
10+
private static _1133.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1133.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[]{5, 7, 3, 9, 4, 9, 8, 3, 1};
21+
assertEquals(8, solution1.largestUniqueNumber(A));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[]{9, 9, 8, 8};
27+
assertEquals(-1, solution1.largestUniqueNumber(A));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)