Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #!/usr/local/bin/python3.4
  2.  
  3. def solution(N, A):
  4. # write your code in Python 2.7
  5. last_max_operation = -1
  6. for i in range(len(A) - 1, -1, -1):
  7. if A[i] == N + 1:
  8. last_max_operation = i
  9. break
  10.  
  11. counters = [0] * N
  12.  
  13. if last_max_operation == -1:
  14. for a in A:
  15. counters[a - 1] += 1
  16. return counters
  17.  
  18. max_val = 0
  19. base = 0
  20.  
  21. for i in range(last_max_operation):
  22.  
  23. if A[i] == N + 1:
  24. base = max_val
  25. else:
  26. diff = max(base - counters[A[i] - 1], 0)
  27. counters[A[i] - 1] += (1 + diff)
  28. if counters[A[i] - 1] > max_val:
  29. max_val = counters[A[i] - 1]
  30.  
  31.  
  32. final_counters = [max_val] * N
  33. if last_max_operation < len(A) - 1:
  34. for a in A[last_max_operation + 1:]:
  35. final_counters[a - 1] += 1
  36. return final_counters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement