Skip to content

Commit 3342115

Browse files
committed
2025-01-16 v. 8.1.2: added "967. Numbers With Same Consecutive Differences"
1 parent 2eecb4a commit 3342115

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
647647
| 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) |
648648
| 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) |
649649
| 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) |
650+
| 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) |
650651
| 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) |
651652
| 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) |
652653
| 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) |

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 = '8.1.1'
8+
s.version = '8.1.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/numbers-with-same-consecutive-differences/
4+
# @param {Integer} n
5+
# @param {Integer} k
6+
# @return {Integer[]}
7+
def nums_same_consec_diff(n, k)
8+
return (0..9).to_a if n == 1
9+
10+
nums = []
11+
(1..9).each { |num| dfs_for_consec_diff(n - 1, num, k, nums) }
12+
13+
nums
14+
end
15+
16+
private
17+
18+
# @param {Integer} n
19+
# @param {Integer} k
20+
# @param {Integer[]} nums
21+
# @return {Void}
22+
def dfs_for_consec_diff(n, num, k, nums)
23+
return nums.push(num) if n.zero?
24+
25+
next_digits = []
26+
tail = num % 10
27+
28+
next_digits << tail + k unless k.zero?
29+
next_digits << tail - k
30+
31+
next_digits.each do |next_digit|
32+
next unless (0..9).include?(next_digit)
33+
34+
new_num = num * 10 + next_digit
35+
36+
dfs_for_consec_diff(n - 1, new_num, k, nums)
37+
end
38+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/967_numbers_with_same_consecutive_differences'
5+
require 'minitest/autorun'
6+
7+
class NumbersWithSameConsecutiveDifferencesTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[181, 292, 707, 818, 929],
11+
nums_same_consec_diff(3, 7)
12+
)
13+
end
14+
15+
def test_default_two
16+
assert_equal(
17+
[12, 10, 23, 21, 34, 32, 45, 43, 56, 54, 67, 65, 78, 76, 89, 87, 98],
18+
nums_same_consec_diff(2, 1)
19+
)
20+
end
21+
end

0 commit comments

Comments
 (0)