Skip to content

Commit 7109b99

Browse files
authored
Merge pull request #443 from kim-young/main
2 parents ea9b6b4 + 1c6cef0 commit 7109b99

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let left = 0, right = 1;
7+
let max = 0;
8+
9+
while(right < prices.length) {
10+
if(prices[right] < prices[left]) {
11+
left = right;
12+
} else {
13+
max = Math.max(max, prices[right] - prices[left]);
14+
}
15+
right++;
16+
}
17+
return max;
18+
};
19+
20+
// time - O(n) iterate through prices once
21+
// space - O(1)

0 commit comments

Comments
 (0)