Skip to content

Commit 63d879c

Browse files
committed
Merge branch 'master' of github.com:everthis/leetcode-js
2 parents 92f4507 + a663226 commit 63d879c

37 files changed

+1081
-27
lines changed

1041-robot-bounded-in-circle.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {string} instructions
3+
* @return {boolean}
4+
*/
5+
const isRobotBounded = function(instructions) {
6+
let x = 0, y = 0, i = 0, d = [[0, 1], [1, 0], [0, -1], [ -1, 0]];
7+
for (let j = 0; j < instructions.length; ++j)
8+
if (instructions.charAt(j) === 'R') i = (i + 1) % 4;
9+
else if (instructions .charAt(j) === 'L') i = (i + 3) % 4;
10+
else {
11+
x += d[i][0]; y += d[i][1];
12+
}
13+
return x == 0 && y == 0 || i > 0;
14+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} A
3+
* @param {number} K
4+
* @return {number}
5+
*/
6+
const maxSumAfterPartitioning = function(A, K) {
7+
const N = A.length
8+
const dp = new Array(N).fill(0);
9+
for (let i = 0; i < N; ++i) {
10+
let curMax = 0;
11+
for (let j = 1; j <= K && i - j + 1 >= 0; j++) {
12+
curMax = Math.max(curMax, A[i - j + 1]);
13+
dp[i] = Math.max(dp[i], (i >= j ? dp[i - j] : 0) + curMax * j);
14+
}
15+
}
16+
return dp[N - 1];
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} S
3+
* @return {string}
4+
*/
5+
const removeDuplicates = function(S) {
6+
const queue = []
7+
for(let i = 0; i < S.length; i++) {
8+
if(queue.length > 0 && queue[queue.length - 1] === S[i]) {
9+
queue.pop()
10+
} else {
11+
queue.push(S[i])
12+
}
13+
}
14+
return queue.join('')
15+
};

1048-longest-string-chain.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {number}
4+
*/
5+
const longestStrChain = function(words) {
6+
words.sort((a, b) => a.length - b.length)
7+
const dp = {}
8+
for(let el of words) {
9+
dp[el] = 1
10+
}
11+
12+
let res = Number.MIN_VALUE
13+
for(let w of words) {
14+
for(let i = 0; i < w.length; i++) {
15+
let prev = w.slice(0, i) + w.slice(i + 1)
16+
dp[w] = Math.max(dp[w], (dp[prev] || 0) + 1 )
17+
}
18+
if(dp[w] > res) res = dp[w]
19+
}
20+
return res
21+
};

1049-last-stone-weight-ii.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} stones
3+
* @return {number}
4+
*/
5+
const lastStoneWeightII = function(stones) {
6+
let sum=stones.reduce((a,b)=>a+b)
7+
let dp=Array(sum+1).fill(0)
8+
dp[0]=1
9+
for(let i=0;i<stones.length;i++){
10+
let cur=stones[i]
11+
for(let j=dp.length-1;j>=0;j--){
12+
if(j-stones[i]<0)break
13+
if(dp[j-stones[i]]){
14+
dp[j]=1
15+
}
16+
}
17+
}
18+
19+
let minLen=Infinity
20+
for(let i=0;i<dp.length;i++){
21+
if(dp[i]){
22+
if(i*2-sum>=0)minLen=Math.min(minLen,i*2-sum)
23+
}
24+
}
25+
return minLen
26+
};

1051-height-checker.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number[]} heights
3+
* @return {number}
4+
*/
5+
const heightChecker = function(heights) {
6+
const arr = heights.slice(0).sort((a, b) => a - b)
7+
let res = 0
8+
for(let i = 0, len = heights.length; i < len; i++) {
9+
if(arr[i] !== heights[i]) res++
10+
}
11+
12+
return res
13+
};

1052-grumpy-bookstore-owner.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} customers
3+
* @param {number[]} grumpy
4+
* @param {number} X
5+
* @return {number}
6+
*/
7+
const maxSatisfied = function(customers, grumpy, X) {
8+
if (customers.length === 1) return customers[0]
9+
const totalSatisfiedCustomers = customers.reduce(
10+
(ac, el, idx) => ac + (grumpy[idx] === 0 ? el : 0),
11+
0
12+
)
13+
const arr = customers.map((el, idx) => (grumpy[idx] === 1 ? el : 0))
14+
const acArr = []
15+
let ac = 0
16+
for (let i = 0, len = arr.length; i < len; i++) {
17+
acArr[i] = ac = ac + arr[i]
18+
}
19+
let max = 0
20+
for (let i = X - 1, len = grumpy.length; i < len; i++) {
21+
let tmp = i - X < 0 ? 0 : acArr[i - X]
22+
if (acArr[i] - tmp > max) max = acArr[i] - tmp
23+
}
24+
25+
return totalSatisfiedCustomers + max
26+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number[]}
4+
*/
5+
const prevPermOpt1 = function(A) {
6+
let n = A.length, left = n - 2, right = n - 1;
7+
while (left >= 0 && A[left] <= A[left + 1]) left--;
8+
if (left < 0) return A;
9+
while (A[left] <= A[right]) right--;
10+
while (A[right - 1] == A[right]) right--;
11+
swap(A,left,right)
12+
return A;
13+
};
14+
function swap(a, i, j) {
15+
let tmp = a[i]
16+
a[i] = a[j]
17+
a[j] = tmp
18+
}

1054-distant-barcodes.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} barcodes
3+
* @return {number[]}
4+
*/
5+
const rearrangeBarcodes = function(barcodes) {
6+
const map = {};
7+
barcodes.forEach(b => map[b] = (map[b] || 0) + 1);
8+
const keys = Object.keys(map).sort((k1, k2) => map[k1] - map[k2]);
9+
10+
let idx = 1;
11+
for (let k of keys) {
12+
let t = map[k];
13+
14+
for (let i = 0; i < t; i++) {
15+
if (idx >= barcodes.length) idx = 0;
16+
barcodes[idx] = k;
17+
idx += 2;
18+
}
19+
}
20+
21+
return barcodes;
22+
};

124-binary-tree-maximum-path-sum.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
const maxPathSum = function(root) {
13+
let obj = {
14+
max: Number.MIN_SAFE_INTEGER
15+
}
16+
traverse(root, obj)
17+
18+
return obj.max
19+
};
20+
21+
function traverse(node, obj) {
22+
if(node === null) return 0
23+
let left = Math.max(0, traverse(node.left, obj))
24+
let right = Math.max(0, traverse(node.right, obj))
25+
obj.max = Math.max(obj.max, node.val+left+right)
26+
return node.val + Math.max(left, right)
27+
}
28+

132-palindrome-partitioning-ii.js

+41-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,54 @@
22
* @param {string} s
33
* @return {number}
44
*/
5+
6+
const minCut = function(s) {
7+
const n = s.length
8+
if (n <= 1) return 0
9+
const dp = new Array(n).fill(0)
10+
for (let i = 1; i < n; i++) dp[i] = i
11+
for (let i = 1; i < n; i++) {
12+
// odd
13+
for (
14+
let start = i, end = i;
15+
end < n && start >= 0 && s[end] === s[start];
16+
start--, end++
17+
) {
18+
dp[end] = Math.min(dp[end], start === 0 ? 0 : dp[start - 1] + 1)
19+
}
20+
// even
21+
for (
22+
let start = i - 1, end = i;
23+
end < n && start >= 0 && s[end] === s[start];
24+
start--, end++
25+
) {
26+
dp[end] = Math.min(dp[end], start === 0 ? 0 : dp[start - 1] + 1)
27+
}
28+
}
29+
return dp[n - 1]
30+
}
31+
32+
33+
34+
// another
35+
536
const minCut = function(s) {
637
const n = s.length
738
const cut = new Array(n + 1).fill(0)
839
for (let i = 0; i <= n; i++) cut[i] = i - 1
940
for (let i = 0; i < n; i++) {
10-
for (
11-
let j = 0;
12-
i - j >= 0 && i + j < n && s[i - j] == s[i + j];
13-
j++ // odd length palindrome
14-
)
15-
cut[i + j + 1] = Math.min(cut[i + j + 1], 1 + cut[i - j])
16-
41+
// odd
42+
for (let j = 0; i + j < n && i - j >= 0 && s[i + j] === s[i - j]; j++) {
43+
cut[i + j + 1] = Math.min(cut[i + j + 1], cut[i - j] + 1)
44+
}
45+
// even
1746
for (
1847
let j = 1;
19-
i - j + 1 >= 0 && i + j < n && s[i - j + 1] == s[i + j];
20-
j++ // even length palindrome
21-
)
22-
cut[i + j + 1] = Math.min(cut[i + j + 1], 1 + cut[i - j + 1])
48+
i + j < n && i - j + 1 >= 0 && s[i + j] === s[i - j + 1];
49+
j++
50+
) {
51+
cut[i + j + 1] = Math.min(cut[i + j + 1], cut[i - j + 1] + 1)
52+
}
2353
}
2454
return cut[n]
2555
}

135-candy.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} ratings
3+
* @return {number}
4+
*/
5+
const candy = function(ratings) {
6+
const candies = new Array(ratings.length).fill(1);
7+
for (let i = 1; i < candies.length; i++) {
8+
if (ratings[i] > ratings[i - 1]) {
9+
candies[i] = candies[i - 1] + 1;
10+
}
11+
}
12+
let sum = candies[candies.length - 1];
13+
for (let i = candies.length - 2; i >= 0; i--) {
14+
if (ratings[i] > ratings[i + 1]) {
15+
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
16+
}
17+
sum += candies[i];
18+
}
19+
return sum;
20+
};

136-single-number.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const singleNumber = function(nums) {
6+
let xor = nums[0]
7+
for(let i = 1; i< nums.length; i++) xor ^= nums[i]
8+
return xor
9+
};

215-kth-largest-element-in-an-array.js

+82
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,85 @@ const findKthLargest = function(nums, k) {
2323
else if (k - larger.length - pivotCount <= 0) return pivot;
2424
else return findKthLargest(smaller, k - larger.length - pivotCount);
2525
};
26+
27+
// another
28+
29+
/**
30+
* @param {number[]} nums
31+
* @param {number} k
32+
* @return {number}
33+
*/
34+
const findKthLargest = function(nums, k) {
35+
return quickselect(nums, 0, nums.length - 1, k)
36+
};
37+
function quickselect(arr, lo, hi, k) {
38+
let pivtIdx = Math.floor(Math.random() * (hi - lo + 1)) + lo
39+
let pivtVal = arr[pivtIdx]
40+
;[arr[hi], arr[pivtIdx]] = [arr[pivtIdx], arr[hi]]
41+
let i = lo
42+
let j = hi - 1
43+
44+
while (i <= j) {
45+
if (arr[i] <= pivtVal) {
46+
i++
47+
} else {
48+
;[arr[j], arr[i]] = [arr[i], arr[j]]
49+
j--
50+
}
51+
}
52+
53+
;[arr[i], arr[hi]] = [arr[hi], arr[i]]
54+
55+
pivtIdx = i
56+
57+
if (pivtIdx === arr.length - k) return arr[pivtIdx]
58+
if (pivtIdx < arr.length - k) return quickselect(arr, pivtIdx + 1, hi, k)
59+
return quickselect(arr, lo, pivtIdx - 1, k)
60+
}
61+
62+
// another
63+
64+
/**
65+
* @param {number[]} nums
66+
* @param {number} k
67+
* @return {number}
68+
*/
69+
const findKthLargest = function(list, k) {
70+
const len = list.length
71+
let lo = 0
72+
let hi = len - 1
73+
let pivot = 0
74+
let t = len - k
75+
while(lo < hi) {
76+
pivot = partition(list, lo, hi)
77+
if(pivot === t) {
78+
break
79+
} else if(pivot < t) {
80+
lo = pivot + 1
81+
} else if(pivot > t) {
82+
hi = pivot - 1
83+
}
84+
}
85+
86+
return list[t]
87+
}
88+
89+
function partition(arr, s, e) {
90+
let t = arr[e]
91+
let i = s
92+
for(let j = s; j <= e - 1; j++) {
93+
if(arr[j] <= t) {
94+
swap(arr, i, j)
95+
i++
96+
}
97+
}
98+
swap(arr, i, e)
99+
return i
100+
}
101+
102+
function swap(arr, i, j) {
103+
let tmp = arr[i]
104+
arr[i] = arr[j]
105+
arr[j] = tmp
106+
}
107+

0 commit comments

Comments
 (0)