Skip to content

Commit ad99dfd

Browse files
committed
best-time-to-buy-and-sell-stock
1 parent b975872 commit ad99dfd

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Arrays/001-twosum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def twoSum(nums, target):
77
if nums[i] + nums[j] == target:
88
return [i,j]
99
#T:O(N^2)
10-
#S:O(N)
10+
#S:O(1)
1111

1212
#Solution2 hashmap
1313
#use hash map to instantly check for difference value, map will add index of last occurrence of a num, don’t use same element twice
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''Leetcode - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/'''
2+
3+
#Solution1
4+
def maxProfit(prices):
5+
maxp= 0
6+
7+
for i in range(len(prices)-1):
8+
for j in range(i+1,len(prices)):
9+
profit = prices[j]-prices[i]
10+
if profit>maxp:
11+
maxp=profit
12+
return maxp
13+
14+
#T:O(N^2)
15+
#S:O(1)
16+
17+
#Solution2
18+
def maxProfit(prices):
19+
maxp= 0
20+
21+
l = 0
22+
for r in range(1, len(prices)):
23+
if prices[r] < prices[l]:
24+
l = r
25+
maxp = max(maxp, prices[r] - prices[l])
26+
return maxp
27+
28+
# T: O(N)
29+
# S: O(1)

0 commit comments

Comments
 (0)