Skip to content

Commit 6ff4780

Browse files
committed
Add solution #122
1 parent 115ecb3 commit 6ff4780

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
119|[Pascal's Triangle II](./0119-pascals-triangle-ii.js)|Easy|
128128
120|[Triangle](./0120-triangle.js)|Medium|
129129
121|[Best Time to Buy and Sell Stock](./0121-best-time-to-buy-and-sell-stock.js)|Easy|
130+
122|[Best Time to Buy and Sell Stock II](./0122-best-time-to-buy-and-sell-stock-ii.js)|Medium|
130131
124|[Binary Tree Maximum Path Sum](./0124-binary-tree-maximum-path-sum.js)|Hard|
131132
125|[Valid Palindrome](./0125-valid-palindrome.js)|Easy|
132133
128|[Longest Consecutive Sequence](./0128-longest-consecutive-sequence.js)|Medium|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 122. Best Time to Buy and Sell Stock II
3+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array prices where prices[i] is the price of a given
7+
* stock on the ith day.
8+
*
9+
* On each day, you may decide to buy and/or sell the stock. You can only hold at
10+
* most one share of the stock at any time. However, you can buy it then immediately
11+
* sell it on the same day.
12+
*
13+
* Find and return the maximum profit you can achieve.
14+
*/
15+
16+
/**
17+
* @param {number[]} prices
18+
* @return {number}
19+
*/
20+
var maxProfit = function(prices) {
21+
return prices.reduce((result, price, i) => {
22+
return prices[i - 1] < price ? result + (price - prices[i - 1]) : result;
23+
}, 0);
24+
};

0 commit comments

Comments
 (0)