Skip to content

Commit ec769fd

Browse files
committed
134. Gas Station
1 parent 32d2888 commit ec769fd

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
| 122 | Best Time to Buy and Sell Stock II | [Ruby](./algorithms/ruby/0122-best-time-to-buy-and-sell-stock-ii.rb) | Medium |
6767
| 129 | Sum Root to Leaf Numbers | [Ruby](./algorithms/ruby/0129-sum-root-to-leaf-numbers.rb) | Medium |
6868
| 133 | Clone Graph | [Ruby](./algorithms/ruby/0133-clone-graph.rb) | Medium |
69+
| 134 | Gas Station | [Ruby](./algorithms/ruby/0134-gas-station.rb) | Easy |
6970
| 136 | Single Number | [Ruby](./algorithms/ruby/0136-single-number.rb) | Easy |
7071
| 142 | Linked List Cycle II | [Ruby](./algorithms/ruby/0142-linked-list-cycle-ii.rb) | Medium |
7172
| 148 | Sort List | [Ruby](./algorithms/ruby/0148-sort-list.rb) | Medium |
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
# 134. Gas Station
4+
# https://leetcode.com/problems/gas-station
5+
# Medium
6+
7+
=begin
8+
There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
9+
10+
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.
11+
12+
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
13+
14+
Example 1:
15+
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
16+
Output: 3
17+
Explanation:
18+
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
19+
Travel to station 4. Your tank = 4 - 1 + 5 = 8
20+
Travel to station 0. Your tank = 8 - 2 + 1 = 7
21+
Travel to station 1. Your tank = 7 - 3 + 2 = 6
22+
Travel to station 2. Your tank = 6 - 4 + 3 = 5
23+
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
24+
Therefore, return 3 as the starting index.
25+
26+
Example 2:
27+
Input: gas = [2,3,4], cost = [3,4,3]
28+
Output: -1
29+
Explanation:
30+
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
31+
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
32+
Travel to station 0. Your tank = 4 - 3 + 2 = 3
33+
Travel to station 1. Your tank = 3 - 3 + 3 = 3
34+
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
35+
Therefore, you can't travel around the circuit once no matter where you start.
36+
37+
Constraints:
38+
n == gas.length == cost.length
39+
1 <= n <= 105
40+
0 <= gas[i], cost[i] <= 104
41+
=end
42+
43+
# @param {Integer[]} gas
44+
# @param {Integer[]} cost
45+
# @return {Integer}
46+
def can_complete_circuit(gas, cost)
47+
if gas.empty? ||
48+
cost.empty? ||
49+
gas.sum < cost.sum
50+
51+
return -1
52+
end
53+
54+
balance = 0
55+
result = 0
56+
57+
gas.count.times do |i|
58+
balance += gas[i] - cost[i]
59+
if balance < 0
60+
balance = 0
61+
result = i + 1
62+
end
63+
end
64+
65+
result
66+
end
67+
68+
# **************** #
69+
# TEST #
70+
# **************** #
71+
72+
require "test/unit"
73+
class Test_can_complete_circuit < Test::Unit::TestCase
74+
def test_
75+
assert_equal 3, can_complete_circuit([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])
76+
assert_equal(-1, can_complete_circuit([2, 3, 4], [3, 4, 3]))
77+
end
78+
end

0 commit comments

Comments
 (0)