Skip to content

Commit dd346ca

Browse files
author
luzhipeng
committed
chore: 增加五个候选题目
1 parent 7558780 commit dd346ca

5 files changed

+151
-0
lines changed

131.palindrome-partitioning.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* @lc app=leetcode id=131 lang=javascript
3+
*
4+
* [131] Palindrome Partitioning
5+
*/
6+
/**
7+
* @param {string} s
8+
* @return {string[][]}
9+
*/
10+
var partition = function(s) {
11+
12+
};
13+

134.gas-station.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=134 lang=javascript
3+
*
4+
* [134] Gas Station
5+
*/
6+
/**
7+
* @param {number[]} gas
8+
* @param {number[]} cost
9+
* @return {number}
10+
*/
11+
var canCompleteCircuit = function(gas, cost) {
12+
13+
};
14+

227.basic-calculator-ii.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=227 lang=javascript
3+
*
4+
* [227] Basic Calculator II
5+
*/
6+
/**
7+
* @param {string} s
8+
* @return {number}
9+
*/
10+
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+
35+
};
36+

300.longest-increasing-subsequence.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode id=300 lang=javascript
3+
*
4+
* [300] Longest Increasing Subsequence
5+
*/
6+
/**
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
var lengthOfLIS = function(nums) {
11+
if (nums.length === 0) return 0;
12+
// const dp = Array(nums.length).fill(1);
13+
// let max = 1;
14+
15+
// for (let i = 0; i < nums.length; i++) {
16+
// for (let j = 0; j < i; j++) {
17+
// if (nums[i] > nums[j]) {
18+
// dp[i] = Math.max(dp[j] + 1, dp[i]);
19+
// }
20+
// max = Math.max(max, dp[i]);
21+
// }
22+
// }
23+
24+
// return max;
25+
26+
// [ 10, 9, 2, 5, 3, 7, 101, 18 ]
27+
// [ 2, 3, 5, 7, 9, 10, 18, 101 ]
28+
};

84.largest-rectangle-in-histogram.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @lc app=leetcode id=84 lang=javascript
3+
*
4+
* [84] Largest Rectangle in Histogram
5+
*/
6+
/**
7+
* @param {number[]} heights
8+
* @return {number}
9+
*/
10+
var largestRectangleArea = function(heights) {
11+
// 木桶原理
12+
// 暴力求解法 时间复杂度O(n^2) 空间复杂度O(1)
13+
// if (heights.length === 1) return heights[0];
14+
15+
// let min = null;
16+
// let max = 0;
17+
18+
// for (let i = 0; i < heights.length; i++) {
19+
// min = heights[i];
20+
// for (let j = i; j < heights.length; j++) {
21+
// min = Math.min(min, heights[j]);
22+
// max = Math.max((j - i + 1) * min, max);
23+
// }
24+
// }
25+
26+
// return max;
27+
// 上面的暴力求解,其实可以做一个小优化,就是通过取局部最大值来减少一部分重复计算,但是时间复杂度还是O(n^2)
28+
29+
// 关键点: 1. 单调栈(Monotone Stack),线性复杂度,因为所有元素只会进入栈一次,并且出栈后再也不会进栈了 2.如果用暴力求解的话,你要会找出所有组合的方法(大部分题目都是两两组合,如果是任意组合的情况,暴力的话复杂度是2^n,
30+
// 这种情况,暴力求解通常不不取,需要考虑别的思路)
31+
// 当前题目就是两两组合 ,时间复杂度是O(n^2),在可以接受的范围
32+
33+
// 社区中流行的一种解法: 单调栈, 在这里我们需要使用单调递增栈
34+
// 时间复杂度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();
48+
49+
max = Math.max(max, heights[p] * (ascStack.length === 0 ? i : i - p));
50+
}
51+
52+
ascStack.push(i);
53+
}
54+
55+
return max;
56+
57+
// 相关题目: 雨水收集
58+
59+
// 直方图矩形面积要最大的话,需要尽可能的使得连续的矩形多,并且最低一块的高度要高
60+
};

0 commit comments

Comments
 (0)