Skip to content

Commit b1f5578

Browse files
authored
Merge pull request neetcode-gh#409 from TedTran2019/ruby-solutions
Ruby solutions to 23** more problems
2 parents 32d6eeb + 6f1e35e commit b1f5578

22 files changed

+453
-0
lines changed

ruby/100-Same-Tree.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def is_same_tree(p, q)
2+
if p.nil? && q.nil?
3+
true
4+
elsif p && q
5+
(p.val == q.val) &&
6+
is_same_tree(p.left, q.left) &&
7+
is_same_tree(p.right, q.right)
8+
else
9+
false
10+
end
11+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def max_depth(root)
2+
return 0 if root.nil?
3+
return 1 if root.left.nil? && root.right.nil?
4+
5+
left = 1 + max_depth(root.left)
6+
right = 1 + max_depth(root.right)
7+
left > right ? left : right
8+
end

ruby/1046-Last-Stone-Weight.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'rubygems'
2+
require 'algorithms'
3+
include Containers
4+
5+
def last_stone_weight(stones)
6+
heap = MaxHeap.new
7+
stones.each { |stone| heap << stone }
8+
until heap.size <= 1
9+
stone1 = heap.pop
10+
stone2 = heap.pop
11+
heap << (stone1 - stone2).abs if stone1 != stone2
12+
end
13+
last = heap.pop
14+
last.nil? ? 0 : last
15+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def max_area(height)
2+
idx_start = 0
3+
idx_end = height.length - 1
4+
max_water = 0
5+
while idx_start < idx_end
6+
challenger = (height[idx_start] > height[idx_end] ? height[idx_end] : height[idx_start])
7+
challenger *= (idx_end - idx_start)
8+
max_water = challenger if challenger > max_water
9+
10+
if height[idx_start] > height[idx_end]
11+
idx_end -= 1
12+
else
13+
idx_start += 1
14+
end
15+
end
16+
max_water
17+
end

ruby/110-Balanced-Binary-Tree.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def is_balanced(root)
2+
$balance = true
3+
balanced?(root)
4+
$balance
5+
end
6+
7+
def balanced?(root)
8+
return -1 if root.nil?
9+
10+
left_height = 1 + balanced?(root.left)
11+
right_height = 1 + balanced?(root.right)
12+
$balance = false if (left_height - right_height).abs > 1
13+
14+
left_height > right_height ? left_height : right_height
15+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def max_profit(prices)
2+
max_profit = 0
3+
min = prices.first
4+
prices.each do |price|
5+
min = price if price < min
6+
profit = price - min
7+
max_profit = profit if profit > max_profit
8+
end
9+
max_profit
10+
end

ruby/125-Valid-Palindrome.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def is_palindrome(s)
2+
str = s.downcase.chars.select { |char| /[a-zA-Z0-9]/.match?(char) }.join
3+
idx_start = 0
4+
idx_end = str.length - 1
5+
while idx_start < idx_end
6+
return false if str[idx_start] != str[idx_end]
7+
8+
idx_start += 1
9+
idx_end -= 1
10+
end
11+
true
12+
end
13+
14+
def is_palindrome(s)
15+
idx_start = 0
16+
idx_end = s.length - 1
17+
while idx_start < idx_end
18+
if !/[a-zA-Z0-9]/.match?(s[idx_start])
19+
idx_start += 1
20+
elsif !/[a-zA-Z0-9]/.match?(s[idx_end])
21+
idx_end -= 1
22+
else
23+
return false if s[idx_start].downcase != s[idx_end].downcase
24+
25+
idx_start += 1
26+
idx_end -= 1
27+
end
28+
end
29+
true
30+
end
31+
32+
def is_palindrome(s)
33+
str = s.downcase.chars.select { |char| /[a-zA-Z0-9]/.match?(char) }
34+
str == str.reverse
35+
end

ruby/141-Linked-List-Cycle.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def hasCycle(head)
2+
return false if head.nil?
3+
return false if head.next.nil?
4+
5+
dict = {}
6+
while head
7+
return true if dict.key?(head)
8+
9+
dict[head] = true
10+
head = head.next
11+
end
12+
false
13+
end
14+
15+
# Tortoise and Hare solution
16+
def hasCycle(head)
17+
return false if head.nil?
18+
return false if head.next.nil?
19+
20+
fast = head
21+
while head && fast
22+
fast = fast.next
23+
return false if fast.nil?
24+
25+
fast = fast.next
26+
head = head.next
27+
28+
return true if head == fast
29+
end
30+
false
31+
end

ruby/15-3Sum.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def three_sum(nums)
2+
sums = []
3+
nums.sort! # This is to get rid of duplicate solutions
4+
nums.each_with_index do |num, idx|
5+
next if idx.positive? && num == nums[idx - 1]
6+
7+
left = idx + 1
8+
right = nums.length - 1
9+
while left < right
10+
case num + nums[left] + nums[right] <=> 0
11+
when 1
12+
right -= 1
13+
when 0
14+
sums << [num, nums[left], nums[right]]
15+
left += 1
16+
left += 1 while nums[left] == nums[left - 1] && left < right
17+
when -1
18+
left += 1
19+
end
20+
end
21+
end
22+
sums
23+
end

ruby/155-Min-Stack.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class MinStack
2+
def initialize
3+
@stack = []
4+
end
5+
6+
def push(val)
7+
min = if @stack.empty?
8+
val
9+
else
10+
val < @stack.last[1] ? val : @stack.last[1]
11+
end
12+
13+
@stack << [val, min]
14+
nil
15+
end
16+
17+
def pop
18+
@stack.pop
19+
nil
20+
end
21+
22+
def top
23+
return nil if @stack.empty?
24+
25+
@stack.last[0]
26+
end
27+
28+
def get_min
29+
return nil if @stack.empty?
30+
31+
@stack.last[1]
32+
end
33+
end

0 commit comments

Comments
 (0)