[백준 2285 골4] 우체국 (그리디, 파이썬) -/R3

밀루·2023년 4월 4일
0

백준 문제풀이

목록 보기
27/51

if __name__ == "__main__":
    N = int(input())
    total = 0
    city_dict = {}
    for _ in range(N):
        city, people = map(int, input().split())
        city_dict[city] = people
        total += people
    sorted_list = sorted(city_dict.items())
    tmp = 0
    for key, value in sorted_list:
        tmp+= value
        if tmp >= total/2:
            print(key)
            break

Tech:

  1. sorted_dict = sorted(city_dict.items()) 를 하는 순간 dict를 정렬했지만 list로 아웃풋 된다는거.
  2. 논리를 찾느라 한참 헤맸는데, 전체 인구수의 1/2 이상 되는 도시를 리턴해주기만 하면 된다.
profile
벨로그에 틀린 코드나 개선할 내용이 있을 수 있습니다. 지적은 언제나 환영합니다.

0개의 댓글