Algorithm/python tip
1481. Least Number of Unique Integers after K Removals
개구리는 개꿀개꿀
2021. 2. 28. 02:17
list to map convert
security-nanglam.tistory.com/427
[python] List to Dict (리스트를 딕셔너리로 변환) 총 정리!!
검색어 : List to Dict List 에서 Dict으로 변환하는 방법에는 여러가지 방법이 있습니다...! string_list = ['A','B','C'] 위와 같은 리스트가 있을때, 딕셔너리로 변환시키는 여러가지 방법들 ..! 1. Dictionary..
security-nanglam.tistory.com
dictinary sort by value
stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary
How do I sort a list of dictionaries by a value of the dictionary?
I have a list of dictionaries and want each item to be sorted by a specific value. Take into consideration the list: [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] When sorted by name, it
stackoverflow.com
'''
https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/
arr, k
k개의 요소를 제거한 뒤에
유일한 숫자중 가장 작은 수를 구하라
solution)
숫자 별로 몇개 있는지 기록
중복 숫자가 작은 수부터 k개 제거
남은 숫자의 갯수를 구한다.
'''
class Solution(object):
def findLeastNumOfUniqueInts(self, arr, k):
# list to dictinary(hashmap)
dic = {i: 0 for i in arr}
for i in arr:
dic[i] += 1
# dictionary to list with sort by value
sort_dic = sorted(dic.items(), key=lambda x: x[1])
ans = set()
for (key, val) in sort_dic:
if k >= val:
k -= val
else:
ans.add(key)
return len(ans)