Skip to content

Commit 8164601

Browse files
committed
update(array) : buy sell stock'
1 parent 5bfd83f commit 8164601

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
######################################################################
2+
# LeetCode Problem Number : 121
3+
# Difficulty Level : Easy
4+
# URL : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
5+
######################################################################
6+
from typing import List
7+
8+
9+
class BuySellStock:
10+
def calculate(self, prices: List[int]) -> int:
11+
max_profit, min_price_so_far = 0, prices[0]
12+
13+
for i in range(1, len(prices)):
14+
max_profit_sell_today = prices[i] - min_price_so_far
15+
max_profit = max(max_profit, max_profit_sell_today)
16+
min_price_so_far = min(min_price_so_far, prices[i])
17+
18+
return max_profit
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
###########################################################################
2+
# LeetCode Problem Number : 123
3+
# Difficulty Level : Hard
4+
# URL : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
5+
##########################################################################
6+
from typing import List
7+
8+
9+
class BuySellStockTwice:
10+
def calculate(self, prices: List[int]) -> int:
11+
max_profit, min_price_so_far = 0, prices[0]
12+
first_buy_sell = [0] * len(prices)
13+
14+
for i in range(1, len(prices)):
15+
max_profit_sell_today = prices[i] - min_price_so_far
16+
max_profit = max(max_profit, max_profit_sell_today)
17+
min_price_so_far = min(min_price_so_far, prices[i])
18+
first_buy_sell[i] = max_profit
19+
20+
max_sell_so_far = float("-inf")
21+
for i, price in reversed(list(enumerate(prices[1:], 1))):
22+
max_sell_so_far = max(max_sell_so_far, price)
23+
max_profit = max(
24+
max_profit, max_sell_so_far - price + first_buy_sell[i - 1]
25+
)
26+
27+
return max_profit
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from simple_array.e_buy_sell_stock_once import BuySellStock
2+
3+
4+
class TestBuySellStock:
5+
def test_max_profit(self):
6+
bs = BuySellStock()
7+
8+
input = [310, 315, 275, 295, 260, 270, 290, 230, 255, 250]
9+
result = bs.calculate(input)
10+
assert result == 30
11+
12+
def test_decrease_price(self):
13+
bs = BuySellStock()
14+
15+
input = [310, 300, 290, 280, 270]
16+
result = bs.calculate(input)
17+
assert result == 0
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from simple_array.h_buy_sell_stock_twice import BuySellStockTwice
2+
3+
4+
class TestBuySellStockTwice:
5+
def test_max_profit(self):
6+
bs = BuySellStockTwice()
7+
8+
input = [12, 11, 13, 9, 12, 8, 14, 13, 15]
9+
result = bs.calculate(input)
10+
assert result == 10
11+
12+
input = [1, 2, 3, 4, 5]
13+
result = bs.calculate(input)
14+
assert result == 4
15+
16+
def test_decrease_price(self):
17+
bs = BuySellStockTwice()
18+
19+
input = [7.6, 4, 3, 1]
20+
result = bs.calculate(input)
21+
assert result == 0

0 commit comments

Comments
 (0)