We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 0add5a1 + 3f7066f commit 1aabf5bCopy full SHA for 1aabf5b
1 file changed
best-time-to-buy-and-sell-stock/dohyeon2.java
@@ -0,0 +1,24 @@
1
+class Solution {
2
+ // TC : O(n)
3
+ // SC : O(1)
4
+ public int maxProfit(int[] prices) {
5
+ int maximumProfit = 0;
6
+ int cursor = 0;
7
+
8
+ // 전체를 순회하면서 이익을 검증
9
+ for(int i = 0; i < prices.length; i++){
10
+ int profit = prices[i] - prices[cursor];
11
12
+ // 만약 현재 값이 커서의 값보다 더 작다면 현재 값부터 비교하기 시작
13
+ if(profit < 0){
14
+ cursor = i;
15
+ continue;
16
+ }
17
18
+ // 이전에 커서로 추정한 값이 높더라도 Math.max로 보존됨
19
+ maximumProfit = Math.max(profit, maximumProfit);
20
21
22
+ return maximumProfit;
23
24
+}
0 commit comments