Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- def trade_strength(profits):
- n = len(profits)
- #OPT(j) =
- #max[1<=i<=j](Profit from day i to j inclusive) IF j is in the max subsequence
- #OPT(j-1) IF j not in the max subsequence
- #sumP[idx] = Sum of profits from day 1 to day idx (inclusive)
- #Sum of profits from day a to day b (inclusive) = sumP[b] - sumP[a-1]
- sumP = [0]
- running_sum = 0
- for profit in profits:
- running_sum += profit
- sumP.append(running_sum)
- #M[idx] = Optimum at day idx
- M = [None,profits[0]]
- #From day 2 to last (inclusive)
- for day_end in range(2,n+1):
- #Start max at previous optimum
- max_for_day_end = M[day_end-1]
- #From day 1 to day_end (inclusive)
- for day_start in range(1,day_end+1):
- profit_sum = sumP[day_end] - sumP[day_start-1]
- if profit_sum > max_for_day_end:
- max_for_day_end = profit_sum
- M.append(max_for_day_end)
- return M[-1]
- #P[idx] = Pay at day (idx+1)
- P = [-1,3,3,3,-1,-1,-1,2]
- print(trade_strength(P))
Add Comment
Please, Sign In to add comment