Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_damages(H):
- '''
- Input: H | list of bricks per house from west to east
- Output: D | list of damage per house from west to east
- '''
- D = [1 for _ in H]
- ##################
- # YOUR CODE HERE #
- ##################
- house_orderer = [[house, index] for index, house in enumerate(H)]
- def merge_sort(A):
- if len(A) <= 1:
- return A # O(1)
- if len(A) > 1:
- mid = len(A) // 2 # O(1)
- # Sort sublists
- final_list = [0]*len(A) # O(n)
- L = merge_sort(A[:mid]) # T(n/2)
- R = merge_sort(A[mid:]) # T(n/2)
- # Merge process starts here
- i, j = len(L)-1, len(R)-1 # O(1)
- end = len(A) - 1 # O(1)
- # Check to see damages for each house, and add values to damage array
- while end >= 0: # O(n)
- if j < 0 or (i >= 0 and L[i][0] > R[j][0]): # O(1) for checking sides
- D[L[i][1]] += len(R[:j+1]) # O(1)
- final_list[end] = L[i] # O(1)
- i -= 1 # O(1)
- else:
- final_list[end] = R[j] # O(1)
- j -= 1 # O(1)
- end -= 1 # O(1)
- return final_list # O(1)
- merge_sort(house_orderer)
- return D
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement