Skip to content

2024-07-11 v. 6.1.2: added "39. Combination Sum" #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 34. Find First and Last Position of Element in Sorted Array | [Link](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | [Link](./lib/medium/34_find_first_and_last_position_of_element_in_sorted_array.rb) |
| 36. Valid Sudoku | [Link](https://leetcode.com/problems/valid-sudoku/) | [Link](./lib/medium/36_valid_sudoku.rb) |
| 38. Count and Say | [Link](https://leetcode.com/problems/count-and-say/) | [Link](./lib/medium/38_count_and_say.rb) |
| 39. Combination Sum | [Link](https://leetcode.com/problems/combination-sum/) | [Link](./lib/medium/39_combination_sum.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.1.1'
s.version = '6.1.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
44 changes: 44 additions & 0 deletions lib/medium/39_combination_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# https://leetcode.com/problems/combination-sum/
# @param {Integer[]} candidates
# @param {Integer} target
# @return {Integer[][]}
def combination_sum(candidates, target)
combine_sum(candidates, target, [], [])
end

private

# @param {Integer[]} candidates
# @param {Integer} target
# @param {Integer[][]} result
# @param {Integer[]} curr
# @return {Integer[][]}
def combine_sum(candidates, target, result, curr)
find_combination(candidates, target, 0, result, curr)

result
end

# @param {Integer[]} candidates
# @param {Integer} target
# @param {Integer} start
# @param {Integer[][]} result
# @param {Integer[]} curr
# @return {Void}
def find_combination(candidates, target, start, result, curr)
if target.zero?
result << curr.dup

return
end

return if target.negative?

(start...candidates.length).each do |i|
curr << candidates[i]
find_combination(candidates, target - candidates[i], i, result, curr)
curr.delete_at(curr.length - 1)
end
end
13 changes: 13 additions & 0 deletions test/medium/test_39_combination_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/39_combination_sum'
require 'minitest/autorun'

class CombinationSumTest < ::Minitest::Test
def test_default
assert_equal([[2, 2, 3], [7]], combination_sum([2, 3, 6, 7], 7))
assert_equal([[2, 2, 2, 2], [2, 3, 3], [3, 5]], combination_sum([2, 3, 5], 8))
assert_equal([], combination_sum([2], 1))
end
end
Loading