Skip to content

Commit bb0875d

Browse files
Add No_121: Best Time to Buy and Sell Stock
1 parent b97bedc commit bb0875d

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
3+
Description:
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
8+
9+
Note that you cannot sell a stock before you buy one.
10+
11+
Example 1:
12+
13+
Input: [7,1,5,3,6,4]
14+
Output: 5
15+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
16+
Not 7-1 = 6, as selling price needs to be larger than buying price.
17+
Example 2:
18+
19+
Input: [7,6,4,3,1]
20+
Output: 0
21+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
22+
23+
'''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'''
2+
3+
Description:
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
8+
9+
Note that you cannot sell a stock before you buy one.
10+
11+
Example 1:
12+
13+
Input: [7,1,5,3,6,4]
14+
Output: 5
15+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
16+
Not 7-1 = 6, as selling price needs to be larger than buying price.
17+
Example 2:
18+
19+
Input: [7,6,4,3,1]
20+
Output: 0
21+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
22+
23+
'''
24+
25+
26+
from typing import List
27+
class Solution:
28+
def maxProfit(self, prices: List[int]) -> int:
29+
30+
if len(prices) == 0:
31+
# corner case
32+
return 0
33+
34+
min_price, min_index = 2**31, 0
35+
max_price, max_index = 0, 0
36+
37+
max_profit = 0
38+
39+
for i, p in enumerate(prices):
40+
41+
if p <= min_price:
42+
# update valley
43+
min_price = p
44+
min_index = i
45+
46+
if p >= min_price:
47+
# update peak
48+
max_price = p
49+
max_index = i
50+
51+
52+
if max_index > min_index and (max_price - min_price) > max_profit :
53+
# update max_profit
54+
max_profit = max_price - min_price
55+
56+
57+
return max_profit
58+
59+
60+
61+
# n : the length of input array, prices.
62+
63+
## Time Complexity: O( n )
64+
#
65+
# The major overhead in time is the for loop iterating on (i, p), which is of O( n ).
66+
67+
## Space Complexity: O( 1 )
68+
#
69+
# The major overhead in space is the variables for price computation, which is of O( 1 ).
70+
71+
72+
73+
def test_bench():
74+
75+
test_data = [
76+
[7,1,5,3,6,4],
77+
[7,6,4,3,1],
78+
[2,4,1],
79+
[]
80+
]
81+
82+
# expected output:
83+
'''
84+
5
85+
0
86+
2
87+
0
88+
'''
89+
90+
for stock_price in test_data:
91+
92+
print( Solution().maxProfit(stock_price) )
93+
94+
return
95+
96+
97+
98+
if __name__ == '__main__':
99+
100+
test_bench()

0 commit comments

Comments
 (0)