Skip to content

Commit

Permalink
Best time to buy and sell stock
Browse files Browse the repository at this point in the history
  • Loading branch information
24rochak committed Jul 26, 2019
1 parent e5413de commit 465edb9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Best Time to Buy and Sell Stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def maxProfit(prices: [int]) -> int:
import sys
profit = 0
minprice = sys.maxsize
for i in range(0, len(prices)):
if prices[i] < minprice:
minprice = prices[i]
elif prices[i] - minprice > profit:
profit = prices[i] - minprice

return profit


l = [3, 2, 6, 5, 0, 3]
ans = maxProfit(l)
print(ans)
14 changes: 14 additions & 0 deletions Maximum subarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def maxSubArray(nums: [int]) -> int:
tot = nums[0]
for i in range(1, len(nums)):
nums[i] = max(nums[i - 1], 0) + nums[i]
if nums[i] > tot:
tot = nums[i]

return tot


l = [-2, 1, -3, 4, -1, 2, 1, -5, 4]

ans = maxSubArray(l)
print(ans)

0 comments on commit 465edb9

Please sign in to comment.