Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 11 additions & 15 deletions lib/easy/501_find_mode_in_binary_search_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,41 @@
# @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)

result
end

$prev = -1
$count = 1
$max = -1

# @param {TreeNode} node
# @param {Integer[]} modes
def _traverse(node, modes)
return if node.nil?

_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
8 changes: 3 additions & 5 deletions lib/easy/543_diameter_of_binary_tree.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
8 changes: 3 additions & 5 deletions lib/easy/563_binary_tree_tilt.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
20 changes: 10 additions & 10 deletions lib/easy/671_second_minimum_node_in_a_binary_tree.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/easy/897_increasing_order_search_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
# @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)
return if node.nil?

inorder_iost(node.left)
node.left = nil
$curr.right = node
$curr = node
@curr.right = node
@curr = node
inorder_iost(node.right)
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions lib/medium/508_most_frequent_subtree_sum.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
15 changes: 6 additions & 9 deletions lib/medium/513_find_bottom_left_tree_value.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions lib/medium/535_encode_and_decode_tinyurl.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
8 changes: 3 additions & 5 deletions lib/medium/538_convert_bst_to_greater_tree.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/easy/test_278_first_bad_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions test/easy/test_374_guess_number_higher_or_lower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading