Skip to content

Commit a7aaf47

Browse files
fishercoder1534zhahui
authored andcommitted
add 1207
1 parent 1738e27 commit a7aaf47

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | |Easy||
3031
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy||
3132
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||
3233
|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||
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
import java.util.Set;
8+
9+
/**
10+
* 1207. Unique Number of Occurrences
11+
*
12+
* Given an array of integers arr,
13+
* write a function that returns true if and only if the number of occurrences of each value in the array is unique.
14+
*
15+
* Example 1:
16+
* Input: arr = [1,2,2,1,1,3]
17+
* Output: true
18+
* Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
19+
*
20+
* Example 2:
21+
* Input: arr = [1,2]
22+
* Output: false
23+
*
24+
* Example 3:
25+
* Input: arr = [-3,0,1,-3,1,1,1,-3,10,0]
26+
* Output: true
27+
*
28+
* Constraints:
29+
* 1 <= arr.length <= 1000
30+
* -1000 <= arr[i] <= 1000
31+
* */
32+
public class _1207 {
33+
public static class Solution1 {
34+
public boolean uniqueOccurrences(int[] arr) {
35+
Map<Integer, Integer> map = new HashMap<>();
36+
Arrays.stream(arr).forEach(num -> {
37+
map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1);
38+
});
39+
Set<Integer> set = new HashSet<>();
40+
return map.keySet().stream().mapToInt(key -> key).allMatch(key -> set.add(map.get(key)));
41+
}
42+
}
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1207;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1207Test {
10+
private static _1207.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1207.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{1, 2, 2, 1, 1, 3};
21+
assertEquals(true, solution1.uniqueOccurrences(arr));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{1, 2};
27+
assertEquals(false, solution1.uniqueOccurrences(arr));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{-3, 0, 1, -3, 1, 1, 1, -3, 10, 0};
33+
assertEquals(true, solution1.uniqueOccurrences(arr));
34+
}
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy