We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c48048d commit 66ceabdCopy full SHA for 66ceabd
โbest-time-to-buy-and-sell-stock/donghyeon95.javaโ
@@ -0,0 +1,23 @@
1
+class Solution {
2
+ public int maxProfit(int[] prices) {
3
+ int result = 0;
4
+ int maxProfit = 0;
5
+ int buyStock = prices[0];
6
+
7
+ // ํ๋ฒ ๋๋ฉด์ ๋๋ณด๋ค ์์ ๊ฒ์ด ๋์ฌ ๋๊น์ง ์ด์ต์ ๋ณธ๋ค
8
9
10
+ for (int price: prices) {
11
+ // ๋๋ณด๋ค ์์ ๊ฒ ๋์ค๋ฉด MAX ์ด์ต์ ๊ฐฑ์ ํ๊ณ ๊ฑฐ๊ธฐ๋ถ์ ๋ค์ ์์ํ๋ค.
12
+ if (price < buyStock) {
13
+ result = Math.max(result, maxProfit);
14
+ maxProfit = 0;
15
+ buyStock = price;
16
+ } else {
17
+ maxProfit = Math.max(price - buyStock, maxProfit);
18
+ }
19
20
21
+ return Math.max(result, maxProfit);
22
23
+}
0 commit comments