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 3629dbe commit b8602b8Copy full SHA for b8602b8
1 file changed
best-time-to-buy-and-sell-stock/juhui-jeong.java
@@ -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
22
23
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
33
34
0 commit comments