Skip to content

Commit 9983072

Browse files
committed
add 697 725 scripts.
1 parent 831dfb1 commit 9983072

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

697-degree-of-an-array.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const findShortestSubArray = function(nums) {
6+
const left = {};
7+
const right = {};
8+
const count = {};
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
if (!left.hasOwnProperty(nums[i])) {
12+
left[nums[i]] = i;
13+
}
14+
right[nums[i]] = i;
15+
count[nums[i]] = count[nums[i]] ? count[nums[i]] + 1 : 1;
16+
}
17+
const degree = Math.max(...Object.keys(count).map(el => count[el]));
18+
let res = nums.length;
19+
for (let el in count) {
20+
if (count.hasOwnProperty(el) && count[el] === degree) {
21+
res = Math.min(res, right[el] - left[el] + 1);
22+
}
23+
}
24+
25+
return res;
26+
};

725-split-linked-list-in-parts.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} root
10+
* @param {number} k
11+
* @return {ListNode[]}
12+
*/
13+
const splitListToParts = function(root, k) {
14+
let cur = root;
15+
let N = 0;
16+
while (cur != null) {
17+
cur = cur.next;
18+
N++;
19+
}
20+
let width = Math.floor(N / k),
21+
rem = N % k;
22+
let ans = [];
23+
cur = root;
24+
for (let i = 0; i < k; ++i) {
25+
let head = cur;
26+
for (let j = 0; j < width + (i < rem ? 1 : 0) - 1; ++j) {
27+
if (cur != null) cur = cur.next;
28+
}
29+
if (cur != null) {
30+
let prev = cur;
31+
cur = cur.next;
32+
prev.next = null;
33+
}
34+
ans[i] = head;
35+
}
36+
return ans;
37+
};

0 commit comments

Comments
 (0)