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

Lines changed: 14 additions & 0 deletions
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+
};
Lines changed: 17 additions & 0 deletions
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+
};
Lines changed: 15 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
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+

0 commit comments

Comments
 (0)