본문 바로가기

카테고리 없음

코딜리티 lesson8 Dominator, EquiLeader

Dominator

[최초풀이]

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

def solution(A):
    if len(A) == 0:
        return -1
    
    limitNum = len(A)/2

    setA = set(A)

    ans = []
    for x in setA:
        if A.count(x) > limitNum:
            ans.append(x)
    
    if len(ans) == 0:
        return -1

    for y in ans:
        for i in range(len(A)):
            if y == A[i]:
                return i

[다른풀이]

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

def solution(A):
    dic = dict()
    half = len(A) / 2

    for i in range(len(A)):
        if A[i] in dic:
            dic[A[i]] += 1
        else:
            dic[A[i]] = 1
        if dic[A[i]] > half:
            return i
    return -1

  

EquiLeader

[최초풀이]

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

def solution(A):
    arr1 = []
    arr2 = []
    N = len(A)
    cnt = 0

    if N <= 1:
        return 0

    for i in range(1, N):
        arr1 = A[:i]
        arr2 = A[i:]
        if findEqui(arr1) == findEqui(arr2):
            if findEqui(arr1) == -1:
                return 0
            cnt += 1
    return cnt


def findEqui(arr):
    dict1 = dict()
    half = len(arr)/2

    for i in range(len(arr)):
        if not arr[i] in dict1:
            dict1[arr[i]] = 1
        else:
            dict1[arr[i]] += 1
        if dict1[arr[i]] > half:
            return arr[i]
    return -1

[다른풀이]

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

def solution(A):
    count = 0

    rdict = dict()
    rlen = len(A)
    for a in A:
        if a in rdict:
            rdict[a] += 1
        else:
            rdict[a] = 1
    ldict = dict()
    llen = 0

    l_leader = 0
    l_leader_cnt = 0

    for a in A:
        rdict[a] -= 1
        rlen -= 1

        if a in ldict:
            ldict[a] += 1
        else:
            ldict[a] = 1
        llen += 1

        if ldict[a] > l_leader_cnt:
            l_leader = a
            l_leader_cnt = ldict[a]

        if l_leader_cnt > llen / 2 and rdict[l_leader] > rlen / 2:
            count += 1
    
    return count