[배열] 백준 1713번 후보 추천하기

정은경·2020년 5월 17일
0

백준 문제풀이

목록 보기
11/51

1. 문제


2. 나의 풀이

  • 틀렸다는 나의 풀이
# candidates = dict()
# recommended_count = list()
#
#
#
# N = int(input())
# sum_recommend_count = int(input())
#
# recommend = list([int(x) for x in input().split()])
# count = 0
#
# for r in recommend:
#     if candidates.get(r):
#         candidates[r] += 1
#     elif count < N:
#
#         candidates[r] = 1
#         count += 1
#     else:
#         min_num = min(candidates.values())
#         for r in candidates:
#             if candidates[r] == min_num:
#
#
#
# print(count, candidates)



N = int(input())
sum_recommend_count = int(input())
recommend = list([int(x) for x in input().split()])

# 딕셔너리 스택
pics = list()
count_info = dict()

for r in recommend:
    if len(pics) < N:
        if r not in pics:
            pics.append(r)
            count_info[r] = 1
        else:
            count_info[r] += 1
    else:
        # 삭제하는 루틴
        if r in pics:
            count_info[r] += 1
        else:
            min_count = min(count_info.values())
            # print(pics, min_count)
            for p in range(len(pics)):
                if count_info[pics[p]] == min_count:
                    del pics[p]
                    break
            pics.append(r)
            count_info[r] = 1


# print(sorted(pics))
for p in sorted(pics):
    print(p, end =' ')
print()

3. 남의 풀이

n = int(input())
num = int(input())

def is_in_arr(arr , w):
    for i in arr:
        if i[2] == w:
            return True
    return False

arr = []
who = input().split()
for idx, w in enumerate(who):
    #1
    if is_in_arr(arr, w):
        #2
        for index, var in enumerate(arr):
            if var[2] == w:
                arr[index][0] += 1
                break
    else:
        #3
        if len(arr) < n:
            arr.append([1, idx, w])
        else:
            arr[0] = [1, idx, w]

    arr.sort(key=lambda x: (x[0], x[1]))

arr.sort(key=lambda x:int(x[2]))
for i in range(n):
    if i == n-1:
        print(arr[i][2])
    else:
        print(arr[i][2], end = ' ')

4. 느낀 점

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글