[Codility] NumberOfDiscIntersections

hyeon·2021년 2월 27일
0

Codility

목록 보기
17/18

import heapq
def solution(A):
    circle = []
    for i, a in enumerate(A):
        circle += [(i-a, False), (i+a, True)] # start, end
    
    heapq.heapify(circle)
    active_circle = 0
    intersection = 0

    while circle :
        point, isEnd = heapq.heappop(circle)
        if not isEnd:
            intersection +=active_circle
            active_circle +=1
        else :
            active_circle -= 1
        if intersection > 10E6 :
            return -1
    
    return intersection
profile
바스락바스락

0개의 댓글