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 980e610 commit df66dc6Copy full SHA for df66dc6
1 file changed
best-time-to-buy-and-sell-stock/soobing2.ts
@@ -0,0 +1,21 @@
1
+/**
2
+ * 문제 유형
3
+ * - Array
4
+ *
5
+ * 문제 설명
6
+ * - 주식을 가장 싸게 사서 비싸게 팔수 있는 경우 찾기
7
8
+ * 아이디어
9
+ * 1) 최소값을 찾고 그 이후의 값 중 최대값을 찾는다.
10
11
+ */
12
+function maxProfit(prices: number[]): number {
13
+ let min = prices[0];
14
+ let maxProfit = 0;
15
+
16
+ for (let i = 1; i < prices.length; i++) {
17
+ min = Math.min(min, prices[i]);
18
+ maxProfit = Math.max(prices[i] - min, maxProfit);
19
+ }
20
+ return maxProfit;
21
+}
0 commit comments