Skip to content

Commit 871b49f

Browse files
committed
solve problem Degree Of An Array
1 parent 1efc3ac commit 871b49f

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ All solutions will be accepted!
9292
|551|[Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/description/)|[java/py/js](./algorithms/StudentAttendanceRecordI)|Easy|
9393
|538|[Convert Bst To Greater Tree](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/description/)|[java/py/js](./algorithms/ConvertBstToGreaterTree)|Easy|
9494
|235|[Lowest Common Ancestor Of A Binary Search Tree](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)|[java/py/js](./algorithms/LowestCommonAncestorOfABinarySearchTree)|Easy|
95+
|697|[Degree Of An Array](https://leetcode-cn.com/problems/degree-of-an-array/description/)|[java/py/js](./algorithms/DegreeOfAnArray)|Easy|
9596

9697
# Database
9798
|#|Title|Solution|Difficulty|

algorithms/DegreeOfAnArray/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Degree Of An Array
2+
This problem is easy to solve by hashmap
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public int findShortestSubArray(int[] nums) {
3+
Map<Integer, Map<String, Integer>> numsMap = new HashMap<Integer, Map<String, Integer>>();
4+
List<Integer> mostNums = new ArrayList<Integer>();
5+
int maxCount = 0;
6+
int shortestLength = Integer.MAX_VALUE;
7+
8+
for (int i = 0; i < nums.length; i++) {
9+
int num = nums[i];
10+
Map<String, Integer> map;
11+
12+
if (numsMap.get(num) == null) {
13+
map = new HashMap<String, Integer>();
14+
map.put("start", i);
15+
map.put("end", i);
16+
map.put("count", 1);
17+
numsMap.put(num, map);
18+
} else {
19+
map = numsMap.get(num);
20+
map.put("end", i);
21+
map.put("count", map.get("count") + 1);
22+
numsMap.put(num, map);
23+
}
24+
25+
if (map.get("count") > maxCount) {
26+
maxCount = map.get("count");
27+
mostNums.clear();
28+
mostNums.add(num);
29+
} else if (map.get("count") == maxCount) {
30+
mostNums.add(num);
31+
}
32+
}
33+
34+
for (int num : mostNums) {
35+
Map<String, Integer> map = numsMap.get(num);
36+
shortestLength = Math.min(shortestLength, map.get("end") - map.get("start") + 1);
37+
}
38+
39+
return shortestLength;
40+
}
41+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findShortestSubArray = function(nums) {
6+
let numsMap = {},
7+
mostNums = [],
8+
maxCount = 0
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
let num = nums[i]
12+
13+
if (numsMap[num] === undefined) {
14+
numsMap[num] = {
15+
start: i,
16+
end: i,
17+
count: 1
18+
}
19+
} else {
20+
numsMap[num].end = i
21+
numsMap[num].count += 1
22+
}
23+
24+
if (numsMap[num].count > maxCount) {
25+
maxCount = numsMap[num].count
26+
mostNums = [num]
27+
} else if (numsMap[num].count === maxCount) {
28+
mostNums.push(num)
29+
}
30+
}
31+
32+
return Math.min(...mostNums.map(num => numsMap[num].end - numsMap[num].start + 1))
33+
};
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def findShortestSubArray(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
nums_map = {}
8+
most_nums = []
9+
max_count = 0
10+
11+
for i in range(0, len(nums)):
12+
num = nums[i]
13+
if nums_map.get(num) == None:
14+
nums_map[num] = {
15+
'start': i,
16+
'end': i,
17+
'count': 1
18+
}
19+
else:
20+
nums_map[num]['end'] = i
21+
nums_map[num]['count'] += 1
22+
23+
if nums_map[num]['count'] > max_count:
24+
max_count = nums_map[num]['count']
25+
most_nums = [num]
26+
elif nums_map[num]['count'] == max_count:
27+
most_nums.append(num)
28+
29+
return min([nums_map[num]['end'] - nums_map[num]['start'] + 1 for num in most_nums])
30+

0 commit comments

Comments
 (0)