Skip to content

Commit b4bd5b1

Browse files
committed
121. Best Time to Buy and Sell Stock
1 parent 6eff0a3 commit b4bd5b1

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

โ€Žbest-time-to-buy-and-sell-stock/haung921209.mdโ€Ž

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
- [๋‹ต์•ˆ ์ฝ”๋“œ ์ œ์ถœ๋ฒ•](https://github.com/DaleStudy/leetcode-study/wiki/%EB%8B%B5%EC%95%88-%EC%A0%9C%EC%B6%9C-%EA%B0%80%EC%9D%B4%EB%93%9C)
44

55
# Problem
6-
- ๋ฌธ์ œ ๋งํฌ :
7-
- ๋ฌธ์ œ ์ด๋ฆ„ :
8-
- ๋ฌธ์ œ ๋ฒˆํ˜ธ :
9-
- ๋‚œ์ด๋„ :
6+
- ๋ฌธ์ œ ๋งํฌ : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
7+
- ๋ฌธ์ œ ์ด๋ฆ„ : Best Time to Buy and Sell Stock
8+
- ๋ฌธ์ œ ๋ฒˆํ˜ธ : 121
9+
- ๋‚œ์ด๋„ : easy
1010
- ์นดํ…Œ๊ณ ๋ฆฌ :
1111

1212
# ๋ฌธ์ œ ์„ค๋ช…
@@ -19,8 +19,25 @@
1919

2020
# โœ… ์ฝ”๋“œ (Solution)
2121

22+
```cpp
23+
class Solution {
24+
public:
25+
int maxProfit(vector<int>& prices) {
26+
int localMax = prices[prices.size()-1];
27+
int res = 0;
28+
for(int i=prices.size()-2;i>=0;i--){
29+
localMax = max(localMax, prices[i]);
30+
res = max(res, localMax-prices[i]);
31+
}
32+
33+
return res;
34+
}
35+
};
36+
```
37+
2238
# ๐Ÿ” ์ฝ”๋“œ ์„ค๋ช…
2339

40+
- local max๋ฅผ ์„ค์ •
2441

2542
# ์ตœ์ ํ™” ํฌ์ธํŠธ (Optimality Discussion)
2643
โ€ข ์ตœ์ ํ™”ํ•œ ์ด์œ ์™€ ์›๋ฆฌ

0 commit comments

Comments
ย (0)