2023.01.18 코딩 테스트 공부 기록 입니다.
🤑풀이
def solution(number, k):
stack = []
l = len(number)
for num in number:
if not stack:
stack.append(num)
continue
if k > 0:
while stack[-1] < num:
stack.pop()
k -= 1
if not stack or k == 0:
break
stack.append(num)
stack = stack[:-k] if k > 0 else stack
return ''.join(stack)
👩🏫 참고 코드
위 문제는 greedy 문제로서 "탐욕적으로 결정된 것"은 "결정된 후" 변하지 않는다는 것을 염두해 두어야 합니다. 따라서, "탐욕적으로 마감시킬 수 있는 결정 조건"에 대해서 생각해야 합니다.
위 풀이의 접근은 number에서 num이 추출되고 while loop을 돌면 방금 고려한 num을 더이상 고려할 필요 없도록 조건을 만들었습니다.
위 조건이 없다면, number에 접근하는 index를 +1, -1 하면서 계속 위치를 수정해야 되는거 같습니다.
향후 문제 접근 방식
Reference
🤑풀이
def down(row, col, now, n) :
for _ in range(n) :
lst[row][col] = now
now += 1
row += 1
return row-1, col+1, now
def right(row, col, now, n) :
for _ in range(n) :
lst[row][col] = now
now += 1
col += 1
return row-1, col-2, now
def diagonal(row, col, now, n) :
for _ in range(n) :
lst[row][col] = now
now += 1
row -= 1
col -= 1
return row+2, col+1, now
def solution(n) :
global lst
lst, max_num, now = [], 0, 1
for i in range(1, n+1) :
max_num += i
lst.append([0 for _ in range(i)])
row, col = 0, 0
while now <= max_num :
row, col, now = down(row, col, now, n)
n -= 1
if n == 0 or lst[row][col] != 0 :
break
row, col, now = right(row, col, now, n)
n -= 1
if lst[row][col] != 0 :
break
row, col, now = diagonal(row, col, now, n)
n -= 1
if lst[row][col] != 0 :
break
answer = []
for ls in lst :
answer.extend(ls)
return answer
👩🏫 아이디어
🤑풀이
def get_board_matrix(rectangle) :
max_x, max_y = 0, 0
for box in rectangle :
if box[2] > max_x :
max_x = box[2]
if box[3] > max_y :
max_y = box[3]
board = [[0 for _ in range(2*max_x)] for _ in range(2*max_y)]
return board
def update_board_matrix(rectangle, board) :
for box in rectangle :
x_length = 2*(box[2] - box[0]) + 1
y_length = 2*(box[3] - box[1]) + 1
# fill bottom, up
for i in range(x_length) :
board[2*box[1]-1][2*box[0]-1+i] = 1 # bottom
board[2*box[3]-1][2*box[0]-1+i] = 1 # up
for i in range(y_length) :
board[2*box[1]-1+i][2*box[0]-1] = 1 # right
board[2*box[1]-1+i][2*box[2]-1] = 1
return board
# 현재 어떤 box의 side 정보를 출력
def get_start_info(rectangle, now_x, now_y) :
start = {}
# 각각의 사각형에서 이동 가능한 경로 생성 (Board 생성)
for idx, info in enumerate(rectangle) :
x1, y1, x2, y2 = info[0], info[1], info[2], info[3]
start[idx] = {'left' : [[2*x1-1], [i for i in range(2*y1-1, 2*y2)]],
'right' : [[2*x2-1], [i for i in range(2*y1-1, 2*y2)]],
'up' : [[i for i in range(2*x1-1, 2*x2)], [2*y2-1]],
'down' : [[i for i in range(2*x1-1, 2*x2)], [2*y1-1]]
}
now_info = []
for box in start :
for side in start[box] :
if now_x in start[box][side][0] and now_y in start[box][side][1] :
now_info.append((box, side))
return now_info
# '0' : left, '1' : right, '2' : up, '3' : down
def go_line(start_info, now_x, now_y) :
if start_info[0][1] == 'left' :
next_x, next_y = now_x, now_y+1
elif start_info[0][1] == 'right' :
next_x, next_y = now_x, now_y-1
elif start_info[0][1] == 'up' :
next_x, next_y = now_x+1, now_y
elif start_info[0][1] == 'down' :
next_x, next_y = now_x-1, now_y
return next_x, next_y
# 같은 박스 내에서 꺽는 이동
def change_direction(start_info, now_x, now_y) :
if start_info[0][1] in ['left', 'up'] and start_info[1][1] in ['left', 'up'] :
next_x, next_y = now_x+1, now_y
if start_info[0][1] in ['right', 'up'] and start_info[1][1] in ['right', 'up'] :
next_x, next_y = now_x, now_y-1
if start_info[0][1] in ['left', 'down'] and start_info[1][1] in ['left', 'down'] :
next_x, next_y = now_x, now_y+1
if start_info[0][1] in ['right', 'down'] and start_info[1][1] in ['right', 'down'] :
next_x, next_y = now_x-1, now_y
return next_x, next_y
def move_to_other_box(start_info, now_x, now_y) :
if start_info[0][1] in ['left', 'down'] and start_info[1][1] in ['left', 'down'] :
next_x, next_y = now_x-1, now_y
if start_info[0][1] in ['left', 'up'] and start_info[1][1] in ['left', 'up'] :
next_x, next_y = now_x, now_y+1
if start_info[0][1] in ['right', 'up'] and start_info[1][1] in ['right', 'up'] :
next_x, next_y = now_x+1, now_y
if start_info[0][1] in ['right', 'down'] and start_info[1][1] in ['right', 'down'] :
next_x, next_y = now_x, now_y-1
return next_x, next_y
# 바깥쪽으로 도는 방법 -> (진행 방향을 직진으로 설정하는 기준) -90도, 180도, 90도 위 순서대로 확인
def search_lst(dx, dy) :
return [[-1*dy, dx], [dx, dy], [dy, -1*dx]]
def solution(rectangle, characterX, characterY, itemX, itemY) :
start, getItem = True, False
# Make Matrix Board 2X scale (x -> 2x-1, y -> 2y-1)
board = get_board_matrix(rectangle)
board = update_board_matrix(rectangle, board)
now_x, now_y = 2*(characterX)-1, 2*(characterY)-1
# 시작점 도는 방향 설정
before = (now_x, now_y)
item_length, return_length = 0, 0
while True :
if start :
start_info = get_start_info(rectangle, now_x, now_y)
if len(start_info) == 1 :
next_x, next_y = go_line(start_info, now_x, now_y)
else :
if start_info[0][0] != start_info[1][0] :
next_x, next_y = move_to_other_box(start_info, now_x, now_y)
else :
next_x, next_y = change_direction(start_info, now_x, now_y)
start = False
else :
dx, dy = now_x - before[0], now_y - before[1]
search = search_lst(dx, dy)
for check in search :
next_x, next_y = now_x+check[0], now_y+check[1]
if next_x > 0 and next_x < len(board[0]) and next_y > 0 and next_y < len(board) :
if board[next_y][next_x] == 1 :
break
# 시작점 -> item
if getItem :
return_length +=1
if next_x == 2*characterX-1 and next_y == 2*characterY -1 :
break
else : # item -> 시작점
item_length +=1
if next_x == 2*itemX-1 and next_y == 2*itemY-1 :
getItem = True
before = (now_x, now_y)
now_x, now_y = next_x, next_y
return min(return_length, item_length)//2
👩🏫 아이디어