✅ 10 회차 모의고사 D-21일
🚀 1차 코딩테스트 대비
- 백준 1051 (숫자 정사각형)
- 백준 6064 (카잉 달력)
- 백준 2002 (추월)
- 백준 1049 (기타줄)
- SQL 루시와 엘리 찾기
1차 풀이
⭐ 1. 백준 1051 (숫자 정사각형)
# 1. 알고(구현): 백준 1051 (숫자 정사각형) - https://www.acmicpc.net/problem/1051
import sys
from itertools import product
from io import StringIO
sys.stdin = StringIO('''11 10
9785409507
2055103694
0861396761
3073207669
1233049493
2300248968
9769239548
7984130001
1670020095
8894239889
4053971072''')
input = sys.stdin.readline
def solve():
N, M = map(int, input().split())
board = [list(map(int, input().rstrip())) for _ in range(N)]
board_set = set()
for r, c in product(range(N), range(M)):
board_set.add((r, c, board[r][c]))
def find_sqr(r, c, l):
off_set = l - 1
num = board[r][c]
right_top = (r, c + off_set, num)
right_bottom = (r + off_set, c + off_set, num)
left_bottom = (r + off_set, c, num)
for dot in [right_top, right_bottom, left_bottom]:
if not dot in board_set:
return False
return True
max_length = min(N, M)
for length in range(max_length, 0, -1):
for r, c in product(range(N-length+1), range(M-length+1)):
if find_sqr(r, c, length):
print(length**2)
return
solve()⭐ 2. 백준 6064 (카잉 달력)
# 2. 알고(수학): 백준 6064 (카잉 달력) - https://www.acmicpc.net/problem/6064
import sys
from math import lcm
from io import StringIO
sys.stdin = StringIO('''3
10 12 3 9
10 12 7 2
13 11 5 6''')
input = sys.stdin.readline
def solve():
M, N, x, y = map(int, input().split())
max_year = lcm(M, N)
year = x
while year <= max_year:
if (year - y) % N == 0: # (year - x) % M == 0 도 동시 만족
print(year)
return
year += M
print(-1)
T = int(input())
for _ in range(T):
solve()⭐ 3. 백준 2002 (추월)
# 3. 알고(Map): 백준 2002 (추월) - https://www.acmicpc.net/problem/2002
import sys
from itertools import islice
from io import StringIO
sys.stdin = StringIO('''5
ZG508OK
PU305A
RI604B
ZG206A
ZG232ZF
PU305A
ZG232ZF
ZG206A
ZG508OK
RI604B''')
input = sys.stdin.readline
def solve():
N = int(input())
entry_idx = dict()
for i in range(N):
entry_idx[input().rstrip()] = i
exit_cars = list()
for _ in range(N):
exit_cars.append(input().rstrip())
count = 0
for i, cur_car in enumerate(exit_cars):
cur_entry_idx = entry_idx[cur_car]
for behind_car in islice(exit_cars, i, None):
behind_entry_idx = entry_idx[behind_car]
# 추월을 안했으면 내 뒤 차량의 진입 순서는 모두 내 진입 순서보다 커야한다.
if cur_entry_idx > behind_entry_idx: # 작은게 발견되면
count += 1
break
print(count)
solve()⭐ 4. 백준 1049 (기타줄)
# 4. 알고(그리디): 백준 1049 (기타줄) - https://www.acmicpc.net/problem/1049
import sys
from io import StringIO
sys.stdin = StringIO('''10 3
20 8
40 7
60 4''')
input = sys.stdin.readline
def solve():
M, N = map(int, input().split())
div, mod = divmod(M, 6)
pack_min = float('inf')
single_min = float('inf')
for _ in range(N):
p, s = map(int, input().split())
pack_min = min(pack_min, p)
single_min = min(single_min, s)
if single_min * 6 > pack_min: # 패키지가 더 싼경우
print(min(pack_min * (div + 1), pack_min * div + single_min*mod))
else:
print(single_min * M)
solve()✏️ 5. SQL 루시와 엘리 찾기
-- 5. SQL(Lv.2): 루시와 엘리 찾기
-- https://school.programmers.co.kr/learn/courses/30/lessons/59046
SELECT
ANIMAL_ID,
NAME,
SEX_UPON_INTAKE
FROM
ANIMAL_INS
WHERE
NAME IN ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')
ORDER BY
ANIMAL_ID ASC;🚀 2차 코딩테스트 대비
- 백준 20055 (컨베이어 벨트 위의 로봇)
- 백준 15989 (1, 2, 3 더하기 4)
- 백준 2206 (벽 부수고 이동하기)
- 백준 2529 (부등호)
- SQL 5월 식품들의 총매출 조회하기
2차 풀이
⭐ 1. 백준 20055 (컨베이어 벨트 위의 로봇)
# 1. 알고(시뮬): 백준 20055 (컨베이어 벨트 위의 로봇) - https://www.acmicpc.net/problem/20055
import sys
from collections import deque
from io import StringIO
sys.stdin = StringIO('''4 5
10 1 10 6 3 4 8 2''')
input = sys.stdin.readline
def solve():
N, K = map(int, input().split())
durable = map(lambda x: [int(x), 0], input().split())
dq = deque(durable)
broken = 0
step = 0
while broken < K: # 고장갯수 체크
step += 1
# 1. 벨트 회전
dq.rotate(1)
dq[N-1][1] = 0 # 도착로봇 제거.
# 2. 로봇이동
for i in range(N-2, 0, -1):
_, cur_robot = dq[i]
if cur_robot == 0: # 현재칸 로봇 없으면
continue
right_dura, right_robot = dq[i + 1]
# 오른쪽에 내구도 있고, 로봇 없으면
if right_dura > 0 and right_robot == 0:
right_dura -= 1 # 내구도 감소
if right_dura == 0:
broken += 1
dq[i + 1][0] = right_dura
dq[i + 1][1] = 1 # 로봇 이동
dq[i][1] = 0 # 현재 로봇 제거
dq[N-1][1] = 0 # 도착로봇 제거.
# 3. 새로운 로봇 탑승 시도, (로봇은 항상 비어 있음)
dura, _ = dq[0]
if dura > 0:
dq[0][1] = 1 # 로봇 올리고
dura = dura - 1 # 내구도 1 감소
if dura == 0: # 고장 수 추가.
broken += 1
dq[0][0] = dura
print(step)
solve()⭐ 2. 백준 15989 (1, 2, 3 더하기 4)
# 2. 알고(DP): 백준 15989 (1, 2, 3 더하기 4) - https://www.acmicpc.net/problem/15989
import sys
from io import StringIO
sys.stdin = StringIO('''3
4
7
10''')
input = sys.stdin.readline
def solve():
T = int(input())
nums = [int(input()) for _ in range(T)]
max_val = max(nums)
# dp[i] i 원을 만들 수 있는 방법의 수
dp = [0] * (max_val + 1)
dp[0] = 1 # 0 원을 만들 수 있는 경우.
for num in (1, 2, 3):
for i in range(num, max_val + 1): # 중복 허용함으로 순방향 허용.
dp[i] = dp[i] + dp[i - num]
print("\n".join(str(dp[num]) for num in nums))
solve()⭐ 3. 백준 2206 (벽 부수고 이동하기)
# 3. 알고(그래프): 백준 2206 (벽 부수고 이동하기) - https://www.acmicpc.net/problem/2206
import sys
from collections import deque
from io import StringIO
sys.stdin = StringIO('''6 4
0100
1110
1000
0000
0111
0000''')
input = sys.stdin.readline
def solve():
N, M = map(int, input().split())
board = [input().rstrip() for _ in range(N)]
directions = [(0, -1), (0, +1), (-1, 0), (+1, 0)]
# visited[broken][r][c] False, True
visited = [[[False] * M for _ in range(N)] for _ in range(2)]
# broken = 부순 벽의 개수
dq = deque([(0, 0, 1, 0)]) # (row, col, dist, broken)
visited[0][0][0] = True
while dq:
r, c, dist, broken = dq.popleft()
if r == N-1 and c == M-1:
print(dist)
return
dist += 1 # 다음 이동
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < N and 0 <= nc < M:
nxt_val = board[nr][nc]
if nxt_val == '0':
if not visited[broken][nr][nc]:
dq.append((nr, nc, dist, broken))
visited[broken][nr][nc] = True
elif broken == 0:
if not visited[1][nr][nc]:
dq.append((nr, nc, dist, 1))
visited[1][nr][nc] = True
print(-1)
solve()⭐ 4. 백준 2529 (부등호)
# 4. 알고(백트래킹): 백준 2529 (부등호) - https://www.acmicpc.net/problem/2529
import sys
from io import StringIO
sys.stdin = StringIO('''9
> < < < > > > < <''')
input = sys.stdin.readline
def solve():
k = int(input())
non_eqs = input().split()
answer = []
answers = []
def dfs(depth):
if len(answer) == k + 1:
ans_num = "".join(map(str, answer))
answers.append(ans_num)
return
for num in range(10):
if num not in answer:
if not answer: # 비어 있을 때
answer.append(num)
dfs(depth)
answer.pop()
continue
# 숫자 있을 때
if non_eqs[depth] == '<' and answer[-1] > num:
continue
if non_eqs[depth] == '>' and answer[-1] < num:
continue
answer.append(num)
dfs(depth + 1)
answer.pop()
dfs(0)
print(answers[-1])
print(answers[0])
solve()✏️ 5. SQL 5월 식품들의 총매출 조회하기
-- 5. SQL(Lv.4): 5월 식품들의 총매출 조회하기
-- https://school.programmers.co.kr/learn/courses/30/lessons/131117
WITH AM AS (
SELECT
PRODUCT_ID,
SUM(AMOUNT) AS SUM_AMOUNT
FROM
FOOD_ORDER
WHERE
DATE_FORMAT(PRODUCE_DATE, '%Y-%m') = '2022-05'
GROUP BY
PRODUCT_ID
)
SELECT
PRODUCT_ID,
PRODUCT_NAME,
(PRICE*SUM_AMOUNT) AS TOTAL_SALES
FROM
FOOD_PRODUCT
JOIN
AM USING (PRODUCT_ID)
ORDER BY
TOTAL_SALES DESC,
PRODUCT_ID ASC;