✅ 01 회차 모의고사 D-30일

🚨 강의영상링크


🚀 1차 코딩테스트 대비

  1. 백준 10709 (기상캐스터)
  2. 백준 2477 (참외밭)
  3. 백준 1302 (베스트셀러)
  4. 백준 2217 (로프)
  5. SQL 강원도에 위치한 생산공장 목록 출력하기

👨‍🏫 COLAB

1차 풀이

⭐ 1. 백준 10709 (기상캐스터)

# 1. 알고(구현): 백준 10709 (기상캐스터) - https://www.acmicpc.net/problem/10709
import sys
 
from io import StringIO
sys.stdin = StringIO('''6 8
.c......
........
.ccc..c.
....c...
..c.cc..
....c...''')
 
input = sys.stdin.readline
 
def solve():
    
    H, W = map(int, input().split())
 
    clouds = [input().rstrip() for _ in range(H)]
    result = [[-1] * W for _ in range(H)]
 
    for r in range(H):
        row = clouds[r]
        for c in range(W):
            for check_idx in range(c, -1, -1):
                if row[check_idx] == 'c':
                    result[r][c] = c - check_idx
                    break
    for row in result:
        print(*row)
    
    pass
 
solve()

⭐ 2. 백준 2477 (참외밭)

# 2. 알고(수학): 백준 2477 (참외밭) - https://www.acmicpc.net/problem/2477
 
import sys
 
from io import StringIO
sys.stdin = StringIO('''7
4 50
2 160
3 30
1 60
3 20
1 100''')
 
input = sys.stdin.readline
 
def solve():
 
    K = int(input())
 
    lines = [list(map(int, input().split())) for _ in range(6)]
 
    max_width, max_w_idx = 0, 0
    max_height, max_h_idx = 0, 0
 
    for i in range(6):
        d, length = lines[i]
 
        # width (동, 서)
        if d < 3 and length > max_width:
            max_w_idx = i
            max_width = length
        if d > 2 and length > max_height:
            max_h_idx = i
            max_height = length
 
    # 3번째 뒤가 짧은 사각형의 인덱스임을 알아야 함.
    min_width = lines[(max_w_idx + 3) % 6][1]
    min_height = lines[(max_h_idx + 3) % 6][1]
 
    big_area = max_width * max_height
    samll_area = min_width * min_height
 
    print((big_area-samll_area)*K)
    pass
 
solve()

⭐ 3. 백준 1302 (베스트셀러)

# 3. 알고(Map): 백준 1302 (베스트셀러) - https://www.acmicpc.net/problem/1302
import sys
from heapq import heappush
 
from io import StringIO
sys.stdin = StringIO('''9
table
chair
table
table
lamp
door
lamp
table
chair''')
 
input = sys.stdin.readline
 
def solve():
 
    N = int(input())
 
    count = dict()
    for _ in range(N):
        book = input().rstrip()
        count[book] = count.get(book, 0) + 1
 
    pq = []
    for key, val in count.items():
        heappush(pq, (-val, key))
 
    print(pq[0][1])
 
solve()

⭐ 4. 백준 2217 (로프)

# 4. 알고(그리디): 백준 2217 (로프) - https://www.acmicpc.net/problem/2217
import sys
 
from io import StringIO
sys.stdin = StringIO('''2
10
15''')
 
input = sys.stdin.readline
 
def solve():
 
    N = int(input())
    
    ropes = [int(input()) for _ in range(N)]
    ropes.sort()
 
    max_w = 0
 
    for i in range(N):
        can_w = ropes[i]
        
        # 들 수 있는 무게는 남은 로프 수
        can_up_w = can_w * (N - i)
 
        if can_up_w > max_w:
            max_w = can_up_w
 
    print(max_w)
    pass
 
solve()

✏️ 5. SQL 강원도에 위치한 생산공장 목록 출력하기

-- 5. SQL(Lv.1): 강원도에 위치한 생산공장 목록 출력하기 
-- https://school.programmers.co.kr/learn/courses/30/lessons/131112
SELECT
    FACTORY_ID,
    FACTORY_NAME,
    ADDRESS
FROM
    FOOD_FACTORY
WHERE
    ADDRESS LIKE '강원도%'
ORDER BY
    FACTORY_ID;

🚀 2차 코딩테스트 대비

  1. 백준 17144 (미세먼지 안녕!)
  2. 백준 14728 (벼락치기)
  3. 백준 1238 (파티)
  4. 백준 1759 (암호 만들기)
  5. SQL 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기

👨‍🏫 COLAB

2차 풀이

⭐ 1. 백준 17144 (미세먼지 안녕!)

# 1. 알고(시뮬): 백준 17144 (미세먼지 안녕!) - https://www.acmicpc.net/problem/17144
import sys
 
from io import StringIO
sys.stdin = StringIO('''7 8 1
0 0 0 0 0 0 0 9
0 0 0 0 3 0 0 8
-1 0 5 0 0 0 22 0
-1 8 0 0 0 0 0 0
0 0 0 0 0 10 43 0
0 0 5 0 15 0 0 0
0 0 40 0 0 0 20 0''')
 
input = sys.stdin.readline
 
def solve():
    R, C, T = map(int, input().split())
 
    room = [list(map(int, input().split())) for _ in range(R)]
    
    directions = [
        (+1, 0), (-1, 0),
        (0, +1), (0, -1)
    ]
 
    cleaner = [r for r in range(R) for c in range(C) if room[r][c] == -1 ]
    cleaner.sort()
 
    def next_time(arr):
        new = [[0] * C for _ in range(R)]
 
        for r in range(R):
            for c in range(C):
                cur_val = arr[r][c]
 
                if cur_val == -1:
                    new[r][c] = -1
 
                else:
                    count = 0
                    diffu = cur_val // 5
 
                    for dr, dc in directions:
                        nr, nc = r + dr, c + dc
                        if 0 <= nr < R and 0 <= nc < C and arr[nr][nc] != -1:
                            new[nr][nc] += diffu # 확산
                            count += 1
                    
                    new[r][c] += cur_val - (diffu * count)
 
        return new
 
    def next_clean(arr):
        up_row = cleaner[0]
        down_row = cleaner[1]
 
        # 맨 좌축 부터
        for i in range(up_row - 1, 0, -1):
            arr[i][0] = arr[i-1][0]
 
        for i in range(down_row + 1, R - 1):
            arr[i][0] = arr[i+1][0]
 
        # 맨위, 맨 아래
        for i in range(C-1):
            arr[0][i] = arr[0][i+1]
            arr[R-1][i] = arr[R-1][i+1]
 
        # 맨 우측
        for i in range(up_row):
            arr[i][C-1] = arr[i+1][C-1]
 
        for i in range(R-1, down_row, -1):
            arr[i][C-1] = arr[i-1][C-1]
 
        # 중앙
        for i in range(C-1, 1, -1):
            arr[up_row][i] = arr[up_row][i-1]
            arr[down_row][i] = arr[down_row][i-1]
 
        arr[up_row][1] = 0
        arr[down_row][1] = 0
 
    for _ in range(T):
        room = next_time(room)
        next_clean(room)
 
    result = 0
    for row in room:
        result += sum(row)
 
    print(result + 2)
solve()

⭐ 2. 백준 14728 (벼락치기)

# 2. 알고(DP): 백준 14728 (벼락치기) - https://www.acmicpc.net/problem/14728
import sys
 
from io import StringIO
sys.stdin = StringIO('''3 310
50 40
100 70
200 150''')
 
input = sys.stdin.readline
 
def solve():
 
    N, T = map(int, input().split())
 
    chapters = [tuple(map(int, input().split())) for _ in range(N)]
 
    # dp[i] = i 시간을 썼을 때 얻을 수 있는 최대 점수.
    dp = [0] * (T + 1)
 
    for time_cost, score_val in chapters:
        for i in range(T, time_cost-1, -1):
            dp[i] = max(dp[i], dp[i - time_cost] + score_val)
 
    print(dp[T])
    pass
 
solve()

⭐ 3. 백준 1238 (파티)

# 3. 알고(그래프): 백준 1238 (파티) - https://www.acmicpc.net/problem/1238
import sys
from heapq import heappush, heappop
 
from io import StringIO
sys.stdin = StringIO('''4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3''')
 
input = sys.stdin.readline
 
def solve():
    N, M, X = map(int, input().split())
 
    graph = [[] for _ in range(N + 1)]
    reverse_graph = [[] for _ in range(N + 1)]
 
    for _ in range(M):
        a, b, c = map(int, input().split())
        graph[a].append((b, c))
        reverse_graph[b].append((a, c))
 
    INF = sys.maxsize
    
    def dijkstra(target_graph):
        min_dist = [INF] * (N + 1)
        pq = [(0, X)]
        min_dist[X] = 0
 
        while pq:
            cur_dist, cur = heappop(pq)
 
            if cur_dist <= min_dist[cur]: 
 
                for nxt, cost in target_graph[cur]:
                    nxt_dist = cur_dist + cost
 
                    if nxt_dist < min_dist[nxt]:
                        min_dist[nxt] = nxt_dist
                        heappush(pq, (nxt_dist, nxt)) 
        return min_dist
 
    dist = dijkstra(graph)
    dist_rev = dijkstra(reverse_graph)
 
    max_value = 0
    for i in range(1, N+1):
        total = dist[i] + dist_rev[i]
        if total > max_value:
            max_value = total
 
    print(max_value)
    pass
 
solve()

⭐ 4. 백준 1759 (암호 만들기)

# 4. 알고(백트래킹): 백준 1759 (암호 만들기) - https://www.acmicpc.net/problem/1759
import sys
 
from io import StringIO
sys.stdin = StringIO('''4 6
a t c i s w''')
 
input = sys.stdin.readline
 
def solve():
 
    L, C = map(int, input().split())
 
    chars = list(input().split())
    chars.sort()
    vowels = set(['a', 'e', 'i', 'o', 'u'])
 
    result = []
    def dfs(idx, path):
        if len(path) == L:
            v_count = 0
            c_count = 0
 
            for char in path:
                if char in vowels:
                    v_count += 1
                else: 
                    c_count += 1
            
            if v_count >=1 and c_count >=2:
                result.append("".join(path))
            return
 
        for i in range(idx, C):
            path.append(chars[i])
            dfs(i + 1, path)
            path.pop()            
 
    dfs(0, [])
    print("\n".join(result))
    pass
 
solve()

✏️ 5. SQL 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기

-- 5. SQL(Lv.3): 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기 
-- https://school.programmers.co.kr/learn/courses/30/lessons/151139
SELECT
    MONTH(START_DATE) AS MONTH,
    CAR_ID,
    COUNT(*) AS RECORDS
FROM
    CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE
    START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
    AND CAR_ID IN (
        SELECT CAR_ID
        FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
        WHERE START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
        GROUP BY CAR_ID
        HAVING COUNT(*) >= 5
    )
GROUP BY
    MONTH, CAR_ID
HAVING
    RECORDS > 0
ORDER BY
    MONTH ASC,
    CAR_ID DESC;