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.
2 parents 0add5a1 + 3f7066f commit 1aabf5bCopy full SHA for 1aabf5b
โbest-time-to-buy-and-sell-stock/dohyeon2.javaโ
@@ -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