File tree 2 files changed +25
-0
lines changed
2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change 127
127
119|[ Pascal's Triangle II] ( ./0119-pascals-triangle-ii.js ) |Easy|
128
128
120|[ Triangle] ( ./0120-triangle.js ) |Medium|
129
129
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|
130
131
124|[ Binary Tree Maximum Path Sum] ( ./0124-binary-tree-maximum-path-sum.js ) |Hard|
131
132
125|[ Valid Palindrome] ( ./0125-valid-palindrome.js ) |Easy|
132
133
128|[ Longest Consecutive Sequence] ( ./0128-longest-consecutive-sequence.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments