프로그래머스(파이썬)/LV.2(파이썬)
더 맵게 → heap★★
나의 풀이 import heapq def solution(scoville, K): cnt=0 heap=[] for x in scoville: heapq.heappush(heap, x) if heap[0]>=K: return cnt while(len(heap)>1): 첫번째로 가장 작은 원소를 pop한다. min1=heapq.heappop(heap) 또 남아있는 것 중에서 가장 작은 원소를 pop한다. min2=heapq.heappop(heap) new_scoville=min1+(min2*2) heap.append(new_scoville) cnt+=1 if heap[0]>=K: return cnt return -1 힙(자료구조)의 활용 ▶ import heapq heap =[] heapq.heappush(..
귤 고르기→ Counter() 함수, most_common()
from collections import Counter def solution(k, tangerine): list=Counter(tangerine).most_common() #종류의 수 kind=1 sum=list[0][1] for i in range(len(list)-1): if sum>=k: return kind else: sum+=list[i+1][1] kind+=1 return kind
k진수에서 소수 개수 구하기→ 10진수를 n진법, 소수 판별★,filter()함수★
나의 풀이 import math #10진법을 k진법으로 변환 def solution(num, n): result = [] if num == 0: result.append(0) while num > 0: if num % n < 10: result.append(num % n) else: result.append(chr(num % n - 10 + ord('A'))) num //= n list_str1=join_split(result[::-1]) list_str1=list(filter(None, list_str1)) return counter(list_str1) #소수인지 판별 def isPrime(x): if x ==1: return False for i in range(2, int(math.sqrt(x))+..
타켓넘버→ DFS(깊이우선탐색)★★+요소 더하기, 요소 빼기+ 재귀함수
나의 틀린 풀이 def solution(numbers, target): return DFS(0, 0, target, 0) def DFS(L, sum, target, cnt): if L ==len(numbers): if sum == target: cnt+=1 return cnt else: DFS(L+1, sum-numbers[L], target, cnt) DFS(L+1, sum+numbers[L], target, cnt) numbers = [4, 1, 2, 1] target = 4 print(solution(numbers, target)) 성공한 나의 풀이 def solution(numbers, target): def DFS(L, sum, target, cnt): if L ==len(numbers): if..