Question: https://codility.com/demo/take-sample-test/chi2012/
Question Name: chi2012 or Cannonballs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | def solution(A, B): to_fall = [-1] * (max(B) + 1) # Store where the ball should # fall. to_fall[i] = j means, # the cannonball, which is shot # from heigth of i, will fall # at position j. # Scan and find the initial falling position for cannonballs # from each different shooting height. current_pos = 0 # Index for the landscape array A. # For all shooting heigthes <= A[0], the cannonballs do not # change the landscape. Keep the content being -1 and no need # to try them. for height in xrange( A[0]+1, max(B)+1 ): while current_pos < len(A) and height > A[current_pos]: current_pos += 1 if current_pos == len(A): # No position could prevent the cannonball from flying # beyond the bound. These cannonballs do not change # the landscape. Keep the content being -1. break else: # The cannonball meets with a high enought position, # and falls at the previous position. to_fall[height] = current_pos - 1 for cannonball in B: if to_fall[cannonball] == -1: # This cannonball would not change the landscape. continue else: fall_pos = to_fall[cannonball] A[ fall_pos ] += 1 # increases the ground by 1 # For shooting height > A[fall_pos], the increase # is NOT enought to block/change their flying # path. # For shooting height < A[fall_pos] <= old A[fall_pos] # they cannot fly beyond this position both # before and after increase. NO change is # made for them. # ONLY for shooting height = A[fall_pos], its # flying path might be changed due to the # increase. to_fall[ A[ fall_pos ] ] = min( to_fall[ A[ fall_pos ] ], fall_pos-1) return A |
Hello Sheng
Do you know a solution for MinMaxDivision Lesson 12 Codility ?
Thanks
Sorry, I just got a 50/100 solution. And I am still struggling with it.
Hi, do you have a copy of task to this solution, because the page is no longer available?
Codility changed the URL for this challenge. I just updated it. Please refer to the post. Thanks!