Skip to content

Commit dceb7c6

Browse files
committedJan 4, 2025
best time to buy and sell stock solved
·
v4.15.0v3.5.0
1 parent 05a50ec commit dceb7c6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
 
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
시간복잡도: O(n)
4+
공간복잡도: O(1)
5+
*/
6+
public int maxProfit(int[] prices) {
7+
int buyPrice = prices[0];
8+
int maxProfit = 0;
9+
10+
for(int i = 0; i < prices.length; i++) {
11+
buyPrice = Math.min(buyPrice, prices[i]);
12+
maxProfit = Math.max(maxProfit, prices[i] - buyPrice);
13+
}
14+
15+
return maxProfit;
16+
}
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.