본문 바로가기

카테고리 없음

코딜리티 lesson4 FrogRiverOne, PermCheck, MaxCounters, MissingInteger

FrogRiverOne

[최초풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(X, A):
    if A.count(X) == 0:
        return -1
    else:
        return A.index(X)

[다른풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(X, A):
    setA = set() 
    for i in range(len(A)):
        setA.add(A[i])
        if len(setA) == X:
            return i
    return -1

 

PermCheck

[최초풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    #정렬
    n_A = sorted(A)
    #최소-최대
    diff = n_A[-1] - n_A[0] +1
    if diff == len(A):
        return 1
    else:
        return 0

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    #정렬
    A = sorted(A)
    for i in range(len(A)):
        if A[i] != i+1:
            return 0
    return 1

 

MaxCounters

[최초풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N, A):
    counter = [0] * N
    for i in range(len(A)):
        if A[i] == N+1:
            counter = [max(counter)] * N
        else:
            counter[A[i]-1] += 1
    return counter

[다른 풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N, A):
    count = [0] * N
    maxNum = 0
    tempNum = 0

    for i in range(len(A)):
        idx = A[i] -1
        if idx < N:
            count[idx] = max(maxNum+1, count[idx] + 1)
            tempNum = max(tempNum, count[idx])
        else:
            maxNum = tempNum # 큰 수로 전체리스트 업데이트 나중에 칠게, 일단 maxNum만 업데이트하자

    # 나중에 maxNum으로 업데이트 쳐주기
    for i in range(len(count)):
        if count[i] < maxNum:
            count[i] = maxNum

    return count

MissingInteger

[최초풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    A = set(A)
    A = sorted(A)
    
    if A[-1] < 0:
        return 1
    else:
        S_min = ((A[0]-1)*(A[0]))/2
        S_max = ((A[-1])*(A[-1]+1))/2
        S = int(S_max - S_min)
        
        for x in A:
            S -= x

        if S == 0:
            return A[-1] + 1
        else:
            return S

[다른풀이]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    A = set(A) 
    A = sorted(A)
    missing = 1

    for i in A:
        if i == missing:
            missing += 1

    return missing