Skip to content

Commit 91eb360

Browse files
committed
[leet] 주식 (easy - mid)
1 parent 564c3e7 commit 91eb360

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

허현빈/6주차/260205-1.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var maxProfit = function(prices) {
2+
let totalProfit = 0;
3+
for (let i = 0; i < prices.length - 1; i++) {
4+
if (prices[i] < prices[i+1]) {
5+
totalProfit += prices[i+1] - prices[i];
6+
}
7+
}
8+
return totalProfit;
9+
};

허현빈/6주차/260205-2.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
if (prices.length === 0) return 0;
7+
8+
let n = prices.length;
9+
let dp = new Array(n).fill(0);
10+
11+
for (let i = 1; i < n; i++) {
12+
let currentChange = prices[i] - prices[i-1];
13+
dp[i] = Math.max(0, dp[i-1] + currentChange);
14+
;
15+
}
16+
17+
return Math.max(...dp);
18+
};

0 commit comments

Comments
 (0)