-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path697.degree-of-an-array.py
50 lines (40 loc) · 1.39 KB
/
697.degree-of-an-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# @lc app=leetcode id=697 lang=python3
#
# [697] Degree of an Array
#
# @lc code=start
# TAGS: Array, Hash Table
import collections
from typing import List
# 1. Find degree of the array
# 2. Find elements with degree
# 3. Find leftmost and rightmost index of the elements from step 2
# 4. Find minimum (right - left) + 1
class Solution:
# 360 ms, 46%. Time: O(N). Space: O(N)
def findShortestSubArray(self, nums: List[int]) -> int:
counters = collections.Counter(nums)
degree = max(counters.values())
elements = [num for num, freq in counters.items() if freq == degree]
first_i = {}
last_i = {}
for i, num in enumerate(nums):
if num not in first_i:
first_i[num] = i
last_i[num] = i
return min(last_i[num] - first_i[num] + 1 for num in elements)
# 321 ms, 57%. O(N).
# Only go through nums once instead of multiple times like previous solution
def findShortestSubArray2(self, nums):
first, count, ans, degree = {}, {}, 0, 0
for i, num in enumerate(nums):
first.setdefault(num, i)
count[num] = count.get(num, 0) + 1
if count[num] > degree:
degree = count[num]
ans = i - first[num] + 1
elif count[num] == degree:
ans = min(ans, i - first[num] + 1)
return ans
# @lc code=end