from collections import deque
def solution(people, limit):
people = deque(sorted(people))
answer = 0
while people :
first = people.pop()
if people and first + people[0] <= limit :
people.popleft()
answer += 1
return answer
def solution(numbers):
str_numbers = list(map(str, numbers))
str_numbers.sort(key=lambda x: x*3, reverse=True)
result = ''.join(str_numbers)
return str(int(result))
def solution(citations):
citations.sort(reverse = True)
for i, citation in enumerate(citations) :
if citation <= i :
return i
return len(citations)
def solution(priorities, location):
files = [[i, priority] for i, priority in enumerate(priorities)]
answer = []
while files :
idx, priority = files.pop(0)
if not files or priority >= max(x[1] for x in files) :
answer.append(idx)
else :
files.append((idx, priority))
return answer.index(location)+1
def solution(prices):
n = len(prices)
answer = [n - 1 - idx for idx in range(n)]
stack = []
for i, price in enumerate(prices):
while stack and price < prices[stack[-1]]:
top = stack.pop()
answer[top] = i - top
stack.append(i)
return answer
import re
def solution(skill, skill_trees):
answer = 0
for skill_tree in skill_trees :
core_skill = re.sub('[^'+skill+']', '', skill_tree)
answer += 1 if core_skill == skill[:len(core_skill)] else 0
return answer
from itertools import chain, combinations, permutations
import math
def is_prime(x):
if x<2: return False
for i in range(2, int(math.sqrt(x))+1):
if x%i==0: return False
return True
def solution(numbers) :
total_case = set(chain.from_iterable(permutations(numbers, r) for r in range(1, len(numbers)+1)))
answer = set()
for num_tuple in total_case :
num = int(''.join(num_tuple))
if is_prime(num) :
answer.add(num)
return len(answer)
from itertools import chain
def fill_snail_pattern(n):
triangle = [[0] * (i + 1) for i in range(n)]
current_number = 1
width_index, height_index = 0, -1
for level in range(n):
for _ in range(level, n):
if level % 3 == 0: # ์๋๋ก ์ด๋ (์ธ๋ก ์ธ๋ฑ์ค ์ฆ๊ฐ)
height_index += 1
elif level % 3 == 1: # ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ (๊ฐ๋ก ์ธ๋ฑ์ค ์ฆ๊ฐ)
width_index += 1
else: # ์ผ์ชฝ ์ ๋๊ฐ์ ์ผ๋ก ์ด๋ (๊ฐ๋ก & ์ธ๋ก ์ธ๋ฑ์ค ๊ฐ์)
width_index -= 1
height_index -= 1
triangle[height_index][width_index] = current_number
current_number += 1
return list(chain(*triangle))
from math import gcd
def solution(w,h):
return w*h - (w+h - gcd(w, h
import heapq
def solution(scoville, K):
answer = 0
heapq.heapify(scoville)
while scoville :
first = heapq.heappop(scoville)
if not scoville and first < K :
return -1
elif first >= K :
break
second = heapq.heappop(scoville)
new_food = first + second * 2
heapq.heappush(scoville, new_food)
answer += 1
return answer