Skip to content

Commit ddc9723

Browse files
author
yeyounging
committed
Sync from source: 6fe15420943dfc0d9a05986a92950bd631d143bc
1 parent 77d7818 commit ddc9723

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let min = Infinity,max=0, profit =0;
7+
8+
for(let i =0;i<prices.length;i++){
9+
// 새로운 min 찾으면 탐색 시작
10+
if(min > prices[i]){
11+
min = prices[i];
12+
max = 0;
13+
continue;
14+
}
15+
16+
// max 업데이트
17+
if(max < prices[i]){
18+
max = prices[i];
19+
profit = Math.max(profit, max - min);
20+
}
21+
}
22+
return profit;
23+
24+
};

0 commit comments

Comments
 (0)