Skip to content

Commit b8602b8

Browse files
committed
WEEK 05 Solutions
1 parent 3629dbe commit b8602b8

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
시간복잡도: O(n²)
3+
공간복잡도: O(1)
4+
class Solution {
5+
public int maxProfit(int[] prices) {
6+
int maxStock = 0;
7+
for(int i = 0; i < prices.length; i++) {
8+
for (int j = 0; j < prices.length; j++) {
9+
if (i <= j) break;
10+
if (prices[i] - prices[j] > maxStock) {
11+
maxStock = prices[i] - prices[j];
12+
}
13+
}
14+
}
15+
return maxStock;
16+
}
17+
}
18+
*/
19+
// 시간복잡도: O(n)
20+
// 공간복잡도: O(1)
21+
class Solution {
22+
public int maxProfit(int[] prices) {
23+
int maxStock = 0;
24+
int minPrice = prices[0];
25+
for (int price : prices) {
26+
if (price < minPrice) {
27+
minPrice = price;
28+
} else {
29+
maxStock = Math.max(price - minPrice, maxStock);
30+
}
31+
}
32+
return maxStock;
33+
}
34+
}

0 commit comments

Comments
 (0)