Skip to content

Commit fc2bdbc

Browse files
authored
2024-12-18 v. 7.3.9.1: refactored some solutions
2 parents 8e3209b + 54e398e commit fc2bdbc

13 files changed

+58
-72
lines changed

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 = '7.3.9'
8+
s.version = '7.3.9.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/easy/501_find_mode_in_binary_search_tree.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,41 @@
44
# @param {TreeNode} root
55
# @return {Integer[]}
66
def find_mode(root)
7-
$prev = -1
8-
$count = 1
9-
$max = -1
7+
@prev = -1
8+
@count = 1
9+
@max = -1
1010

1111
result = []
1212
_traverse(root, result)
1313

1414
result
1515
end
1616

17-
$prev = -1
18-
$count = 1
19-
$max = -1
20-
2117
# @param {TreeNode} node
2218
# @param {Integer[]} modes
2319
def _traverse(node, modes)
2420
return if node.nil?
2521

2622
_traverse(node.left, modes)
2723

28-
if $prev != -1
29-
if $prev == node.val
30-
$count += 1
24+
if @prev != -1
25+
if @prev == node.val
26+
@count += 1
3127
else
32-
$count = 1
28+
@count = 1
3329
end
3430
end
3531

3632
# noinspection RubyMismatchedArgumentType
37-
if $count > $max
38-
$max = $count
33+
if @count > @max
34+
@max = @count
3935
modes.clear
4036
modes << node.val
41-
elsif $count == $max
37+
elsif @count == @max
4238
modes << node.val
4339
end
4440

45-
$prev = node.val
41+
@prev = node.val
4642

4743
_traverse(node.right, modes)
4844
end
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# frozen_string_literal: true
22

3-
$max = 0
4-
53
# https://leetcode.com/problems/diameter-of-binary-tree/
64
# @param {TreeNode} root
75
# @return {Integer}
86
def diameter_of_binary_tree(root)
9-
$max = 0
7+
@max = 0
108
calc_max_depth(root)
119

12-
$max
10+
@max
1311
end
1412

1513
# @param {TreeNode} node
@@ -18,7 +16,7 @@ def calc_max_depth(node)
1816

1917
left = calc_max_depth(node.left)
2018
right = calc_max_depth(node.right)
21-
$max = [$max, left + right].max
19+
@max = [@max, left + right].max
2220

2321
[left, right].max + 1
2422
end

lib/easy/563_binary_tree_tilt.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# frozen_string_literal: true
22

3-
$tilt = 0
4-
53
# https://leetcode.com/problems/binary-tree-tilt/
64
# @param {TreeNode} root
75
# @return {Integer}
86
def find_tilt(root)
9-
$tilt = 0
7+
@tilt = 0
108
calc_tilt(root)
119

12-
$tilt
10+
@tilt
1311
end
1412

1513
# @param {TreeNode} node
@@ -20,7 +18,7 @@ def calc_tilt(node)
2018

2119
left = calc_tilt(node.left)
2220
right = calc_tilt(node.right)
23-
$tilt += (left - right).abs
21+
@tilt += (left - right).abs
2422

2523
left + right + node.val
2624
end

lib/easy/671_second_minimum_node_in_a_binary_tree.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# frozen_string_literal: true
22

3-
$r_max = 1 << (1 << 16)
4-
$f_min = 0
5-
$s_min = $max
6-
73
# https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
84
# @param {TreeNode} root
95
# @return {Integer}
106
def find_second_minimum_value(root)
11-
$f_min = root.val
12-
$s_min = $r_max
7+
@r_max = 1 << (1 << 16)
8+
@f_min = 0
9+
@s_min = @max
10+
11+
@f_min = root.val
12+
@s_min = @r_max
1313
find_second_minimum(root)
1414

15-
$s_min < $r_max ? $s_min : -1
15+
@s_min < @r_max ? @s_min : -1
1616
end
1717

1818
# @param {TreNode} node
1919
def find_second_minimum(node)
2020
return if node.nil?
2121

22-
if $f_min < node.val && node.val < $s_min
23-
$s_min = node.val
24-
elsif $f_min == node.val
22+
if @f_min < node.val && node.val < @s_min
23+
@s_min = node.val
24+
elsif @f_min == node.val
2525
find_second_minimum(node.left)
2626
find_second_minimum(node.right)
2727
end

lib/easy/897_increasing_order_search_tree.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
# @return {TreeNode}
88
def increasing_bst(root)
99
result = ::TreeNode.new
10-
$curr = result
10+
@curr = result
1111
inorder_iost(root)
1212

1313
result.right
1414
end
1515

16-
$curr = nil
16+
private
1717

1818
# @param {TreeNode} node
1919
def inorder_iost(node)
2020
return if node.nil?
2121

2222
inorder_iost(node.left)
2323
node.left = nil
24-
$curr.right = node
25-
$curr = node
24+
@curr.right = node
25+
@curr = node
2626
inorder_iost(node.right)
2727
end

lib/medium/105_construct_binary_tree_from_preorder_and_inorder_traversal.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
require_relative '../common/binary_tree'
44

5-
$pre_index = 0
6-
75
# https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
86
# @param {Integer[]} preorder
97
# @param {Integer[]} inorder
108
# @return {TreeNode}
119
def build_tree(preorder, inorder)
12-
$pre_index = 0
10+
@pre_index = 0
1311
in_index = {}
14-
(0...inorder.size).each do |i|
15-
in_index[inorder[i]] = i
16-
end
12+
(0...inorder.size).each { |i| in_index[inorder[i]] = i }
1713

1814
array_to_tree(preorder, 0, preorder.size - 1, in_index)
1915
end
@@ -27,8 +23,8 @@ def build_tree(preorder, inorder)
2723
def array_to_tree(pre, l, r, in_index)
2824
return if l > r
2925

30-
val = pre[$pre_index]
31-
$pre_index += 1
26+
val = pre[@pre_index]
27+
@pre_index += 1
3228

3329
root = ::TreeNode.new(val)
3430
root.left = array_to_tree(pre, l, in_index[val] - 1, in_index)

lib/medium/508_most_frequent_subtree_sum.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
# frozen_string_literal: true
22

3-
$freq = 0
4-
53
# https://leetcode.com/problems/most-frequent-subtree-sum/
64
# @param {TreeNode} root
75
# @return {Integer[]}
86
def find_frequent_tree_sum(root)
9-
$freq = 0
7+
@freq = 0
108

119
values = {}
1210
fill_values_from_sub_tree(root, values)
1311

1412
most_freq = []
15-
values.entries.each { |key, value| most_freq << key if value == $freq }
13+
values.entries.each { |key, value| most_freq << key if value == @freq }
1614

1715
most_freq
1816
end
@@ -30,7 +28,7 @@ def fill_values_from_sub_tree(node, values)
3028
sum = node.val + left + right
3129

3230
values[sum] = values.fetch(sum, 0) + 1
33-
$freq = [$freq, values[sum]].max
31+
@freq = [@freq, values[sum]].max
3432

3533
sum
3634
end

lib/medium/513_find_bottom_left_tree_value.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
# frozen_string_literal: true
22

3-
$path = -1
4-
$value = 0
5-
63
# https://leetcode.com/problems/find-bottom-left-tree-value/
74
# @param {TreeNode} root
85
# @return {Integer}
96
def find_bottom_left_value(root)
10-
$path = -1
11-
$value = 0
7+
@path = -1
8+
@value = 0
129

1310
apply_to_find_bottom_left_value(root, 0)
1411

15-
$value
12+
@value
1613
end
1714

1815
private
@@ -23,9 +20,9 @@ def find_bottom_left_value(root)
2320
def apply_to_find_bottom_left_value(node, curr)
2421
return unless node
2522

26-
if $path < curr
27-
$path = curr
28-
$value = node.val
23+
if @path < curr
24+
@path = curr
25+
@value = node.val
2926
end
3027

3128
apply_to_find_bottom_left_value(node.left, curr + 1)

lib/medium/535_encode_and_decode_tinyurl.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# frozen_string_literal: true
22

3-
$urls = {}
4-
53
# https://leetcode.com/problems/encode-and-decode-tinyurl/
64
# @param {String} long_url
75
# @return {String}
86
def encode(long_url)
7+
@urls = {}
8+
99
short_url = generate_short_url(long_url)
10-
short_url = generate_short_url(long_url) while $urls.include?(short_url)
10+
short_url = generate_short_url(long_url) while @urls.include?(short_url)
1111

12-
$urls[short_url] = long_url
12+
@urls[short_url] = long_url
1313

1414
short_url
1515
end
@@ -27,4 +27,4 @@ def generate_short_url(_long_url)
2727

2828
# @param {String} short_url
2929
# @return {String}
30-
def decode(short_url) = $urls[short_url]
30+
def decode(short_url) = @urls[short_url]

0 commit comments

Comments
 (0)