Skip to content

Commit c46a926

Browse files
author
liguanliang1
committedApr 24, 2022
feat: doc
1 parent 9b6091b commit c46a926

File tree

4 files changed

+161
-31
lines changed

4 files changed

+161
-31
lines changed
 

‎leetcode/dynamic-programing/water-problem.js

+67-31
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,39 @@ import LinkedList from '../link-list/LinkedList'
22

33

44
// 积水问题,使用单调栈的思想
5-
class Solution42 {
6-
trap(height) {
7-
const stack = new LinkedList();
8-
// 最少也需要3个柱子才能积水,否则直接返回0
9-
if (height.length < 3) return 0;
10-
11-
let res = 0;
12-
for (let i = 0; i < height.length; i++) {
13-
// 当栈不为空,且不满足递减关系 弹栈并记录弹出元素下标,用于计算积水高度
14-
while (!stack.isEmpty() && height[i] > height[stack.getLast()]) {
15-
let temp = stack.deleteTail();
16-
// 当有重复连续值时都弹出
17-
while (!stack.isEmpty() && height[temp] == height[stack.getLast()]) {
18-
stack.deleteTail();
19-
}
20-
//计算积水深度
21-
if (!stack.isEmpty()) {
22-
// 计算宽度
23-
let width = i - stack.getLast() - 1;
24-
// 计算高度
25-
let high = Math.min(height[i] - height[temp], height[stack.getLast()] - height[temp]);
26-
// System.out.println(i + " " + width + " " + high + " " + temp);
27-
res += high * width;
28-
}
5+
function trap(height) {
6+
const stack = new LinkedList();
7+
// 最少也需要3个柱子才能积水,否则直接返回0
8+
if (height.length < 3) return 0;
9+
10+
let res = 0;
11+
for (let i = 0; i < height.length; i++) {
12+
// 当栈不为空,且不满足递减关系 弹栈并记录弹出元素下标,用于计算积水高度
13+
while (!stack.isEmpty() && height[i] > height[stack.getLast()]) {
14+
let temp = stack.deleteTail();
15+
// 当有重复连续值时都弹出
16+
while (!stack.isEmpty() && height[temp] == height[stack.getLast()]) {
17+
stack.deleteTail();
18+
}
19+
//计算积水深度
20+
if (!stack.isEmpty()) {
21+
// 计算宽度
22+
let width = i - stack.getLast() - 1;
23+
// 计算高度
24+
let high = Math.min(height[i] - height[temp], height[stack.getLast()] - height[temp]);
25+
// System.out.println(i + " " + width + " " + high + " " + temp);
26+
res += high * width;
2927
}
30-
stack.append(i);
31-
// System.out.println();
3228
}
33-
34-
return res;
29+
stack.append(i);
30+
// System.out.println();
3531
}
32+
33+
return res;
3634
}
3735

38-
const s = new Solution42()
39-
const result= s.trap([4,2,0,3,2,5])
40-
console.log('water-problem',result)
36+
const result = trap([4, 2, 0, 3, 2, 5])
37+
console.log('water-problem', result)
4138
// // 按层计算,负责度较高可能会超时
4239
// class Solution42_byLayer {
4340
// public let trap(let[] height) {
@@ -148,3 +145,42 @@ console.log('water-problem',result)
148145
// }
149146
// }
150147

148+
// 链接:https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode/
149+
// 暴力解法
150+
function trap(height = []) {
151+
let ans = 0;
152+
let size = height.length;
153+
for (let i = 1; i < size - 1; i++) {
154+
let max_left = 0, max_right = 0;
155+
for (let j = i; j >= 0; j--) { //Search the left part for max bar size
156+
max_left = Math.max(max_left, height[j]);
157+
}
158+
for (let j = i; j < size; j++) { //Search the right part for max bar size
159+
max_right = Math.max(max_right, height[j]);
160+
}
161+
ans += Math.min(max_left, max_right) - height[i];
162+
}
163+
return ans;
164+
}
165+
// 链接:https://leetcode-cn.com/problems/trapping-rain-water/solution/jie-yu-shui-by-leetcode/
166+
// 动态规划
167+
function trap(height = []) {
168+
if (height == null || height.length == 0)
169+
return 0;
170+
let ans = 0;
171+
let size = height.length;
172+
let left_max = new Array(size).fill(0);
173+
let right_max = new Array(size).fill(0);
174+
left_max[0] = height[0];
175+
for (let i = 1; i < size; i++) {
176+
left_max[i] = Math.max(height[i], left_max[i - 1]);
177+
}
178+
right_max[size - 1] = height[size - 1];
179+
for (let i = size - 2; i >= 0; i--) {
180+
right_max[i] = Math.max(height[i], right_max[i + 1]);
181+
}
182+
for (let i = 1; i < size - 1; i++) {
183+
ans += Math.min(left_max[i], right_max[i]) - height[i];
184+
}
185+
return ans;
186+
}

‎leetcode/invertBinaryTree.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var invertTree = function(root) {
2+
if (root === null) {
3+
return null;
4+
}
5+
const left = invertTree(root.left);
6+
const right = invertTree(root.right);
7+
root.left = right;
8+
root.right = left;
9+
return root;
10+
};

‎leetcode/knapsack/index.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
let weights = [35, 30, 60, 50, 40, 10, 25]
2+
let values = [10, 40, 30, 50, 35, 40, 30]
3+
let C = 150
4+
5+
const Objects = weights.map((w, i) => [w, values[i], 0])
6+
7+
8+
// 寻找价值最高的
9+
function chooseFun1() {
10+
let index = -1;
11+
let maxPrice = 0;
12+
13+
for (let i = 0; i < Objects.length; i++) {
14+
const value = Objects[i][0]
15+
const status = Objects[i][2]
16+
if (status === 0 && value > maxPrice) {
17+
maxPrice = value
18+
index = i
19+
}
20+
}
21+
22+
return index;
23+
}
24+
// 选重量最轻的
25+
function chooseFun2() {
26+
let index = -1;
27+
let minWeight = 10 ** 8;
28+
29+
for (let i = 0; i < Objects.length; i++) {
30+
const weight = Objects[i][0]
31+
const status = Objects[2]
32+
if (status === 0 && weight < minWeight) {
33+
minWeight = weight
34+
index = i
35+
}
36+
}
37+
38+
return index;
39+
}
40+
41+
42+
// 选价值密度高的
43+
function chooseFun3() {
44+
let index = -1;
45+
let maxDensity = 0;
46+
47+
for (let i = 0; i < Objects.length; i++) {
48+
const weight = Objects[i][0]
49+
const status = Objects[2]
50+
const value = Objects[i][0]
51+
const density = value / weight;
52+
53+
if (status === 0 && density > maxDensity) {
54+
55+
maxDensity = density;
56+
index = i;
57+
}
58+
}
59+
return index
60+
}
61+
62+
63+
function greedyAlgo (weights) {
64+
// 开始装入背包,按策略,装到不能装为止
65+
let totalWeight = 0
66+
let _weights = [...weights]
67+
68+
while (totalWeight < C){
69+
// if() {
70+
71+
// }
72+
}
73+
}

‎leetcode/reverseLinkList.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var reverseList = function(head) {
2+
let prev = null;
3+
let curr = head;
4+
while (curr) {
5+
const next = curr.next;
6+
curr.next = prev;
7+
prev = curr;
8+
curr = next;
9+
}
10+
return prev;
11+
};

0 commit comments

Comments
 (0)