diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 30a001f9..3e840194 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 = '7.3.9' + s.version = '7.3.9.1' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/easy/501_find_mode_in_binary_search_tree.rb b/lib/easy/501_find_mode_in_binary_search_tree.rb index 6a1721d9..a6fe1e6f 100644 --- a/lib/easy/501_find_mode_in_binary_search_tree.rb +++ b/lib/easy/501_find_mode_in_binary_search_tree.rb @@ -4,9 +4,9 @@ # @param {TreeNode} root # @return {Integer[]} def find_mode(root) - $prev = -1 - $count = 1 - $max = -1 + @prev = -1 + @count = 1 + @max = -1 result = [] _traverse(root, result) @@ -14,10 +14,6 @@ def find_mode(root) result end -$prev = -1 -$count = 1 -$max = -1 - # @param {TreeNode} node # @param {Integer[]} modes def _traverse(node, modes) @@ -25,24 +21,24 @@ def _traverse(node, modes) _traverse(node.left, modes) - if $prev != -1 - if $prev == node.val - $count += 1 + if @prev != -1 + if @prev == node.val + @count += 1 else - $count = 1 + @count = 1 end end # noinspection RubyMismatchedArgumentType - if $count > $max - $max = $count + if @count > @max + @max = @count modes.clear modes << node.val - elsif $count == $max + elsif @count == @max modes << node.val end - $prev = node.val + @prev = node.val _traverse(node.right, modes) end diff --git a/lib/easy/543_diameter_of_binary_tree.rb b/lib/easy/543_diameter_of_binary_tree.rb index b8f8fba3..837b63bf 100644 --- a/lib/easy/543_diameter_of_binary_tree.rb +++ b/lib/easy/543_diameter_of_binary_tree.rb @@ -1,15 +1,13 @@ # frozen_string_literal: true -$max = 0 - # https://leetcode.com/problems/diameter-of-binary-tree/ # @param {TreeNode} root # @return {Integer} def diameter_of_binary_tree(root) - $max = 0 + @max = 0 calc_max_depth(root) - $max + @max end # @param {TreeNode} node @@ -18,7 +16,7 @@ def calc_max_depth(node) left = calc_max_depth(node.left) right = calc_max_depth(node.right) - $max = [$max, left + right].max + @max = [@max, left + right].max [left, right].max + 1 end diff --git a/lib/easy/563_binary_tree_tilt.rb b/lib/easy/563_binary_tree_tilt.rb index 1a67923e..d626f2ea 100644 --- a/lib/easy/563_binary_tree_tilt.rb +++ b/lib/easy/563_binary_tree_tilt.rb @@ -1,15 +1,13 @@ # frozen_string_literal: true -$tilt = 0 - # https://leetcode.com/problems/binary-tree-tilt/ # @param {TreeNode} root # @return {Integer} def find_tilt(root) - $tilt = 0 + @tilt = 0 calc_tilt(root) - $tilt + @tilt end # @param {TreeNode} node @@ -20,7 +18,7 @@ def calc_tilt(node) left = calc_tilt(node.left) right = calc_tilt(node.right) - $tilt += (left - right).abs + @tilt += (left - right).abs left + right + node.val end diff --git a/lib/easy/671_second_minimum_node_in_a_binary_tree.rb b/lib/easy/671_second_minimum_node_in_a_binary_tree.rb index 796e3eb4..e70dd082 100644 --- a/lib/easy/671_second_minimum_node_in_a_binary_tree.rb +++ b/lib/easy/671_second_minimum_node_in_a_binary_tree.rb @@ -1,27 +1,27 @@ # frozen_string_literal: true -$r_max = 1 << (1 << 16) -$f_min = 0 -$s_min = $max - # https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/ # @param {TreeNode} root # @return {Integer} def find_second_minimum_value(root) - $f_min = root.val - $s_min = $r_max + @r_max = 1 << (1 << 16) + @f_min = 0 + @s_min = @max + + @f_min = root.val + @s_min = @r_max find_second_minimum(root) - $s_min < $r_max ? $s_min : -1 + @s_min < @r_max ? @s_min : -1 end # @param {TreNode} node def find_second_minimum(node) return if node.nil? - if $f_min < node.val && node.val < $s_min - $s_min = node.val - elsif $f_min == node.val + if @f_min < node.val && node.val < @s_min + @s_min = node.val + elsif @f_min == node.val find_second_minimum(node.left) find_second_minimum(node.right) end diff --git a/lib/easy/897_increasing_order_search_tree.rb b/lib/easy/897_increasing_order_search_tree.rb index ff42d470..0f95f5ea 100644 --- a/lib/easy/897_increasing_order_search_tree.rb +++ b/lib/easy/897_increasing_order_search_tree.rb @@ -7,13 +7,13 @@ # @return {TreeNode} def increasing_bst(root) result = ::TreeNode.new - $curr = result + @curr = result inorder_iost(root) result.right end -$curr = nil +private # @param {TreeNode} node def inorder_iost(node) @@ -21,7 +21,7 @@ def inorder_iost(node) inorder_iost(node.left) node.left = nil - $curr.right = node - $curr = node + @curr.right = node + @curr = node inorder_iost(node.right) end diff --git a/lib/medium/105_construct_binary_tree_from_preorder_and_inorder_traversal.rb b/lib/medium/105_construct_binary_tree_from_preorder_and_inorder_traversal.rb index e54d8edc..168cc91e 100644 --- a/lib/medium/105_construct_binary_tree_from_preorder_and_inorder_traversal.rb +++ b/lib/medium/105_construct_binary_tree_from_preorder_and_inorder_traversal.rb @@ -2,18 +2,14 @@ require_relative '../common/binary_tree' -$pre_index = 0 - # https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ # @param {Integer[]} preorder # @param {Integer[]} inorder # @return {TreeNode} def build_tree(preorder, inorder) - $pre_index = 0 + @pre_index = 0 in_index = {} - (0...inorder.size).each do |i| - in_index[inorder[i]] = i - end + (0...inorder.size).each { |i| in_index[inorder[i]] = i } array_to_tree(preorder, 0, preorder.size - 1, in_index) end @@ -27,8 +23,8 @@ def build_tree(preorder, inorder) def array_to_tree(pre, l, r, in_index) return if l > r - val = pre[$pre_index] - $pre_index += 1 + val = pre[@pre_index] + @pre_index += 1 root = ::TreeNode.new(val) root.left = array_to_tree(pre, l, in_index[val] - 1, in_index) diff --git a/lib/medium/508_most_frequent_subtree_sum.rb b/lib/medium/508_most_frequent_subtree_sum.rb index 314d79dd..7d43ab94 100644 --- a/lib/medium/508_most_frequent_subtree_sum.rb +++ b/lib/medium/508_most_frequent_subtree_sum.rb @@ -1,18 +1,16 @@ # frozen_string_literal: true -$freq = 0 - # https://leetcode.com/problems/most-frequent-subtree-sum/ # @param {TreeNode} root # @return {Integer[]} def find_frequent_tree_sum(root) - $freq = 0 + @freq = 0 values = {} fill_values_from_sub_tree(root, values) most_freq = [] - values.entries.each { |key, value| most_freq << key if value == $freq } + values.entries.each { |key, value| most_freq << key if value == @freq } most_freq end @@ -30,7 +28,7 @@ def fill_values_from_sub_tree(node, values) sum = node.val + left + right values[sum] = values.fetch(sum, 0) + 1 - $freq = [$freq, values[sum]].max + @freq = [@freq, values[sum]].max sum end diff --git a/lib/medium/513_find_bottom_left_tree_value.rb b/lib/medium/513_find_bottom_left_tree_value.rb index e52216cb..f7e4476b 100644 --- a/lib/medium/513_find_bottom_left_tree_value.rb +++ b/lib/medium/513_find_bottom_left_tree_value.rb @@ -1,18 +1,15 @@ # frozen_string_literal: true -$path = -1 -$value = 0 - # https://leetcode.com/problems/find-bottom-left-tree-value/ # @param {TreeNode} root # @return {Integer} def find_bottom_left_value(root) - $path = -1 - $value = 0 + @path = -1 + @value = 0 apply_to_find_bottom_left_value(root, 0) - $value + @value end private @@ -23,9 +20,9 @@ def find_bottom_left_value(root) def apply_to_find_bottom_left_value(node, curr) return unless node - if $path < curr - $path = curr - $value = node.val + if @path < curr + @path = curr + @value = node.val end apply_to_find_bottom_left_value(node.left, curr + 1) diff --git a/lib/medium/535_encode_and_decode_tinyurl.rb b/lib/medium/535_encode_and_decode_tinyurl.rb index a6286985..69f818c4 100644 --- a/lib/medium/535_encode_and_decode_tinyurl.rb +++ b/lib/medium/535_encode_and_decode_tinyurl.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true -$urls = {} - # https://leetcode.com/problems/encode-and-decode-tinyurl/ # @param {String} long_url # @return {String} def encode(long_url) + @urls = {} + short_url = generate_short_url(long_url) - short_url = generate_short_url(long_url) while $urls.include?(short_url) + short_url = generate_short_url(long_url) while @urls.include?(short_url) - $urls[short_url] = long_url + @urls[short_url] = long_url short_url end @@ -27,4 +27,4 @@ def generate_short_url(_long_url) # @param {String} short_url # @return {String} -def decode(short_url) = $urls[short_url] +def decode(short_url) = @urls[short_url] diff --git a/lib/medium/538_convert_bst_to_greater_tree.rb b/lib/medium/538_convert_bst_to_greater_tree.rb index 24c75753..63f0ed63 100644 --- a/lib/medium/538_convert_bst_to_greater_tree.rb +++ b/lib/medium/538_convert_bst_to_greater_tree.rb @@ -1,12 +1,10 @@ # frozen_string_literal: true -$sum = 0 - # https://leetcode.com/problems/convert-bst-to-greater-tree/ # @param {TreeNode} root # @return {TreeNode} def convert_bst(root) - $sum = 0 + @sum = 0 convert_bst_with_sum(root) end @@ -19,8 +17,8 @@ def convert_bst_with_sum(node) return unless node convert_bst_with_sum(node.right) - $sum += node.val - node.val = $sum + @sum += node.val + node.val = @sum convert_bst_with_sum(node.left) node diff --git a/test/easy/test_278_first_bad_version.rb b/test/easy/test_278_first_bad_version.rb index ba554c8f..a315b87a 100644 --- a/test/easy/test_278_first_bad_version.rb +++ b/test/easy/test_278_first_bad_version.rb @@ -7,11 +7,13 @@ class FirstBadVersionTest < ::Minitest::Test def test_default_one $b_version = 4 + assert_equal(4, first_bad_version(5)) end def test_default_two $b_version = 1 + assert_equal(1, first_bad_version(1)) end end diff --git a/test/easy/test_374_guess_number_higher_or_lower.rb b/test/easy/test_374_guess_number_higher_or_lower.rb index c5069374..b328d39b 100644 --- a/test/easy/test_374_guess_number_higher_or_lower.rb +++ b/test/easy/test_374_guess_number_higher_or_lower.rb @@ -7,16 +7,19 @@ class GuessNumberHigherOrLowerTest < ::Minitest::Test def test_default_one $guess_num = 6 + assert_equal(6, guess_number(10)) end def test_default_two $guess_num = 1 + assert_equal(1, guess_number(1)) end def test_default_three $guess_num = 1 + assert_equal(1, guess_number(2)) end end