-
[자료구조] [python] Truck TourData miner/Algorithm & Data structure 2021. 2. 8. 18:27728x90
Hackerrank의 Data structures 관한 문제 중에서 Hard에 속하는 문제이다. 원 문제 링크는 다음을 따라가보자.
www.hackerrank.com/domains/data-structures?filters%5Bsubdomains%5D%5B%5D=queues- 트럭이 전 펌프를 순회할 수 있는 펌프 지점을 찾되, 가장 index가 작은 값을 찾아야 하므로 앞에서부터 값을 점검하였다. 값을 점검할 때에는, petrol - distance 값을 업데이트하면서, temp값에 저장하였다. 특정 시점에부터 시작하여, 중간에 temp 가 0보다 작은 값을 가질 경우(다음 펌프 지점까지 갈 수 있는 연료가 충분하지 않을 때에는 특정 시점에서부터 계산하는 것을 멈췄다. 다음 시점으로 이동하여 동일한 계산을 반복하였다.
import os import sys # # Complete the truckTour function below. # def truckTour(petrolpumps): # # Write your code here. length = len(petrolpumps) for i in range(length): petrol, distance = petrolpumps[i] temp = petrol - distance if temp < 0: continue cir = i+1 for j in range(length): if cir == length: cir = 0 petrol, distance = petrolpumps[cir] temp += petrol temp -= distance if temp < 0: break cir +=1 if j == length-1: return i else: continue # if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') n = int(input()) petrolpumps = [] for _ in range(n): petrolpumps.append(list(map(int, input().rstrip().split()))) result = truckTour(petrolpumps) fptr.write(str(result) + '\n') fptr.close()
'Data miner > Algorithm & Data structure' 카테고리의 다른 글
[자료구조] [heapq] Jesse and Cookies (0) 2021.02.09 [자료구조] [python] [heapq] QHEAP1 (0) 2021.02.09 [자료구조] [python] Queries with Fixed Length (min-max) (0) 2021.02.07 [자료구조] [python] Queue using Two Stacks (0) 2021.02.06 [자료구조] [python] Down to Zero II (0) 2021.02.06