프로그래머스/Level 2
[연습문제 / 프로그래머스] 133501번 : 야간 전술보행
ydin
2022. 11. 14. 19:15
- 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/133501
- 정답 풀이 :
def solution(distance, scope, times):
ch = []
for i in range(len(scope)):
start, end = sorted(scope[i])
work, rest = times[i]
time = start
while time <= end:
N = time % (work+rest)
if 0 < N <= work:
ch.append(time)
break
time += 1
if ch: return sorted(ch)[0]
else: return distance
- 시도해본 풀이 :
78.6에서 틀렸다
def solution(distance, scope, times):
answer = []
soldiers = []
for i in range(len(scope)):
scope[i].sort()
temp = scope[i] + times[i]
temp.append(sum(times[i]))
soldiers.append(temp)
soldiers.sort()
for i in range(len(soldiers)):
start, end = soldiers[i][0], soldiers[i][1]
cycle = end // soldiers[i][4] # 사이클 몇번 도는지
for j in range(start, end + 1):
# 병사 근무 범위에서 근무하는 중일 때
if soldiers[i][4] * cycle < j <= soldiers[i][4] * cycle + soldiers[i][2]:
answer.append(j)
break
if len(answer) == 0:
return distance
else:
return min(answer)
테스트 50%에서 틀렸다.
def solution(distance, scope, times):
sum_times = [sum(times[i]) for i in range(len(times))] # 일 + 휴식 단위
answer = []
location = 1
for i in range(len(scope)):
start, end = scope[i][0], scope[i][1]
cycle = end // sum_times[i] # 사이클 몇번 도는지
for j in range(start, end + 1):
# 병사 근무 범위에서 근무하는 중일 때
if sum_times[i] * cycle < j <= sum_times[i] * cycle + times[i][0]:
answer.append(j)
break
if len(answer) == 0:
return distance
else:
return min(answer)