File tree 4 files changed +50
-1
lines changed 4 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -515,3 +515,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
515
515
| 128. Longest Consecutive Sequence | [ Link] ( https://leetcode.com/problems/longest-consecutive-sequence/ ) | [ Link] ( ./lib/medium/128_longest_consecutive_sequence.rb ) |
516
516
| 129. Sum Root to Leaf Numbers | [ Link] ( https://leetcode.com/problems/sum-root-to-leaf-numbers/ ) | [ Link] ( ./lib/medium/129_sum_root_to_leaf_numbers.rb ) |
517
517
| 133. Clone Graph | [ Link] ( https://leetcode.com/problems/clone-graph/ ) | [ Link] ( ./lib/medium/133_clone_graph.rb ) |
518
+ | 134. Gas Station | [ Link] ( https://leetcode.com/problems/gas-station/ ) | [ Link] ( ./lib/medium/134_gas_station.rb ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.4.9 '
8
+ s . version = '6.5.0 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # https://leetcode.com/problems/gas-station/
4
+ # @param {Integer[]} gas
5
+ # @param {Integer[]} cost
6
+ # @return {Integer}
7
+ def can_complete_circuit ( gas , cost )
8
+ tmp_sum = 0
9
+ index = 0
10
+ sum = gas . first - cost . first
11
+ ( 1 ...gas . size ) . each do |i |
12
+ if sum . negative?
13
+ index = i
14
+ tmp_sum += sum
15
+ sum = 0
16
+ end
17
+
18
+ sum += gas [ i ] - cost [ i ]
19
+ end
20
+
21
+ sum += tmp_sum
22
+
23
+ sum >= 0 ? index : -1
24
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+ require_relative '../../lib/medium/134_gas_station'
5
+ require 'minitest/autorun'
6
+
7
+ class GasStationTest < ::Minitest ::Test
8
+ def test_default
9
+ assert_equal (
10
+ 3 ,
11
+ can_complete_circuit (
12
+ [ 1 , 2 , 3 , 4 , 5 ] ,
13
+ [ 3 , 4 , 5 , 1 , 2 ]
14
+ )
15
+ )
16
+ assert_equal (
17
+ -1 ,
18
+ can_complete_circuit (
19
+ [ 2 , 3 , 4 ] ,
20
+ [ 3 , 4 , 3 ]
21
+ )
22
+ )
23
+ end
24
+ end
You can’t perform that action at this time.
0 commit comments