Skip to content

Commit eaeb2f9

Browse files
committed
best time to buy and sell stock solution
1 parent cd835e1 commit eaeb2f9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- ์ž‘์€์ˆ˜๋ฅผ ๋‘๊ณ , ํฐ์ˆ˜์— ๋บ€ ๊ฐ’์„ ๊ตฌํ•˜์ž.
4+
time: O(N), space: O(1)
5+
6+
[ํšŒ๊ณ ]
7+
์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋‚œ์ด๋„๊ฐ€ easy์ธ ๋•๋ถ„์— ๋ฌด๋ฆฌ์—†์ด ํ’€์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
8+
*/
9+
class Solution {
10+
public int maxProfit(int[] prices) {
11+
int min = prices[0];
12+
int max = 0;
13+
for (int i = 1; i < prices.length; i++) {
14+
if (min > prices[i]) {
15+
min = prices[i];
16+
continue;
17+
}
18+
max = Math.max(max, prices[i] - min);
19+
}
20+
return max;
21+
}
22+
}
23+

0 commit comments

Comments
ย (0)