Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. def get_damages(H):
  2. '''
  3. Input: H | list of bricks per house from west to east
  4. Output: D | list of damage per house from west to east
  5. '''
  6. D = [1 for _ in H]
  7. ##################
  8. # YOUR CODE HERE #
  9. ##################
  10. house_orderer = [[house, index] for index, house in enumerate(H)]
  11.  
  12. def merge_sort(A):
  13. if len(A) <= 1:
  14. return A # O(1)
  15. if len(A) > 1:
  16. mid = len(A) // 2 # O(1)
  17.  
  18. # Sort sublists
  19. final_list = [0]*len(A) # O(n)
  20. L = merge_sort(A[:mid]) # T(n/2)
  21. R = merge_sort(A[mid:]) # T(n/2)
  22.  
  23. # Merge process starts here
  24. i, j = len(L)-1, len(R)-1 # O(1)
  25. end = len(A) - 1 # O(1)
  26. # Check to see damages for each house, and add values to damage array
  27. while end >= 0: # O(n)
  28. if j < 0 or (i >= 0 and L[i][0] > R[j][0]): # O(1) for checking sides
  29. D[L[i][1]] += len(R[:j+1]) # O(1)
  30. final_list[end] = L[i] # O(1)
  31. i -= 1 # O(1)
  32. else:
  33. final_list[end] = R[j] # O(1)
  34. j -= 1 # O(1)
  35. end -= 1 # O(1)
  36. return final_list # O(1)
  37.  
  38. merge_sort(house_orderer)
  39.  
  40. return D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement