Skip to content

Commit 1aabf5b

Browse files
authored
Merge pull request DaleStudy#2509 from dohyeon2/week5
2 parents 0add5a1 + 3f7066f commit 1aabf5b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)