Skip to content

Commit 4356ed4

Browse files
committed
solve problem Best Time To Buy And Sell Stock II
1 parent a9a0997 commit 4356ed4

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ All solutions will be accepted!
8888
|563|[Binary Tree Tilt](https://leetcode-cn.com/problems/binary-tree-tilt/description/)|[java/py/js](./algorithms/BinaryTreeTilt)|Easy|
8989
|696|[Count Binary Substrings](https://leetcode-cn.com/problems/count-binary-substrings/description/)|[java/py/js](./algorithms/CountBinarySubstrings)|Easy|
9090
|121|[Best Time To Buy And Sell Stock](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/)|[java/py/js](./algorithms/BestTimeToBuyAndSellStock)|Easy|
91+
|122|[Best Time To Buy And Sell Stock II](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/)|[java/py/js](./algorithms/BestTimeToBuyAndSellStockII)|Easy|
9192
|599|[Minimum Index Sum Of Two Lists](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/description/)|[java/py/js](./algorithms/MinimumIndexSumOfTwoLists)|Easy|
9293
|551|[Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/description/)|[java/py/js](./algorithms/StudentAttendanceRecordI)|Easy|
9394
|538|[Convert Bst To Greater Tree](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/description/)|[java/py/js](./algorithms/ConvertBstToGreaterTree)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Best Time To Buy And Sell Stock
2+
This problem is easy to solve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
Integer currentStock = null;
4+
int maxProfit = 0;
5+
6+
for (int i = 0; i < prices.length; i++) {
7+
if (currentStock == null && i + 1 < prices.length && prices[i + 1] > prices[i]) {
8+
currentStock = prices[i];
9+
} else if (currentStock != null && ((i + 1 < prices.length && prices[i + 1] < prices[i]) || i + 1 == prices.length)) {
10+
maxProfit += prices[i] - currentStock;
11+
currentStock = null;
12+
}
13+
}
14+
15+
return maxProfit;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let currentStock = null,
7+
maxProfit = 0
8+
9+
for (let i = 0; i < prices.length; i++) {
10+
if (currentStock === null && i + 1 < prices.length && prices[i + 1] > prices[i]) {
11+
currentStock = prices[i]
12+
} else if (currentStock !== null && ((i + 1 < prices.length && prices[i + 1] < prices[i]) || i + 1 === prices.length)) {
13+
maxProfit += prices[i] - currentStock
14+
currentStock = null
15+
}
16+
}
17+
18+
return maxProfit
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
current_stock = None
8+
max_profit = 0
9+
10+
for i in range(0, len(prices)):
11+
# buy
12+
if current_stock == None and i + 1 < len(prices) and prices[i + 1] > prices[i]:
13+
current_stock = prices[i]
14+
# sell
15+
elif current_stock != None and ((i + 1 < len(prices) and prices[i + 1] < prices[i]) or i + 1 == len(prices)):
16+
max_profit += prices[i] - current_stock
17+
current_stock = None
18+
19+
return max_profit

0 commit comments

Comments
 (0)