Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 30ed9bb

Browse files
697. Degree of an Array
1 parent a23f4d2 commit 30ed9bb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎src/697. Degree of an Array.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 697. Degree of an Array
3+
* https://leetcode.com/problems/degree-of-an-array/
4+
* @param {number[]} nums
5+
* @return {number}
6+
*/
7+
var findShortestSubArray = function (nums) {
8+
const map = Object.create(null);
9+
let maxCount = 1;
10+
11+
for (let i = 0; i < nums.length; i++) {
12+
if (!map[nums[i]]) {
13+
map[nums[i]] = {
14+
count: 1,
15+
start: i,
16+
end: i
17+
}
18+
} else {
19+
map[nums[i]].count++;
20+
map[nums[i]].end = i;
21+
maxCount = Math.max(maxCount, map[nums[i]].count);
22+
}
23+
}
24+
let minD = 0;
25+
for (let key in map) {
26+
if (map[key].count === maxCount) {
27+
minD = minD ? Math.min(map[key].end - map[key].start, minD) : map[key].end - map[key].start;
28+
}
29+
}
30+
return minD + 1;
31+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /