diff --git a/README.md b/README.md index 05620267..3d7af9c7 100644 --- a/README.md +++ b/README.md @@ -699,6 +699,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1461. Check If a String Contains All Binary Codes of Size K | [Link](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) | [Link](./lib/medium/1461_check_if_a_string_contains_all_binary_codes_of_size_k.rb) | [Link](./test/medium/test_1461_check_if_a_string_contains_all_binary_codes_of_size_k.rb) | | 1472. Design Browser History | [Link](https://leetcode.com/problems/design-browser-history/) | [Link](./lib/medium/1472_design_browser_history.rb) | [Link](./test/medium/test_1472_design_browser_history.rb) | | 1481. Least Number of Unique Integers after K Removals | [Link](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [Link](./lib/medium/1481_least_number_of_unique_integers_after_k_removals.rb) | [Link](./test/medium/test_1481_least_number_of_unique_integers_after_k_removals.rb) | +| 1492. The kth Factor of n | [Link](https://leetcode.com/problems/the-kth-factor-of-n/) | [Link](./lib/medium/1492_the_kth_factor_of_n.rb) | [Link](./test/medium/test_1492_the_kth_factor_of_n.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) | | 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 8634a29f..b01d4c35 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '8.6.5' + s.version = '8.6.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1492_the_kth_factor_of_n.rb b/lib/medium/1492_the_kth_factor_of_n.rb new file mode 100644 index 00000000..24fb28c1 --- /dev/null +++ b/lib/medium/1492_the_kth_factor_of_n.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/the-kth-factor-of-n/ +# @param {Integer} n +# @param {Integer} k +# @return {Integer} +def kth_factor(n, k) + factors = ::Array.new(k, 0) + p = 0 + i = 1 + + while i <= n && p < k + if (n % i).zero? + factors[p] = i + p += 1 + end + + i += 1 + end + + p < k ? -1 : factors.last +end diff --git a/test/medium/test_1492_the_kth_factor_of_n.rb b/test/medium/test_1492_the_kth_factor_of_n.rb new file mode 100644 index 00000000..ff9578f9 --- /dev/null +++ b/test/medium/test_1492_the_kth_factor_of_n.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1492_the_kth_factor_of_n' +require 'minitest/autorun' + +class TheKthFactorOfNTest < ::Minitest::Test + def test_default_one = assert_equal(3, kth_factor(12, 3)) + + def test_default_two = assert_equal(7, kth_factor(7, 2)) + + def test_default_three = assert_equal(-1, kth_factor(4, 4)) +end