Skip to content

Commit d69facb

Browse files
committed
add best-time-to-buy-and-sell-stock
1 parent cacd05e commit d69facb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
3+
최대이익 = 최고금액 - 최소금액
4+
*/
5+
class Solution {
6+
public int maxProfit(int[] prices) {
7+
int minPrice = Integer.MAX_VALUE;
8+
int maximumProfit = 0;
9+
10+
for(int price : prices) {
11+
if(price < minPrice) {
12+
minPrice = price;
13+
}
14+
int currentProfit = price - minPrice;
15+
if(currentProfit > maximumProfit) {
16+
maximumProfit = currentProfit;
17+
}
18+
}
19+
return maximumProfit;
20+
}
21+
}
22+

0 commit comments

Comments
 (0)