Guest User

Untitled

a guest
Nov 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. def trade_strength(profits):
  4. n = len(profits)
  5.  
  6. #OPT(j) =
  7. #max[1<=i<=j](Profit from day i to j inclusive) IF j is in the max subsequence
  8. #OPT(j-1) IF j not in the max subsequence
  9.  
  10. #sumP[idx] = Sum of profits from day 1 to day idx (inclusive)
  11. #Sum of profits from day a to day b (inclusive) = sumP[b] - sumP[a-1]
  12. sumP = [0]
  13.  
  14. running_sum = 0
  15. for profit in profits:
  16. running_sum += profit
  17. sumP.append(running_sum)
  18.  
  19. #M[idx] = Optimum at day idx
  20. M = [None,profits[0]]
  21.  
  22. #From day 2 to last (inclusive)
  23. for day_end in range(2,n+1):
  24. #Start max at previous optimum
  25. max_for_day_end = M[day_end-1]
  26. #From day 1 to day_end (inclusive)
  27. for day_start in range(1,day_end+1):
  28. profit_sum = sumP[day_end] - sumP[day_start-1]
  29. if profit_sum > max_for_day_end:
  30. max_for_day_end = profit_sum
  31. M.append(max_for_day_end)
  32.  
  33. return M[-1]
  34.  
  35.  
  36. #P[idx] = Pay at day (idx+1)
  37. P = [-1,3,3,3,-1,-1,-1,2]
  38.  
  39. print(trade_strength(P))
Add Comment
Please, Sign In to add comment