디시인사이드 갤러리

마이너 갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

134. Gas Station

개발뉴비갤로그로 이동합니다. 2023.01.07 15:51:24
조회 47 추천 0 댓글 0
														

https://leetcode.com/problems/gas-station/

 

Gas Station - LeetCode

Gas Station - There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations. Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique Example 1: Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] Output: 3 Explanation: Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. Your tank = 8 - 2 + 1 = 7 Travel to station 1. Your tank = 7 - 3 + 2 = 6 Travel to station 2. Your tank = 6 - 4 + 3 = 5 Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. Therefore, return 3 as the starting index. Example 2: Input: gas = [2,3,4], cost = [3,4,3] Output: -1 Explanation: You can't start at station 0 or 1, as there is not enough gas to travel to the next station. Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 0. Your tank = 4 - 3 + 2 = 3 Travel to station 1. Your tank = 3 - 3 + 3 = 3 You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3. Therefore, you can't travel around the circuit once no matter where you start. Constraints: * n == gas.length == cost.length * 1 <= n <= 105 * 0 <= gas[i], cost[i] <= 104

leetcode.com


class Solution:

def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
go = [g-c for g,c in zip(gas, cost)] * 2
start, num, fuel, lim = 0, 0, 0, len(go)//2
while start < lim:
if fuel + go[start + num] < 0:
fuel -= go[start]
start += 1
num -= 1
else :
fuel += go[start + num]
num += 1
if num == lim :
return start
return -1



시작 station부터 n개의 누적합이 전부 양수인 점을 찾는데

O(N^2)을 돌리기 싫으니 슬라이딩 윈도우로 앞에서 구했던 값들을 사용

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 2025년 가장 기억에 남는 인터넷 이슈는? 운영자 25/12/22 - -
85 909. Snakes and Ladders 이벤트ㅇㅅㅇ(219.251) 23.01.24 48 0
83 997. Find the Town Judge 이벤트ㅇㅅㅇ(219.251) 23.01.23 44 0
82 997. Find the Town Judge [1] 개발뉴비갤로그로 이동합니다. 23.01.23 74 0
81 백준 9996번 키비갤로그로 이동합니다. 23.01.23 56 0
78 마갤 입문함 [2] 키비갤로그로 이동합니다. 23.01.22 61 0
77 131. Palindrome Partitioning [1] 개발뉴비갤로그로 이동합니다. 23.01.22 59 0
75 93. Restore IP Addresses [1] 개발뉴비갤로그로 이동합니다. 23.01.21 72 0
74 Longest Palindromic Substring 5번 o오어o(14.37) 23.01.21 30 0
73 Restore IP Addresses 93번 o오어o(14.37) 23.01.21 39 0
72 안녕하세요 오늘부터 릿코 달리게씁니당 [2] 조르디(112.170) 23.01.21 55 0
71 491. Non-decreasing Subsequences [1] 개발뉴비갤로그로 이동합니다. 23.01.21 57 0
70 974. Subarray Sums Divisible by K [1] 개발뉴비갤로그로 이동합니다. 23.01.19 44 0
69 926. Flip String to Monotone Increasing [1] 개발뉴비갤로그로 이동합니다. 23.01.17 43 0
68 57. Insert Interval [1] 개발뉴비갤로그로 이동합니다. 23.01.17 39 0
66 트리 문제 두 개 개빡치네; 개발뉴비갤로그로 이동합니다. 23.01.15 48 0
65 1061. Lexicographically Smallest ~ 개발뉴비갤로그로 이동합니다. 23.01.15 42 0
64 여기 갤럼들은 전부 해외취업 준비중? [2] EvenAsura갤로그로 이동합니다. 23.01.14 107 0
63 1519. Number of Nodes in the Sub-Tree~ 개발뉴비갤로그로 이동합니다. 23.01.12 28 0
62 1443. Minimum Time to Collect All Apples 개발뉴비갤로그로 이동합니다. 23.01.11 41 0
59 230111 데일리 꽃지갤로그로 이동합니다. 23.01.11 68 0
58 100. Same Tree 개발뉴비갤로그로 이동합니다. 23.01.10 35 0
56 220110 데일리 꽃지갤로그로 이동합니다. 23.01.10 41 0
53 144. Binary Tree Preorder Traversal 개발뉴비갤로그로 이동합니다. 23.01.09 41 0
52 230109 데일리 꽃지갤로그로 이동합니다. 23.01.09 31 0
51 리트코드 4번 [4] o오어o(222.121) 23.01.09 66 0
50 리트코드 3번 o오어o(222.121) 23.01.09 25 0
49 220108 데일리 꽃지갤로그로 이동합니다. 23.01.08 55 0
48 68. Text Justification ㅇㅇ(12.50) 23.01.08 37 0
47 3번문제 코드 딱 한줄만 알려준다 ㅇㅇ(218.234) 23.01.08 38 0
46 코딩 테스트 tdd로 하시는 분 [1] TS(119.64) 23.01.07 61 0
45 가스스테이션(데일리) o오어o(222.121) 23.01.07 52 0
44 230107 데일리 [1] 꽃지갤로그로 이동합니다. 23.01.07 55 0
134. Gas Station 개발뉴비갤로그로 이동합니다. 23.01.07 47 0
42 1833. Maximum Ice Cream Bars 개발뉴비갤로그로 이동합니다. 23.01.06 58 0
41 230106 데일리 꽃지갤로그로 이동합니다. 23.01.06 50 0
38 오늘의 릿코드 Minimum Number of Arrows~ 개발뉴비갤로그로 이동합니다. 23.01.05 41 0
36 230105 데일리 [3] 꽃지갤로그로 이동합니다. 23.01.05 65 0
35 2244. Minimum Rounds 이거 푸는데 TS(119.64) 23.01.05 37 0
34 데일리 문제 어떻게 알아봄...? [1] TS(119.64) 23.01.05 40 0
33 230104 데일리 [2] 꽃지갤로그로 이동합니다. 23.01.04 87 0
31 Minimum Rounds to Complete All Task [1] 개발뉴비갤로그로 이동합니다. 23.01.04 103 0
30 944. Delete Columns to Make Sorted in TS ㅇㅇ(119.64) 23.01.04 43 0
29 백준 17726 Inheritance 꽃지갤로그로 이동합니다. 23.01.04 58 0
28 백준 14503번 로봇 청소기 개발뉴비갤로그로 이동합니다. 23.01.04 94 0
23 오늘의 릿코드 Delete Columns to Make Sorted [2] 개발뉴비갤로그로 이동합니다. 23.01.03 79 0
22 오늘의 데일리 [3] 꽃지갤로그로 이동합니다. 23.01.03 84 0
19 리트코드 해보면서 느낀 점 ㅇㅇ(119.64) 23.01.03 90 0
18 어쨌든 TS로 대문자 판별하기 문제 풀었는데 ㅇㅇ(119.64) 23.01.03 47 0
17 저는 js로 할게요 [5] ㅇㅇ(119.64) 23.01.03 80 0
16 오늘의 릿코드 Detect Capital 개발뉴비갤로그로 이동합니다. 23.01.02 61 1
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2