count3 [LeetCode] Ransom Note 파이썬 (dict, set, count) from collections import defaultdict class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: seen = defaultdict(int) for r in ransomNote: seen[r] += 1 for m in magazine: if m in seen: seen[m] -= 1 for v in seen.values(): if v > 0: return False return True "fihjjjjei" "hjibagacbhadfaefdjaeaebgi" defaultdict(, {'f': -1, 'i': 0, 'h': -1, 'j': 2, 'e': -2}) seen 이라는 dict에 0보다 큰.. 2021. 4. 14. [LeetCode] Classes More Than 5 Students (group by, having, count, distinct) SELECT class FROM courses GROUP BY class HAVING COUNT(DISTINCT student) > 4; class 로 group by 한후 class 당 unique한 (distinct) student를 count한 결과가 4보다 큰 class 만 select 2021. 4. 9. [프로그래머스] 다음 큰 숫자 def solution(num): b_num = bin(num) b_num_1 = b_num.count("1") new_num = num+1 while True: b_new_num = bin(new_num) new_num_1 = b_new_num.count("1") if b_num_1 == new_num_1: break new_num += 1 return new_num 지저분하지만 맞긴했따. def solution(num): new_num = num+1 while True: if bin(num).count("1") == bin(new_num).count("1"): break new_num += 1 return new_num 깔끔하게 2021. 3. 15. 이전 1 다음