counter4 [LEET CODE] 347. Top K Frequent Elements( zip 함수, * (아스테리스크)) # Problem Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Constraints: 1 List[int]: freqs = collections.Counter(nums) freqs_heap = [] # 힙에 음수로 삽입 for f in freqs: heapq.heappush(freqs_heap, (-freqs[f], f)) topk = list() # k번 만큼 추출, 민 힙 이므로 가장 작은 음수 순으로 추출 for _ in range(k): topk.append(heapq.heappop(freqs_heap)[1]) return .. 2021. 4. 5. [LEET CODE] 771. Jewels and Stones # Problem You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. Letters are case sensitive, so "a" is considered a different type of stone from "A". Constraints: 1 int: freqs = {} count = 0 # 돌(S)의 빈도 수 계산 for.. 2021. 4. 5. ❗️[LEET CODE] 316. Remove Duplicate Letters # Problem Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ Constraints: 1 str: counter, seen, stack = collections.Counter(s), set(), [] for.. 2021. 3. 23. [LEET CODE] 819. Most Common Word ( 정규표현식 ) #Problem Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique. The words in paragraph are case-insensitive and the answer should be returned in lowercase. Example 1: Input : paragraph : "Bob hit a ball, the hit BALL flew far after it w.. 2021. 3. 15. 이전 1 다음 반응형