1. 더 맵게
https://programmers.co.kr/learn/courses/30/lessons/42626

더보기
import heapq
#최소값 두 값을 빼서 새로운 값을 넣고 다시 최소값을 ...=>힙 사용하기!!
def solution(scoville, K):
count=0
scoville.sort()
while len(scoville)>1:
smin=heapq.heappop(scoville) #heappop원리 : 제일 첫 원소가 min이므로 첫 원소를 꺼냄
if smin>=K: #따라서 첫원소가 최소값이 아닌 리스트를 heappop하면 xx => heapify를 하든지 sort()를 하고 pop인자로 넘겨줘야 함
return count
smin2=heapq.heappop(scoville)
heapq.heappush(scoville,smin+smin2*2)
count+=1
if scoville[0]>K:
return count
return -1
python
2. 카펫
https://programmers.co.kr/learn/courses/30/lessons/42842

더보기
def solution(brown, yellow):
for x in range(1,brown+1):
if (yellow+brown)%x==0:
y=(yellow+brown)//x
if (x-2)*(y-2)==yellow:
return [max(x,y),min(x,y)]
python
3. 위장
https://programmers.co.kr/learn/courses/30/lessons/42578

더보기
#내풀이
from collections import defaultdict
def solution(clothes):
dict=defaultdict(int)
for c in clothes:
dict[c[1]]+=1 #개수 구해서 사전에 ==> counter이용
answer=1
for d in dict.values(): #'누적'곱 => itertools.reduce() 사용
answer*=(d+1)
return answer-1
python
4. 주식 가격
https://programmers.co.kr/learn/courses/30/lessons/42584

더보기
def solution(prices):
answer=[0]*len(prices)
stack=[]
for i,p in enumerate(prices):
while stack and stack[-1][1]>p:
idx=stack.pop()[0]
answer[idx]=i-idx
stack.append((i,p))
return answer
python
5. 짝지어 제거하기
https://programmers.co.kr/learn/courses/30/lessons/12973

더보기
def solution(s):
stack=[]
for char in s:
if stack and stack[-1]==char:
stack.pop()
else:
stack.append(char)
return int(not stack)
python
6. 예상 대진표
https://programmers.co.kr/learn/courses/30/lessons/12985

더보기
def solution(n,a,b):
answer = 0
while a!=b:
a=a//2+1 if a%2 else a//2 #(a+1)//2와 동일
b=b//2+1 if b%2 else b//2
answer+=1
return answer
python
'알고리즘(Python) > 프로그래머스' 카테고리의 다른 글
Level2 - 7~12 (0) | 2022.02.13 |
---|---|
레벨1 문제풀이 - 26문제(카카오 제외) (0) | 2022.02.03 |
Comment