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

Commit1c1e217

Browse files
committed
continue best time to sell/buy stocks 123, 309
1 parenta9f4d57 commit1c1e217

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* key: use left to track the max profit on day i (or before day i)?
3+
* use right to track the max profit after transaction day i, ( track from the end
4+
* to the beginning. )
5+
* e.g. Prices: 1 4 5 7 6 3 2 9
6+
* left = [0, 3, 4, 6, 6, 6, 6, 8]
7+
* right= [8, 7, 7, 7, 7, 7, 7, 0]
8+
* max is 6 + 7 = 13
9+
*
10+
*@param {number[]} prices
11+
*@return {number}
12+
*/
13+
varmaxProfit=function(prices){
14+
if(prices.length<2)return0;
15+
varleft=[0];
16+
varright=[];
17+
varmaxLeftProfit=0;
18+
varminPrice=prices[0];
19+
for(vari=1;i<prices.length;i++){
20+
minPrice=Math.min(prices[i],minPrice);
21+
maxLeftProfit=Math.max(prices[i]-minPrice,maxLeftProfit);
22+
left.push(maxLeftProfit);
23+
}
24+
25+
varmaxRightProfit=0;
26+
maxPrice=prices[prices.length-1];
27+
right[prices.length-1]=0;
28+
for(vari=prices.length-2;i>=0;i--){
29+
maxPrice=Math.max(prices[i],maxPrice);
30+
maxRightProfit=Math.max(maxPrice-prices[i],maxRightProfit);
31+
right[i]=maxRightProfit;
32+
}
33+
34+
varfinalProfit=[];
35+
for(vari=0;i<left.length;i++){
36+
finalProfit.push(left[i]+right[i]);
37+
}
38+
returnMath.max.apply(null,finalProfit);
39+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*@param {number[]} prices
3+
*@return {number}
4+
*/
5+
varmaxProfit=function(prices){
6+
if(prices.length<=1)return0;
7+
varsells=[0];
8+
varbuys=[-prices[0]];
9+
10+
for(vari=1;i<prices.length;i++){
11+
vardelta=prices[i]-prices[i-1];
12+
sells[i]=Math.max(buys[i-1]+prices[i],sells[i-1]+delta);
13+
if(i>1){
14+
buys[i]=Math.max(sells[i-2]-prices[i],buys[i-1]-delta);
15+
}else{
16+
buys[i]=Math.max(-prices[i],buys[i-1]-delta);
17+
}
18+
}
19+
20+
returnMath.max.apply(null,sells);
21+
};

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
*[260. Single Number III](https://leetcode.com/problems/single-number-iii/) -[Solution](./Medium/260-singleNumberIII.js)
127127
*[268. Missing Number](https://leetcode.com/problems/missing-number/) -[Solution](./Medium/268-missingNumber.js)
128128
*[300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) -[Solution](./Medium/300-longestIncreasingSubsequence.js)
129+
*[309. Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) -[Solution](./Medium/309-bestTimeStockCooldown.js)
129130
*[318. Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) -[Solution](./Medium/318-maximumProductWordLengths.js)
130131
*[319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) -[Solution](./Medium/319-bulbSwitcher.js)
131132
*[337. House Robber III](https://leetcode.com/problems/house-robber-iii/) -[Solution](./Medium/337-houseRobberIII.js)
@@ -135,3 +136,4 @@
135136
*[25. Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) -[Solution](./Hard/25-reverseNodesInKGroup.js)
136137
*[32. Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) -[Solution](./Hard/32-longestValidParentheses.js)
137138
*[33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) -[Solution](./Hard/33-searchRotatedSortedArray.js)
139+
*[123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) -[Solution](./Hard/123-bestTimeBuySellStockIII.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp