Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c62aa1

Browse files
committed
697_Degree_of_an_Array
1 parent d76e889 commit 1c62aa1

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Remember solutions are only solutions to given problems. If you want full study
145145
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)|
146146
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
147147
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
148+
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
148149
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
149150
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |
150151
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/743_Network_Delay_Time.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/743_Network_Delay_Time.java) | Let V == N, then: 1. DFS, O(V^V+ElgE), O(V+E)<br>2. Dijkstra, O(V^2+E), O(V+E)|

java/697_Degree_of_an_Array.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int findShortestSubArray(int[] nums) {
3+
Map<Integer, Integer> left = new HashMap(),
4+
right = new HashMap(), count = new HashMap();
5+
6+
for (int i = 0; i < nums.length; i++) {
7+
int x = nums[i];
8+
// left most position
9+
if (left.get(x) == null) left.put(x, i);
10+
// right most position
11+
right.put(x, i);
12+
count.put(x, count.getOrDefault(x, 0) + 1);
13+
}
14+
15+
int ans = nums.length;
16+
int degree = Collections.max(count.values());
17+
for (int x: count.keySet()) {
18+
if (count.get(x) == degree) {
19+
ans = Math.min(ans, right.get(x) - left.get(x) + 1);
20+
}
21+
}
22+
return ans;
23+
}
24+
}

python/697_Degree_of_an_Array.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
# def findShortestSubArray(self, nums):
3+
# """
4+
# :type nums: List[int]
5+
# :rtype: int
6+
# """
7+
# res = len(nums)
8+
# counter = collections.Counter()
9+
# for num in nums:
10+
# counter[num] += 1
11+
# degree = max(counter.values())
12+
# for key, kdegree in counter.most_common():
13+
# if degree != kdegree:
14+
# break
15+
# res = min(res, self.smallestSubArray(nums, key, degree))
16+
# return res
17+
18+
# def smallestSubArray(self, nums, key, degree):
19+
# start = nums.index(key)
20+
# pos = start + 1
21+
# degree -= 1
22+
# while pos < len(nums) and degree != 0:
23+
# if nums[pos] == key:
24+
# degree -= 1
25+
# pos += 1
26+
# return pos - start
27+
28+
def findShortestSubArray(self, nums):
29+
left, right, count = {}, {}, {}
30+
for i, x in enumerate(nums):
31+
if x not in left: left[x] = i
32+
right[x] = i
33+
count[x] = count.get(x, 0) + 1
34+
35+
ans = len(nums)
36+
degree = max(count.values())
37+
for x in count:
38+
if count[x] == degree:
39+
ans = min(ans, right[x] - left[x] + 1)
40+
41+
return ans

0 commit comments

Comments
 (0)