
당신은 꺼내려는 상자 번호가 주어졌을 때, 꺼내려는 상자를 포함해 총 몇 개의 택배 상자를 꺼내야 하는지 알고 싶습니다. 창고에 있는 택배 상자의 개수를 나타내는 정수 n, 가로로 놓는 상자의 개수를 나타내는 정수 w와 꺼내려는 택배 상자의 번호를 나타내는 정수 num이 매개변수로 주어집니다. 이때, 꺼내야 하는 상자의 총개수를 return 하도록 solution 함수를 완성해 주세요.
(코드를 날려서 없지만) 대략 이런 느낌
from collections import deque
def solution(n, w, num):
q = deque(range(1, n + 1))
# num의 열(col) 계산
floor = (num - 1) // w
if floor % 2 == 0: # 왼 → 오
col = (num - 1) % w
else: # 오 → 왼
col = w - 1 - (num - 1) % w
count = 0
while q:
box = q.pop() # 위에서부터 꺼냄
floor = (box - 1) // w
if floor % 2 == 0:
box_col = (box - 1) % w
else:
box_col = w - 1 - (box - 1) % w
if box_col == col:
count += 1
if box == num:
break
return count
어떤 상자가 나오는지 상자 번호는 중요하지 않으니 방향을 틀었음
# top 개수, 좌측 정렬, 우측 정렬
# num의 위치, 층
# 0-based
def solution(n, w, num):
answer = 0
n, num = n - 1, num - 1 # 0-based
topflr = n // w + 1
topcnt = n % w + 1# 맨 위에 원소 개수
top = [0] * w
if topflr % 2 == 1:
for i in range(topcnt):
top[i] = 1
else:
for i in range(w - 1, w - 1 - topcnt, -1):
top[i] = 1
tempflr = num // w + 1
if tempflr % 2 == 1: tempidx = num % w
else: tempidx = w - 1 - num % w
answer = topflr - tempflr
if top[tempidx] == 1: answer +=1
return answer
👉 self-rating 인덱스 정의와 같은 디테일이 아직 모자란듯
def solution(n, w, num):
n -= 1
num -= 1
topflr, topcnt = n // w + 1, n % w + 1
tempflr = num // w + 1
top = [0] * w
if topflr % 2 == 1: top[:topcnt] = [1] * topcnt
else: top[w - topcnt:] = [1] * topcnt
tempidx = num % w if tempflr % 2 == 1 else w - 1 - num % w
return topflr - tempflr + 1 if top[tempidx] == 1 else topflr - tempflr
def solution(n, w, num):
n -= 1
num -= 1
total_floor = n // w
cur_floor = num // w
# 현재 상자의 열 계산
if cur_floor % 2 == 0:
col = num % w
else:
col = w - 1 - (num % w)
# 맨 위층 정보
top_count = n % w + 1
top_floor = total_floor
# 맨 위층에서 해당 열에 상자가 있는지
if top_floor % 2 == 0:
exist_on_top = col < top_count
else:
exist_on_top = col >= w - top_count
return total_floor - cur_floor + (1 if exist_on_top else 0)
맨 위층이
왼→오면 col < top_count
오→왼이면 col ≥ w - top_count
👉 맨 위층 어디에 상자가 있는지 볼 필요 ❌ target에 해당하는 열에 있는지만 체크 ⭕