We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 6411e48 + c973a58 commit f1b408dCopy full SHA for f1b408d
βbest-time-to-buy-and-sell-stock/seona926.js
@@ -0,0 +1,23 @@
1
+function maxProfit(prices) {
2
+ let minPrice = Infinity; // μ΅μ κ°κ²©μ 무νλλ‘ μ΄κΈ°ν
3
+ let maxProfit = 0; // μ΅λ μ΄μ΅μ 0μΌλ‘ μμ
4
+
5
+ for (let i = 0; i < prices.length; i++) {
6
+ if (prices[i] < minPrice) {
7
+ // λ μμ κ°κ²©μ λ°κ²¬νλ©΄ κ·Έ κ°κ²©μ μ΅μ κ°κ²©μΌλ‘ μ λ°μ΄νΈ
8
+ minPrice = prices[i];
9
+ } else {
10
+ // νμ¬ κ°κ²©μμ μ΅μ κ°κ²©μ λΊ κ°μ κ³μ°νμ¬ μ΅λ μ΄μ΅μ μ λ°μ΄νΈ
11
+ maxProfit = Math.max(maxProfit, prices[i] - minPrice);
12
+ }
13
14
15
+ return maxProfit;
16
+}
17
18
+/*
19
+ 1. μκ°λ³΅μ‘λ: O(n)
20
+ - λ°°μ΄μ ν λ² μν
21
+ 2. 곡κ°λ³΅μ‘λ: O(1)
22
+ - μμ κ³΅κ° μ¬μ©
23
+*/
0 commit comments