Skip to content

Commit 1325556

Browse files
authored
2025-02-21 v. 8.6.6: added "1492. The kth Factor of n"
2 parents db2c835 + 847ff7c commit 1325556

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
699699
| 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) |
700700
| 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) |
701701
| 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) |
702+
| 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) |
702703
| 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) |
703704
| 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) |
704705
| 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) |

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.6.5'
8+
s.version = '8.6.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/the-kth-factor-of-n/
4+
# @param {Integer} n
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def kth_factor(n, k)
8+
factors = ::Array.new(k, 0)
9+
p = 0
10+
i = 1
11+
12+
while i <= n && p < k
13+
if (n % i).zero?
14+
factors[p] = i
15+
p += 1
16+
end
17+
18+
i += 1
19+
end
20+
21+
p < k ? -1 : factors.last
22+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1492_the_kth_factor_of_n'
5+
require 'minitest/autorun'
6+
7+
class TheKthFactorOfNTest < ::Minitest::Test
8+
def test_default_one = assert_equal(3, kth_factor(12, 3))
9+
10+
def test_default_two = assert_equal(7, kth_factor(7, 2))
11+
12+
def test_default_three = assert_equal(-1, kth_factor(4, 4))
13+
end

0 commit comments

Comments
 (0)