Skip to content

Commit a10c99d

Browse files
committed
Add solution #121
1 parent cc31f24 commit a10c99d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
67|[Add Binary](./0067-add-binary.js)|Easy|
3636
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
3737
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
38+
121|[Best Time to Buy and Sell Stock](./0121-best-time-to-buy-and-sell-stock.js)|Easy|
3839
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
3940
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
4041
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 121. Best Time to Buy and Sell Stock
3+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
4+
* Difficulty: Easy
5+
*
6+
* You are given an array prices where prices[i] is the price of a given stock
7+
* on the ith day.
8+
*
9+
* You want to maximize your profit by choosing a single day to buy one stock
10+
* and choosing a different day in the future to sell that stock.
11+
*
12+
* Return the maximum profit you can achieve from this transaction. If you
13+
* cannot achieve any profit, return 0.
14+
*/
15+
16+
/**
17+
* @param {number[]} prices
18+
* @return {number}
19+
*/
20+
var maxProfit = function(prices) {
21+
let max = 0;
22+
23+
for (let i = 0, min = prices[0]; i < prices.length; i++) {
24+
min = Math.min(min, prices[i]);
25+
max = Math.max(max, prices[i] - min);
26+
}
27+
28+
return max;
29+
};

0 commit comments

Comments
 (0)