Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a3f502

Browse files
黯晓hijiangtao
黯晓
authored andcommitted
update: 42 & 123 & 188 & 309
1 parentdcdb766 commit3a3f502

File tree

5 files changed

+128
-0
lines changed
  • src
    • best-time-to-buy-and-sell-stock-iii
    • best-time-to-buy-and-sell-stock-iv
    • best-time-to-buy-and-sell-stock-with-cooldown
    • trapping-rain-water

5 files changed

+128
-0
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2929
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)|[JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
3030
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[JavaScript](./src/search-for-a-range/res.js)|Medium|
3131
|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/description/)|[JavaScript](./src/first-missing-positive/res.js)|Hard|
32+
|42|[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/)|[JavaScript](./src/trapping-rain-water/res.js)|Hard|
3233
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[JavaScript](./src/multiply-strings/res.js)|Medium|
3334
|45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)|[JavaScript](./src/jump-game-ii/res.js)|Hard|
3435
|46|[Permutations](https://leetcode.com/problems/permutations/)|[JavaScript](./src/permutations/res.js)|Medium|
@@ -47,6 +48,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4748
|120|[Triangle](https://leetcode.com/problems/triangle/)|[JavaScript](./src/triangle/res.js)|Medium|
4849
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[JavaScript](./src/best-time-to-buy-and-sell-stock/res.js)|Easy|
4950
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[JavaScript](./src/best-time-to-buy-and-sell-stock-ii/res.js)|Easy|
51+
|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) <sup>*</sup>|[JavaScript](./src/best-time-to-buy-and-sell-stock-iii/res.js)|Hard|
5052
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)|[JavaScript](./src/word-ladder/res.js)|Medium|
5153
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)|[JavaScript](./src/surrounded-regions/res.js)|Medium|
5254
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[JavaScript](./src/clone-graph/res.js)|Medium|
@@ -63,6 +65,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6365
|182|[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/)|[SQL](./src/duplicate-emails/res.txt)|Easy|
6466
|183|[Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/)|[SQL](./src/customers-who-never-order/res.txt)|Easy|
6567
|184|[Department Highest Salary](https://leetcode.com/problems/department-highest-salary/)|[SQL](./src/department-highest-salary/res.txt)|Medium|
68+
|188|[Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)|[JavaScript](./src/best-time-to-buy-and-sell-stock-iv/res.txt)|Hard|
6669
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[JavaScript](./src/rotate-array/res.js)|Easy|
6770
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/)|[JavaScript](./src/reverse-bits/res.js)|Easy|
6871
|196|[Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/)|[SQL](./src/delete-duplicate-emails/res.txt)|Easy|
@@ -78,6 +81,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
7881
|275|[H-Index II](https://leetcode.com/problems/h-index-ii/)|[JavaScript](./src/h-index-ii/res.js)|Medium|
7982
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[JavaScript](./src/bulls-and-cows/res.js)|Medium|
8083
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/)|[JavaScript](./src/range-sum-query-mutable/res.js)|Medium|
84+
|309|[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)|[JavaScript](./src/best-time-to-buy-and-sell-stock-with-cooldown/res.js)|Medium|
8185
|310|[Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/)|[JavaScript](./src/minimum-height-trees/res.js)|Medium|
8286
|342|[Power of Four](https://leetcode.com/problems/power-of-four/)|[JavaScript](./src/power-of-four/res.js)|Easy|
8387
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|[JavaScript](./src/reverse-string/res.js)|Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
3+
* dp[i][1] = max(dp[i-1][1], dp[i-2][0] - prices[i])
4+
* 解释:第 i 天选择 buy 的时候,要从 i-2 的状态转移,而不是 i-1 。
5+
* 查看 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/yi-ge-tong-yong-fang-fa-tuan-mie-6-dao-gu-piao-wen/
6+
*
7+
*/
8+
9+
/**
10+
*@param {number[]} prices
11+
*@return {number}
12+
*/
13+
varmaxProfit=function(prices){
14+
constmax_k=2;
15+
constlen=prices.length;
16+
17+
if(!len)return0;
18+
19+
letdp=[];
20+
for(leti=0;i<len;i++){
21+
dp.push([]);
22+
for(letj=0;j<max_k+1;j++){
23+
dp[i][j]=[0,Number.MIN_SAFE_INTEGER];
24+
}
25+
}
26+
27+
for(leti=0;i<len;i++){
28+
for(letk=max_k;k>=1;k--){
29+
if(i===0){
30+
dp[i][k]=[0,-prices[i]];
31+
}else{
32+
dp[i][k][0]=Math.max(dp[i-1][k][0],dp[i-1][k][1]+prices[i]);
33+
dp[i][k][1]=Math.max(dp[i-1][k][1],dp[i-1][k-1][0]-prices[i]);
34+
}
35+
}
36+
}
37+
38+
returndp[len-1][max_k][0];
39+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
*@param {number[]} prices
3+
*@return {number}
4+
*/
5+
varmaxProfit=function(k,prices){
6+
constmax_k=k;
7+
constlen=prices.length;
8+
9+
if(!len)return0;
10+
if(max_k>len/2){
11+
returnmaxProfit_Infinite(prices);
12+
}
13+
14+
letdp=[];
15+
for(leti=0;i<len;i++){
16+
dp.push([]);
17+
for(letj=0;j<max_k+1;j++){
18+
dp[i][j]=[0,Number.MIN_SAFE_INTEGER];
19+
}
20+
}
21+
22+
for(leti=0;i<len;i++){
23+
for(letk=max_k;k>=1;k--){
24+
if(i===0){
25+
dp[i][k]=[0,-prices[i]];
26+
}else{
27+
dp[i][k][0]=Math.max(dp[i-1][k][0],dp[i-1][k][1]+prices[i]);
28+
dp[i][k][1]=Math.max(dp[i-1][k][1],dp[i-1][k-1][0]-prices[i]);
29+
}
30+
}
31+
}
32+
33+
returndp[len-1][max_k][0];
34+
};
35+
36+
constmaxProfit_Infinite=(prices)=>{
37+
letn=prices.length;
38+
letdp_i_0=0,dp_i_1=Number.MIN_SAFE_INTEGER;
39+
for(leti=0;i<n;i++){
40+
lettemp=dp_i_0;
41+
dp_i_0=Math.max(dp_i_0,dp_i_1+prices[i]);
42+
dp_i_1=Math.max(dp_i_1,temp-prices[i]);
43+
}
44+
returndp_i_0;
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
constmaxProfit=(prices)=>{
2+
letn=prices.length;
3+
letdp_i_0=0,dp_i_1=Number.MIN_SAFE_INTEGER;
4+
letdp_pre_i_0=0;
5+
for(leti=0;i<n;i++){
6+
lettemp=dp_i_0;
7+
dp_i_0=Math.max(dp_i_0,dp_i_1+prices[i]);
8+
dp_i_1=Math.max(dp_i_1,dp_pre_i_0-prices[i]);
9+
dp_pre_i_0=temp;
10+
}
11+
returndp_i_0;
12+
}

‎src/trapping-rain-water/res.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*@param {number[]} height
3+
*@return {number}
4+
*/
5+
consttrap=function(height){
6+
constlen=height.length;
7+
if(len<3)return0;
8+
9+
constmax_left=[height[0]];
10+
letmax_right=[height[len-1]];
11+
letres=0;
12+
13+
for(leti=1;i<len;i++){
14+
max_left[i]=Math.max(height[i],max_left[i-1]);
15+
}
16+
for(leti=len-2;i>=0;i--){
17+
max_right=[Math.max(height[i],max_right[0])].concat(max_right);
18+
}
19+
20+
for(leti=1;i<len-1;i++){
21+
constmin=Math.min(max_left[i],max_right[i]);
22+
if(min>height[i]){
23+
res+=(min-height[i]);
24+
}
25+
}
26+
27+
returnres;
28+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp