Skip to content

Commit 8ad65b2

Browse files
authored
Merge pull request #379 from fartem/1518_Water_Bottles
2023-10-08 v. 3.2.2: added "1518. Water Bottles"
2 parents 2f5c6a5 + 5bd2203 commit 8ad65b2

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
268268
| 1502. Can Make Arithmetic Progression From Sequence | [Link](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Link](./lib/easy/1502_can_make_arithmetic_progression_from_sequence.rb) |
269269
| 1507. Reformat Date | [Link](https://leetcode.com/problems/reformat-date/) | [Link](./lib/easy/1507_reformat_date.rb) |
270270
| 1512. Number of Good Pairs | [Link](https://leetcode.com/problems/number-of-good-pairs/) | [Link](./lib/easy/1512_number_of_good_pairs.rb) |
271+
| 1518. Water Bottle | [Link](https://leetcode.com/problems/water-bottles/) | [Link](./lib/easy/1518_water_bottles.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '3.2.1'
8+
s.version = '3.2.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/easy/1518_water_bottles.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/water-bottles/
4+
# @param {Integer} num_bottles
5+
# @param {Integer} num_exchange
6+
# @return {Integer}
7+
def num_water_bottles(num_bottles, num_exchange)
8+
result = num_bottles
9+
e_bottles = num_bottles
10+
while e_bottles >= num_exchange
11+
portion = e_bottles / num_exchange
12+
result += portion
13+
e_bottles -= portion * num_exchange
14+
e_bottles += portion
15+
end
16+
17+
result
18+
end

test/easy/test_1518_water_bottles.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/1518_water_bottles'
5+
require 'minitest/autorun'
6+
7+
class WaterBottlesTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(13, num_water_bottles(9, 3))
10+
assert_equal(19, num_water_bottles(15, 4))
11+
end
12+
end

0 commit comments

Comments
 (0)