2/6 (Mon): 이코테 복습 (그리디, 구현)

Minsang Kang·2023년 2월 6일
0

TIL

목록 보기
1/12

이코테 복습

출제 경향

  • 그리디, 구현, DFS/BFS 탐색, DP, 그래프, 정렬

그리디

큰 수의 법칙

  • 배열의 주어진 수들을 M번 더하여 가장 큰 수를 만드는 법칙
  • 특정인덱스의 수가 연속해서 K번 초과하여 더해질 수 없음
  • 배열 크기 N, 더해지는 횟수 M, K -> 큰 수의 법칙 결과

풀이특징

  • 반복 비교 -> 몫, 나머지를 구하여 한번에 계산
n, m, k = map(int, input().split())
numbers = list(map(int, input().split()))
numbers.sort()

maxVal = numbers[n-1]
secVal = numbers[n-2]

result = 0
result += (maxVal) * (m // (k+1)) * k
result += (secVal) * (m // (k+1))
result += (maxVal) * (m % (k+1))

print(result)

숫자 카드 게임

  • n행, m열의 숫자카드들
  • 행을 고른 후 해당 행의 가장 숫자가 낮은 카드를 뽑는다
  • 가장 높은 숫자를 출력

풀이특징

  • 반복문을 한번만 할 수 있도록
n, m = map(int, input().split())

maxCard = -1
for _ in range(n):
    row = list(map(int, input().split()))
    maxCard = max(maxCard, min(row))
print(maxCard)

1이 될 때까지

  • n이 1이될 때 까지 두과정을 반복적으로 수행, 1이 될 때 까지의 최소횟수
  • n에서 1을 뺀다.
  • n을 k로 나눈다.

풀이특징

  • 반복 비교 -> 몫, 나머지를 구하여 한번에 계산
n, k = map(int, input().split())
count = 0

while True:
    canDiv = k * (n // k) #k 배수
    count += (n - canDiv) #1 빼기: 한번에 연산
    n = canDiv
    
    if n < k:
        break
    n //= k #k로 나누기
    count += 1

print(count)

구현

상하좌우

  • n * n 크기의 정사각형 지도
  • 좌상단: (1, 1), 우하단: (n, n)
  • L, R, U, D 명령에 따라 벗어나는 움직임 제외 이동, 최종 도착지점 출력

풀이특징

  • 커멘드에 따라 이동같은 시뮬레이션의 경우 x, y 좌표이동방향에 대한 값이 필요
  • dictionary 를 사용하여 푼 경우
move = dict(L=(0, -1), R=(0, 1), U=(-1, 0), D=(1, 0))

n = int(input())
inputs = input().split()

x, y = 1, 1
for command in inputs:
    nx = x + move[command][0]
    ny = y + move[command][1]
    
    if nx < 1 or nx > n:
        continue
    if ny < 1 or ny > n:
        continue
        
    x, y = nx, ny
    
print(x, y)

시각

  • n 입력시 0시 0 분 0초 ~ n시 59분 59초 까지의 6자리수 비교
  • 3이 하나라도 포함되어 있는 모든 경우의 수 출력

풀이특징

  • str(n)으로 문자열 생성, in 통해 확인
n = int(input())

count = 0
for h in range(0, n+1):
    for m in range(0, 60):
        for s in range(0, 60):
            if '3' in str(h)+str(m)+str(s):
                count += 1
print(count)

왕실의 나이트

  • 8*8 크기의 체스, 행: 1~8, 열: a~h, 나이트 이동 (2가지 방법)
  • 수평 2칸이동, 수직 1칸이동
  • 수직 2칸이동, 수평 1칸이동
  • 나이트가 이동할 수 있는 경우의 수 출력
move = [(2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)]

inputs = list(input())
row = ord(inputs[0]) - ord('a') + 1
column = int(inputs[1])

count = 0
for m in move:
    nr = row + m[0]
    nc = column + m[1]
    if nr < 1 or nr > 8:
        continue
    if nc < 1 or nc > 8:
        continue
    count += 1
print(count)

게임 개발

  • n*m 크기의 맵, 위치 (x, y): (행, 열)
  • 상하좌우로 이동 가능, 바다로는 갈 수 없다.
  • 현재 위치에서 현재 방향을 기준으로 반시계방향부터 차례로 갈곳을 정한다.
  • 왼쪽에 가보지 않은 칸이 존재할 경우 전진, 가보지 않는 칸이 없다면 회전만 하고 continue
  • 네방향이 모두 가본칸 또는 바다: 바라보는 방향 유지하고 뒤로 이동 (바다인 경우 종료)
  • 방문한 칸의 수를 출력
move = [(-1, 0), (0, 1), (1, 0), (0, -1)] #북, 동, 남, 서

n, m = map(int, input().split())
x, y, direction = map(int, input().split())

graph = [list(map(int, input().split())) for _ in range(n)]

moveCount = 1
while True:
    graph[x][y] = 2
    directionCount = 0
    for _ in range(4):
        directionCount += 1
        direction -= 1
        direction %= 4
        
        nx = x + move[direction][0]
        ny = y + move[direction][1]
        
        if graph[nx][ny] == 0:
            x = nx
            y = ny
            moveCount += 1
            break
    
    if directionCount != 4:
        continue
    
    nx = x - move[direction][0]
    ny = y - move[direction][1]
    
    if graph[nx][ny] == 2:
        x = nx
        y = ny
    else:
        break
        
print(moveCount)
profile
 iOS Developer

0개의 댓글