Skip to content

Commit 687135f

Browse files
[N-0] add 697
1 parent ba06e19 commit 687135f

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|697|[Degree of an Array](https://leetcode.com/problems/degree-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_697.java) | O(n) | O(n) | Easy |
2526
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_695.java) | O(m*n) | O(1) | Easy | DFS
2627
|694|[Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_694.java) | O(m*n) | O(1) | Medium | DFS
2728
|693|[Binary Number with Alternating Bits](https://leetcode.com/problems/binary-number-with-alternating-bits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_693.java) | O(n) | O(1) | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* 697. Degree of an Array
10+
*
11+
* Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
12+
* Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
13+
14+
Example 1:
15+
16+
Input: [1, 2, 2, 3, 1]
17+
Output: 2
18+
Explanation:
19+
The input array has a degree of 2 because both elements 1 and 2 appear twice.
20+
Of the subarrays that have the same degree:
21+
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
22+
The shortest length is 2. So return 2.
23+
24+
Example 2:
25+
Input: [1,2,2,3,1,4,2]
26+
Output: 6
27+
28+
Note:
29+
nums.length will be between 1 and 50,000.
30+
nums[i] will be an integer between 0 and 49,999.
31+
*/
32+
public class _697 {
33+
public static class Solution1 {
34+
public int findShortestSubArray(int[] nums) {
35+
Map<Integer, Integer> map = new HashMap<>();
36+
for (int i = 0; i < nums.length; i++) {
37+
if (map.containsKey(nums[i])) {
38+
map.put(nums[i], map.get(nums[i]) + 1);
39+
} else {
40+
map.put(nums[i], 1);
41+
}
42+
}
43+
int degree = -1;
44+
for (int key : map.keySet()) {
45+
degree = Math.max(degree, map.get(key));
46+
}
47+
List<Integer> candidateNums = new ArrayList();
48+
for (int key : map.keySet()) {
49+
if (map.get(key) == degree) {
50+
candidateNums.add(key);
51+
}
52+
}
53+
int shortest = Integer.MAX_VALUE;
54+
for (int candidate : candidateNums) {
55+
shortest = Math.min(shortest, findLength(nums, candidate));
56+
}
57+
return shortest;
58+
}
59+
60+
int findLength(int[] arr, int candidate) {
61+
{
62+
int firstAppearance = Integer.MAX_VALUE;
63+
for (int i = 0; i < arr.length; i++) {
64+
if (arr[i] == candidate) {
65+
firstAppearance = i;
66+
break;
67+
}
68+
}
69+
int lastAppearance = arr.length - 1;
70+
for (int i = arr.length - 1; i > firstAppearance; i--) {
71+
if (arr[i] == candidate) {
72+
lastAppearance = i;
73+
break;
74+
}
75+
}
76+
return (lastAppearance - firstAppearance) + 1;
77+
}
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._697;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _697Test {
10+
private static _697.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _697.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1};
21+
assertEquals(1, solution1.findShortestSubArray(nums));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)