Skip to content

Commit f3335a8

Browse files
author
luzhipeng
committed
chore: 候选题
1 parent dac830c commit f3335a8

7 files changed

+198
-45
lines changed

134.gas-station.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,59 @@
33
*
44
* [134] Gas Station
55
*/
6+
7+
function getIndex(index, n) {
8+
if (index > n - 1) {
9+
return index - n;
10+
}
11+
return index;
12+
}
613
/**
714
* @param {number[]} gas
815
* @param {number[]} cost
916
* @return {number}
1017
*/
1118
var canCompleteCircuit = function(gas, cost) {
19+
// bad 时间复杂度O(n^2)
20+
// let remain = 0;
21+
// const n = gas.length;
22+
// for (let i = 0; i < gas.length; i++) {
23+
// remain += gas[i];
24+
// remain -= cost[i];
25+
// let count = 0;
26+
// while (remain >= 0) {
27+
// count++;
28+
// if (count === n) return i;
29+
// remain += gas[getIndex(i + count, n)];
30+
// remain -= cost[getIndex(i + count, n)];
31+
// }
32+
// remain = 0;
33+
// }
34+
// return -1;
35+
// better solution 时间复杂度O(n)
36+
37+
const n = gas.length;
38+
let total = 0;
39+
let remain = 0;
40+
let start = 0;
41+
42+
for(let i = 0; i < n; i++) {
43+
total += gas[i];
44+
total -= cost[i]
45+
46+
remain += gas[i];
47+
remain -= cost[i];
1248

13-
};
49+
// 如果remain < 0, 说明从start到i走不通
50+
// 并且从start到i走不通,那么所有的solution中包含start到i的肯定都走不通
51+
// 因此我们重新从i + 1开始作为start
52+
if (remain < 0) {
53+
remain = 0;
54+
start = i + 1;
55+
}
56+
}
57+
// 事实上,我们遍历一遍,也就确定了每一个元素作为start是否可以走完一圈
1458

59+
// 如果cost总和大于gas总和,无论如何也无法走到终点
60+
return total >= 0 ? start : -1;
61+
};

14.longest-common-prefix.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode id=14 lang=javascript
3+
*
4+
* [14] Longest Common Prefix
5+
*/
6+
7+
function isCommonPrefix(strs, middle) {
8+
const prefix = strs[0].substring(0, middle);
9+
for (let i = 1; i < strs.length; i++) {
10+
if (!strs[i].startsWith(prefix)) return false;
11+
}
12+
13+
return true;
14+
}
15+
/**
16+
* @param {string[]} strs
17+
* @return {string}
18+
*/
19+
var longestCommonPrefix = function(strs) {
20+
// trie 解法
21+
// 时间复杂度O(m) 空间复杂度O(m * n)
22+
23+
// tag: 二分法
24+
// 时间复杂度 O(n*logm*logm) 空间复杂度O(1)
25+
if (strs.length === 0) return "";
26+
if (strs.length === 1) return strs[0];
27+
28+
let minLen = Number.MAX_VALUE;
29+
30+
for (let i = 0; i < strs.length; i++) {
31+
minLen = Math.min(minLen, strs[i].length);
32+
}
33+
34+
let low = 0;
35+
let high = minLen;
36+
37+
while (low <= high) {
38+
const middle = (low + high) >> 1;
39+
if (isCommonPrefix(strs, middle)) low = middle + 1;
40+
else high = middle - 1;
41+
}
42+
43+
return strs[0].substring(0, (low + high) >> 1);
44+
};

227.basic-calculator-ii.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,5 @@
88
* @return {number}
99
*/
1010
var calculate = function(s) {
11-
const chars = s.split('').filter(q => q);
12-
13-
const stack = [];
14-
for(let c of chars) {
15-
if (!isNaN(c)) {
16-
stack.push(c);
17-
} else {
18-
const a = stack.pop();
19-
const b = stack.pop();
20-
21-
if (c === '+') {
22-
stack.push(b + a);
23-
} else if (c === '-') {
24-
stack.push(b - a);
25-
} else if (c === '*') {
26-
stack.push(b * a);
27-
} else if (c === '/') {
28-
stack.push(Math.floor(b / a));
29-
}
30-
}
31-
}
32-
33-
return stack.pop();
34-
11+
// "3+2*2"
3512
};
36-

300.longest-increasing-subsequence.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* @return {number}
99
*/
1010
var lengthOfLIS = function(nums) {
11-
if (nums.length === 0) return 0;
11+
// 时间复杂度O(n^2)
12+
// if (nums.length === 0) return 0;
1213
// const dp = Array(nums.length).fill(1);
1314
// let max = 1;
1415

@@ -25,4 +26,20 @@ var lengthOfLIS = function(nums) {
2526

2627
// [ 10, 9, 2, 5, 3, 7, 101, 18 ]
2728
// [ 2, 3, 5, 7, 9, 10, 18, 101 ]
29+
30+
// 参考: https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation
31+
// const tails = [];
32+
// for (let i = 0; i < nums.length; i++) {
33+
// let left = 0;
34+
// let right = tails.length;
35+
// while (left < right) {
36+
// const mid = left + (right - left) / 2; // 防止溢出
37+
// if (tails[mid] < nums[i]) left = mid + 1;
38+
// else right = mid;
39+
// }
40+
// // 说明nums[i] 比如tails中所有数字都大,我们直接push
41+
// if (right === tails.length) tails.push(nums[i]);
42+
// else tails[right] = nums[i]; // 否则我们修改tails[right]
43+
// }
44+
// return tails.length;
2845
};

84.largest-rectangle-in-histogram.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ var largestRectangleArea = function(heights) {
3232

3333
// 社区中流行的一种解法: 单调栈, 在这里我们需要使用单调递增栈
3434
// 时间复杂度O(n) 空间复杂度O(n)
35-
const ascStack = [];
36-
let max = 0;
37-
heights.push(0); // hack, 为了使最后一个柱子也参与运算
38-
39-
for (let i = 0; i < heights.length; i++) {
40-
let p = i;
41-
42-
while (
43-
ascStack.length > 0 &&
44-
heights[i] < heights[ascStack[ascStack.length - 1]]
45-
) {
46-
// 由于是递增栈, height[p]一定是最小的,一定是短板
47-
p = ascStack.pop();
35+
// const ascStack = [];
36+
// let max = 0;
37+
// heights.push(0); // hack, 为了使最后一个柱子也参与运算
38+
39+
// for (let i = 0; i < heights.length; i++) {
40+
// let p = i;
41+
42+
// while (
43+
// ascStack.length > 0 &&
44+
// heights[i] < heights[ascStack[ascStack.length - 1]]
45+
// ) {
46+
// // 由于是递增栈, height[p]一定是最小的,一定是短板
47+
// p = ascStack.pop();
4848

49-
max = Math.max(max, heights[p] * (ascStack.length === 0 ? i : i - p));
50-
}
49+
// max = Math.max(max, heights[p] * (ascStack.length === 0 ? i : i - p));
50+
// }
5151

52-
ascStack.push(i);
53-
}
52+
// ascStack.push(i);
53+
// }
5454

55-
return max;
55+
// return max;
5656

5757
// 相关题目: 雨水收集
5858

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode id=5 lang=javascript
3+
*
4+
* [5] Longest Palindromic Substring
5+
*/
6+
/**
7+
* @param {string} s
8+
* @return {string}
9+
*/
10+
var longestPalindrome = function(s) {
11+
// babad
12+
// tag : dp
13+
if (!s || s.length === 0) return "";
14+
let res = s[0];
15+
16+
const dp = [];
17+
// dp[i][j] 表示 s中从i到j(包括i和j)是否可以形成回文
18+
19+
// 倒着遍历简化操作, 这么做的原因是dp[i][..]依赖于dp[i + 1][..]
20+
for (let i = s.length - 1; i >= 0; i--) {
21+
dp[i] = [];
22+
for (let j = i; j < s.length; j++) {
23+
if (j - i === 0) dp[i][j] = true;
24+
// specail case 1
25+
else if (j - i === 1 && s[i] === s[j]) dp[i][j] = true;
26+
// specail case 2
27+
else if (s[i] === s[j] && dp[i + 1][j - 1]) {
28+
// state transition
29+
dp[i][j] = true;
30+
}
31+
32+
if (dp[i][j] && j - i + 1 > res.length) {
33+
// update res
34+
res = s.slice(i, j + 1);
35+
}
36+
}
37+
}
38+
39+
return res;
40+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=516 lang=javascript
3+
*
4+
* [516] Longest Palindromic Subsequence
5+
*/
6+
/**
7+
* @param {string} s
8+
* @return {number}
9+
*/
10+
var longestPalindromeSubseq = function(s) {
11+
// bbbab 返回4
12+
// tag : dp
13+
const dp = [];
14+
15+
for(let i = s.length - 1; i >= 0; i--) {
16+
dp[i] = Array(s.length).fill(0);
17+
for(let j = i; j < s.length; j++) {
18+
if (i - j === 0) dp[i][j] = 1;
19+
else if (s[i] === s[j]) {
20+
dp[i][j] = dp[i + 1][j - 1] + 2;
21+
} else {
22+
dp[i][j] = Math.max(dp[i][j -1], dp[i +1][j]);
23+
}
24+
}
25+
}
26+
27+
return dp[0][s.length - 1]
28+
};
29+

0 commit comments

Comments
 (0)