Skip to content

Commit ae36756

Browse files
authored
2025-02-14 v. 8.5.6: added "1382. Balance a Binary Search Tree"
2 parents 17e6065 + 502d771 commit ae36756

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
688688
| 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) |
689689
| 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) |
690690
| 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) |
691+
| 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) |
691692
| 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) |
692693
| 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) |
693694
| 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.5.5'
8+
s.version = '8.5.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/sort-integers-by-the-power-value/
4+
# @param {Integer} lo
5+
# @param {Integer} hi
6+
# @param {Integer} k
7+
# @return {Integer}
8+
def get_kth(lo, hi, k)
9+
powers = []
10+
11+
(lo..hi).each do |i|
12+
num = i
13+
power = 0
14+
15+
until num == 1
16+
num = num.even? ? num / 2 : num * 3 + 1
17+
power += 1
18+
end
19+
20+
powers << [i, power]
21+
end
22+
23+
powers.sort_by! { |o| o[1] }
24+
25+
powers[k - 1][0]
26+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1387_sort_integers_by_the_power_value'
5+
require 'minitest/autorun'
6+
7+
class SortIntegersByThePowerValueTest < ::Minitest::Test
8+
def test_default_one = assert_equal(13, get_kth(12, 15, 2))
9+
10+
def test_default_two = assert_equal(7, get_kth(7, 11, 4))
11+
end

0 commit comments

Comments
 (0)