Question Name: chi2012 or Cannonballs
Solution to chi2012 (Cannonballs) by codility
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, the cannonballs do not
# change the landscape. Keep the content being -1 and no need
# to try them.
for height in xrange( A+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.
# 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.
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
# 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
to_fall[ A[ fall_pos ] ] = min(
to_fall[ A[ fall_pos ] ], fall_pos-1)
Do you know a solution for MinMaxDivision Lesson 12 Codility ?
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!