Skip to content
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 @@ -647,6 +647,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 950. Reveal Cards In Increasing Order | [Link](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [Link](./lib/medium/950_reveal_cards_in_increasing_order.rb) | [Link](./test/medium/test_950_reveal_cards_in_increasing_order.rb) |
| 951. Flip Equivalent Binary Trees | [Link](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [Link](./lib/medium/951_flip_equivalent_binary_trees.rb) | [Link](./test/medium/test_951_flip_equivalent_binary_trees.rb) |
| 958. Check Completeness of a Binary Tree | [Link](https://leetcode.com/problems/check-completeness-of-a-binary-tree/) | [Link](./lib/medium/958_check_completeness_of_a_binary_tree.rb) | [Link](./test/medium/test_958_check_completeness_of_a_binary_tree.rb) |
| 967. Numbers With Same Consecutive Differences | [Link](https://leetcode.com/problems/numbers-with-same-consecutive-differences/) | [Link](./lib/medium/967_numbers_with_same_consecutive_differences.rb) | [Link](./test/medium/test_967_numbers_with_same_consecutive_differences.rb) |
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
Expand Down
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 = '8.1.1'
s.version = '8.1.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
38 changes: 38 additions & 0 deletions lib/medium/967_numbers_with_same_consecutive_differences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

# https://leetcode.com/problems/numbers-with-same-consecutive-differences/
# @param {Integer} n
# @param {Integer} k
# @return {Integer[]}
def nums_same_consec_diff(n, k)
return (0..9).to_a if n == 1

nums = []
(1..9).each { |num| dfs_for_consec_diff(n - 1, num, k, nums) }

nums
end

private

# @param {Integer} n
# @param {Integer} k
# @param {Integer[]} nums
# @return {Void}
def dfs_for_consec_diff(n, num, k, nums)
return nums.push(num) if n.zero?

next_digits = []
tail = num % 10

next_digits << tail + k unless k.zero?
next_digits << tail - k

next_digits.each do |next_digit|
next unless (0..9).include?(next_digit)

new_num = num * 10 + next_digit

dfs_for_consec_diff(n - 1, new_num, k, nums)
end
end
21 changes: 21 additions & 0 deletions test/medium/test_967_numbers_with_same_consecutive_differences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/967_numbers_with_same_consecutive_differences'
require 'minitest/autorun'

class NumbersWithSameConsecutiveDifferencesTest < ::Minitest::Test
def test_default_one
assert_equal(
[181, 292, 707, 818, 929],
nums_same_consec_diff(3, 7)
)
end

def test_default_two
assert_equal(
[12, 10, 23, 21, 34, 32, 45, 43, 56, 54, 67, 65, 78, 76, 89, 87, 98],
nums_same_consec_diff(2, 1)
)
end
end
Loading