Skip to content

Commit bc0d3dd

Browse files
committed
feat: best time to buy and sell
1 parent 15bcd5a commit bc0d3dd

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Solutions of LeetCode Blind 75 Problems in JavaScript
44

55
:goal_net: [Curated List of Top 75 LeetCode Questions](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU)
66

7-
| Problem | Difficulty | Tags | LeetCode |
8-
| ----------------------- | ------------------------------------------------------ | --------------------- | ------------------------------------------------ |
9-
| [Two Sum](./two-sum.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Hash Table` | [:link:](https://leetcode.com/problems/two-sum/) |
7+
| Problem | Difficulty | Tags | LeetCode |
8+
| ----------------------------------------------------------------------- | ------------------------------------------------------ | ------------------------------ | ------------------------------------------------------------------------ |
9+
| [Two Sum](./two-sum.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Hash Table` | [:link:](https://leetcode.com/problems/two-sum/) |
10+
| [Best Time to Buy and Sell Stock](./best-time-to-buy-and-sell-stock.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming` | [:link:](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |

best-time-to-buy-and-sell-stock.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const maxProfit = (prices) => {
2+
let buy = 0,
3+
sell = 1,
4+
max = (profit = 0);
5+
6+
while (sell < prices.length) {
7+
if (prices[buy] < prices[sell]) {
8+
profit = prices[sell] - prices[buy];
9+
max = Math.max(max, profit);
10+
} else {
11+
buy = sell;
12+
}
13+
14+
sell++;
15+
}
16+
17+
return max;
18+
};

0 commit comments

Comments
 (0)