<๋ฌธ์ ๋งํฌ>
https://school.programmers.co.kr/learn/courses/30/parts/12230
<ํ์ด ์ ๋ต>
1. ์์ ํ์์ด๋ผ ํน๋ณํ ํ์ด์ ๋ต๋ณด๋ค ๋ฌธ์ ๋ฅผ ์ ์ฝ๊ณ ์ถฉ์คํ ๊ตฌํํ๋ฉด ์ด๋ ต์ง ์๊ฒ ํ ์ ์์๋ ๋ฌธ์ ๋ค์ด๋ค.
2. 6๋ฒ์งธ ๋ฌธ์ ์ธ ์ ๋ ฅ๋ง ๋๋ก ๋๋๊ธฐ๋ ์ฐ๊ฒฐ๊ด๊ณ ํ์ ์ ์ํด bfs๋ก ํ์๋ค.
<์ ๋ต ์ฝ๋>
# 1. ์ต์ ์ง์ฌ๊ฐํ
def solution(sizes):
sizes = list(map(sorted, sizes))
x = max(sizes, key = lambda x: x[0])
y = max(sizes, key = lambda x: x[1])
answer = x[0] * y[1]
return answer
# 2. ๋ชจ์๊ณ ์ฌ
def solution(answers):
answer = []
supo_1 = [1, 2, 3, 4, 5] * 2000
supo_2 = [2, 1, 2, 3, 2, 4, 2, 5] * 1250
supo_3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] * 1000
cnt_1, cnt_2, cnt_3 = 0, 0, 0
for i in range(len(answers)):
ans = answers[i]
if ans == supo_1[i]:
cnt_1 += 1
if ans == supo_2[i]:
cnt_2 += 1
if ans == supo_3[i]:
cnt_3 += 1
max_score = max([cnt_1, cnt_2, cnt_3])
temp_list = list(enumerate([cnt_1, cnt_2, cnt_3], start = 1))
for (idx, score) in temp_list:
if score == max_score:
answer.append(idx)
return answer
# 3. ์์์ฐพ๊ธฐ
from itertools import permutations
import math
def solution(numbers):
answer = 0
def sieve(num):
if num == 1:
return False
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
return False
return True
checked = set()
for i in range(1, len(numbers) + 1):
for perm in permutations(numbers, i):
num = ''.join(perm)
num = int(num)
if num == 0:
continue
if num in checked:
continue
checked.add(num)
if sieve(num):
answer += 1
return answer
# 4. ์นดํซ
def solution(brown, yellow):
# ์ฌ๊ฐํ ๋๋ ์ ์ ๋ฐ
perimeter_half = (brown +4) // 2
# ์ฌ๊ฐํ ๊ฐ๋ก ์ธ๋ก ๊ธธ์ด ๊ฒฝ์ฐ์ ์ ์์ ํ์
for i in range(3, perimeter_half//2 + 1):
j = perimeter_half - i
if (i - 2) * (j - 2) == yellow:
answer = [j, i]
break
return answer
# 5. ํผ๋ก๋
from itertools import permutations
def solution(k, dungeons):
answer = 0
N = len(dungeons)
for perm in permutations(dungeons):
cnt = 0
left = k
for i in range(N):
energy = perm[i]
if left >= energy[0]:
left -= energy[1]
cnt += 1
answer = max(cnt, answer)
return answer
# 6. ์ ๋ ฅ๋ง ๋๋ก ๋๋๊ธฐ
from collections import deque
from sys import maxsize
def bfs(start: int, graph: list, visited: list):
queue = deque()
queue.append(start)
visited[start] = 1
while queue:
current = queue.popleft()
for next in graph[current]:
if visited[next]:
continue
visited[next] = 1
queue.append(next)
return sum(visited)
def solution(n:int, wires:list):
answer = maxsize
for i in range(len(wires)):
cut_wire = wires[:]
cut_wire.remove(wires[i])
graph = [[] for _ in range(n + 1)]
for node1, node2 in cut_wire:
graph[node1].append(node2)
graph[node2].append(node1)
visited = [0 for _ in range(n + 1)]
cnt = 0
for edge in graph:
if edge:
cnt = bfs(edge[0], graph, visited)
break
diff = abs(n - cnt - cnt)
answer = min(answer, diff)
return answer
# 7. ๋ชจ์์ฌ์
from itertools import product
def solution(word):
vowel = ['A', 'E', 'I', 'O', 'U']
word_list = []
for length in range(1, 6):
for prod in product(vowel, repeat=length):
word_list.append(''.join(prod))
word_list.sort()
for i in range(len(word_list)):
if word == word_list[i]:
return i + 1
'โญ Problem_Solving > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ (Python/ํ์ด์ฌ) (0) | 2023.02.09 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๋๋์ง (Python/ํ์ด์ฌ) (0) | 2023.02.08 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ์ ๋ ฌ (Python/ํ์ด์ฌ) (0) | 2023.02.05 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ณ ๋์ kit - ํ(Heap) (Python/ํ์ด์ฌ) (0) | 2023.02.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฌ์น์ฐ์ฐ (Python/ํ์ด์ฌ) (0) | 2023.01.31 |
๋๊ธ