We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a4d8f65 commit 9c8ca7eCopy full SHA for 9c8ca7e
best-time-to-buy-and-sell-stock/sungjinwi.cpp
@@ -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