본문 바로가기
Problem Solving/Codility

[Python] 5.3. MinAvgTwoSlice

by 부르르 2021. 3. 18.

https://app.codility.com/programmers/lessons/5-prefix_sums/

 

5. Prefix Sums lesson - Learn to Code - Codility

Compute number of integers divisible by k in range [a..b].

app.codility.com


1차 시도

Task Score 50%

Correctness 100%

Performance 0%

시간복잡도 O(N ** 3)

def solution(A):

    N = len(A)
    prefix_sum = [A.copy() for _ in range(N)]
    min_val = 10 ** 10
    answer = N

    for p in range(N):
        for q in range(p+1,N):
            if p < q:
                prefix_sum[p][q] = prefix_sum[p][q-1] + A[q]
                avg = prefix_sum[p][q]/(q-p+1)
                if avg < min_val:
                    min_val = avg
                    answer = p
                elif avg == min_val:
                    answer = min(answer, p)

    return answer
728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] 6.1. Distinct  (0) 2021.03.25
[Python] 5.4. PassingCars  (0) 2021.03.24
[Python] 5.2. GenomicRangeQuery  (0) 2021.03.16
[Python] 5.1. CountDiv  (0) 2021.03.15
[Python] 4.4. PermCheck  (0) 2021.03.15

댓글