File tree Expand file tree Collapse file tree 1 file changed +21
-4
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change 3
3
- [ ๋ต์ ์ฝ๋ ์ ์ถ๋ฒ] ( 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 )
4
4
5
5
# 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
10
10
- ์นดํ
๊ณ ๋ฆฌ :
11
11
12
12
# ๋ฌธ์ ์ค๋ช
19
19
20
20
# โ
์ฝ๋ (Solution)
21
21
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
+
22
38
# ๐ ์ฝ๋ ์ค๋ช
23
39
40
+ - local max๋ฅผ ์ค์
24
41
25
42
# ์ต์ ํ ํฌ์ธํธ (Optimality Discussion)
26
43
โข ์ต์ ํํ ์ด์ ์ ์๋ฆฌ
You canโt perform that action at this time.
0 commit comments