Skip to content

Commit 9c8ca7e

Browse files
committed
#221 solution
1 parent a4d8f65 commit 9c8ca7e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
풀이 :
3+
현재의 price에 도달하기 전 가장 작은 price를 min_cur로 업데이트
4+
price - min_cur가 저장되있는 max_profit보다 크면 값을 업데이트
5+
6+
prices의 개수 N
7+
8+
TC : O(N)
9+
10+
SC : O(1)
11+
*/
12+
13+
14+
#include <vector>
15+
using namespace std;
16+
17+
class Solution {
18+
public:
19+
int maxProfit(vector<int>& prices) {
20+
int min_cur = prices[0];
21+
int max_profit = 0;
22+
23+
for (int& price : prices)
24+
{
25+
if (price < min_cur)
26+
{
27+
min_cur = price;
28+
continue ;
29+
}
30+
31+
int profit = price - min_cur;
32+
if (profit > max_profit)
33+
max_profit = profit;
34+
}
35+
return max_profit;
36+
}
37+
};

0 commit comments

Comments
 (0)