알고리즘, 자료구조/Codility

[Python] Codility Lesson1 - BinaryGap

jaee 2022. 9. 19. 01:20

 

https://www.python.org/community/logos

 

문제: https://app.codility.com/programmers/lessons/1-iterations/binary_gap/

 

풀이

def solution(N):
    result = 0
    binData = format(N, 'b')
    gapList = []

    for i, v in enumerate(binData):
        if v == '1':
            gapList.append(i)

    if len(gapList) < 2:
        return result

    gapList.reverse()
    for i in range(len(gapList) - 1):
        tmp = gapList[i] - gapList[i + 1] - 1
        
        if tmp > result:
            result = tmp

    return result

 

 

Javascript 풀이: https://jaejade.tistory.com/123?category=787340

 

[Javascript] Codility Lesson1 - BinaryGap

문제: https://app.codility.com/programmers/lessons/1-iterations/binary_gap/ 문제 요약 양의 정수인 N을 이진수로 나타냈을 때, 양 끝이 1로 이루어진 연속되는 0을 BinaryGap이라 한다. 가장 긴 BinaryGap의..

jaejade.tistory.com