728x90
반응형
이분 탐색 & 브루트포스 알고리즘 문제이다.
이분 탐색을 통해 각 버스별로 기다려야 되는 시간을 구하고,
버스를 탈 수 있는 경우가 없다면(res == []) -1을 출력
버스를 탈 수 있는 경우가 있다면 제일 짧은 시간을 출력(min(res))해주면 된다.
N, T = map(int, input().split())
res = []
for _ in range(N):
t, d, n = map(int, input().split())
li = [t+d*i for i in range(n)]
if li[-1] < T:
continue
s, e = 0, n-1
a = 0
while s <= e:
m = (s+e)//2
if li[m] >= T:
a = m
e = m-1
else:
s = m+1
res.append(li[a]-T)
print(min(res) if res else -1)
728x90
반응형
'Agorithm > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 2343번 기타 레슨(python) (0) | 2021.02.26 |
---|---|
백준 알고리즘 2022번 사다리(python) (0) | 2021.02.26 |
백준 알고리즘 1072번 게임(python) (0) | 2021.02.26 |
백준 알고리즘 4072번 Words(python) (0) | 2021.02.25 |
백준 알고리즘 3449번 해밍 거리(python) (0) | 2021.02.25 |