diff --git a/README.md b/README.md index 29d3c68b..834ba621 100644 --- a/README.md +++ b/README.md @@ -688,6 +688,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1376. Time Needed to Inform All Employees | [Link](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [Link](./lib/medium/1376_time_needed_to_inform_all_employees.rb) | [Link](./test/medium/test_1376_time_needed_to_inform_all_employees.rb) | | 1381. Design a Stack With Increment Operation | [Link](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [Link](./lib/medium/1381_design_a_stack_with_increment_operation.rb) | [Link](./test/medium/test_1381_design_a_stack_with_increment_operation.rb) | | 1382. Balance a Binary Search Tree | [Link](https://leetcode.com/problems/balance-a-binary-search-tree/) | [Link](./lib/medium/1382_balance_a_binary_search_tree.rb) | [Link](./test/medium/test_1382_balance_a_binary_search_tree.rb) | +| 1387. Sort Integers by The Power Value | [Link](https://leetcode.com/problems/sort-integers-by-the-power-value/) | [Link](./lib/medium/1387_sort_integers_by_the_power_value.rb) | [Link](./test/medium/test_1387_sort_integers_by_the_power_value.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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 43860d89..cbcd8a2c 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.5.5' + s.version = '8.5.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1387_sort_integers_by_the_power_value.rb b/lib/medium/1387_sort_integers_by_the_power_value.rb new file mode 100644 index 00000000..a3919255 --- /dev/null +++ b/lib/medium/1387_sort_integers_by_the_power_value.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/sort-integers-by-the-power-value/ +# @param {Integer} lo +# @param {Integer} hi +# @param {Integer} k +# @return {Integer} +def get_kth(lo, hi, k) + powers = [] + + (lo..hi).each do |i| + num = i + power = 0 + + until num == 1 + num = num.even? ? num / 2 : num * 3 + 1 + power += 1 + end + + powers << [i, power] + end + + powers.sort_by! { |o| o[1] } + + powers[k - 1][0] +end diff --git a/test/medium/test_1387_sort_integers_by_the_power_value.rb b/test/medium/test_1387_sort_integers_by_the_power_value.rb new file mode 100644 index 00000000..205d1740 --- /dev/null +++ b/test/medium/test_1387_sort_integers_by_the_power_value.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1387_sort_integers_by_the_power_value' +require 'minitest/autorun' + +class SortIntegersByThePowerValueTest < ::Minitest::Test + def test_default_one = assert_equal(13, get_kth(12, 15, 2)) + + def test_default_two = assert_equal(7, get_kth(7, 11, 4)) +end