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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions 0101-0200/0173_binary_search_tree_iterator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
class BSTIterator
def initialize(root)
@in_order_nodes = [root.clone]
end

def next()
head = @in_order_nodes[0]
until head.nil? || head.left.nil?
head_left = head.left
head.left = nil
head = head_left
@in_order_nodes.unshift(head)
end

head = @in_order_nodes.shift
if head.right
@in_order_nodes.unshift(head.right)
end

head.val
end


def has_next()
!@in_order_nodes.empty?
end
end

# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator.new(root)
# param_1 = obj.next()
# param_2 = obj.has_next()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions 0201-0300/0233_number_of_digit_one.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# @param {Integer} n
# @return {Integer}
def count_digit_one(n)
# for each current digits d
# xx0xx0xdx00xx0xx
# base
# [left ] [ right]
#
# when d == 1, num of 1 is:
# [left ]100000000 + ==> ([left ]*base nums stick to d)
# 1x00xx0xx + ==> (x00xx0xx nums stick to d)
# 100000000 ==> (1 remain case)
# when d > 1, num of 1 is:
# [left ]100000000 + ==> ([left ]*base nums stick to d=1)
# 199999999 + ==> (99999999 nums stick to d=1)
# 100000000 ==> (1 remain case)
# when d == 0, num of 1 is:
# [left ]019999999 + ==> ([left ]*base nums stick to the next d=1)
#

sum = 0
power_of_ten = 1
while power_of_ten <= n
right = n % power_of_ten
left = n / (power_of_ten * 10)
curr = (n/ power_of_ten) % 10

left_base = left * power_of_ten
if curr == 1
sum += left_base + (right + 1)
elsif curr > 1
sum += left_base + power_of_ten
else
sum += left_base
end

power_of_ten *= 10
end

sum
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions 0301-0400/0350_intersection_of_two_arrays_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @param {Integer[]} nums1
# @param {Integer[]} nums2
# @return {Integer[]}
def intersect(nums1, nums2)
counter1 = nums1.tally
nums2.select { |num|
counter1.has_key?(num) && (counter1[num] -= 1) >= 0
}
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions 0601-0700/0700_search_in_a_binary_search_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @param {Integer} val
# @return {TreeNode}
def search_bst(root, val)
return if root.nil?
return root if root.val == val

if root.val > val
search_bst(root.left, val)
else
search_bst(root.right, val)
end
end


# non recursion
def search_bst(root, val)
while root do
return root if root.val == val

root = root.val > val ? root.left : root.right
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions 0901-1000/0995_minimum_number_of_k_consecutive_bit_flips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def min_k_bit_flips(nums, k)
flips_count = 0
# the current flip state at i = flip_state_at_i-k ^ flip_state_at_i-k+1 ^ ....flip_state_at_i-1
flipped = 0
(0...nums.length).each do |i|
flipped ^= (1-nums[i-k]) if i >= k
# ....1|0............
# .... i......i+k....
# whatever the start window i
# if the current state of nums[i] is 1, it must be flipped
# and if the current state of nums[i] is 0, it must not be flipped
nums[i] = flipped == 0 ? nums[i] : 1 - nums[i]

if i > nums.length - k
return -1 if nums[i] == 0
else
if nums[i] == 0
flipped ^= 1
flips_count += 1
end
end
end

flips_count
end
28 changes: 28 additions & 0 deletions 1001-1100/1038_binary_search_tree_to_greater_sum_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end

def sum(node, greater = 0)
return greater if node.nil?

sum_r = sum(node.right, greater)
node.val += sum_r
if node.left
sum(node.left, node.val)
else
node.val
end
end

# @param {TreeNode} root
# @return {TreeNode}
def bst_to_gst(root)
sum(root)
root
end
13 changes: 13 additions & 0 deletions 1301-1400/1332_remove_palindromic_subsequences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @param {String} s
# @return {Integer}
def remove_palindrome_sub(s)
l = 0
r = s.length - 1
while l < r
return 2 if s[l] != s[r]
l += 1
r -= 1
end

1
end
32 changes: 32 additions & 0 deletions 1301-1400/1382_balance_a_binary_search_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end

def in_order(node)
return [] if node.nil?

in_order(node.left) + [node] + in_order(node.right)
end

def build_bst(order_nodes)
return if order_nodes.empty?

m = order_nodes.length/2
root = order_nodes[m]
root.left = build_bst(order_nodes[0...m])
root.right = build_bst(order_nodes[m+1..])
root
end

# @param {TreeNode} root
# @return {TreeNode}
def balance_bst(root)
order_nodes = in_order(root)
build_bst(order_nodes)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @param {Integer[]} nums
# @param {Integer} limit
# @return {Integer}
def longest_subarray(nums, limit)
max, min = nums[0], nums[0]
l, r = 0, 0
longest = 0
loop do
d = (max - min).abs
if d <= limit
longest = [longest, r - l + 1].max
else
l += 1
max = nums[l..r].max if nums[l-1] == max
min = nums[l..r].min if nums[l-1] == min
end

r += 1
break if r >= nums.length

max = [max, nums[r]].max
min = [min, nums[r]].min
end

longest
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# @param {Integer[]} nums
# @return {Integer}
def min_difference(nums)
return 0 if nums.length < 5

nums.sort!

min_dist = Float::INFINITY
(0..3).each do |i|
min_dist = [min_dist, nums[nums.length - 4 + i] - nums[i]].min
end

min_dist
end
14 changes: 14 additions & 0 deletions 1501-1600/1518_water_bottles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# @param {Integer} num_bottles
# @param {Integer} num_exchange
# @return {Integer}
def num_water_bottles(num_bottles, num_exchange)
count = num_bottles
remain = 0
while num_bottles >= num_exchange
count += (exchange = num_bottles/num_exchange)
remain = num_bottles % num_exchange
num_bottles = exchange + remain
end

count
end
6 changes: 6 additions & 0 deletions 1501-1600/1528_shuffle_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @param {String} s
# @param {Integer[]} indices
# @return {String}
def restore_string(s, indices)
indices.zip(s.chars).sort_by(&:first).map(&:last).join
end
5 changes: 5 additions & 0 deletions 1501-1600/1550_three_consecutive_odds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @param {Integer[]} arr
# @return {Boolean}
def three_consecutive_odds(arr)
arr.each_cons(3).any? { |x, y, z| x.odd? and y.odd? and z.odd? }
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class UnionFind
def initialize(n)
@linked = (0..n).to_a
end

def find(x)
@linked[x] = find(@linked[x]) if x != @linked[x]
@linked[x]
end

def union(x, y)
return if (x_set = find(x)) == (y_set = find(y))

@linked[x_set] = @linked[y_set]
end

def num_of_sets
@linked[1..].map { |x| find(x) }.tally.size
end
end

# @param {Integer} n
# @param {Integer[][]} edges
# @return {Integer}
def max_num_edges_to_remove(n, edges)
adj = Array.new(3) { [] }
edges.each { |type, u, v|
adj[type-1] << [u, v]
}

removed_edges = 0
alice_uf = UnionFind.new(n)
bob_uf = UnionFind.new(n)

adj[2].each { |u, v|
a = alice_uf.union(u, v)
b = bob_uf.union(u, v)
removed_edges += 1 unless a && b
}

adj[1].each { |u, v|
removed_edges += 1 unless bob_uf.union(u, v)
}

adj[0].each { |u, v|
removed_edges += 1 unless alice_uf.union(u, v)
}

(alice_uf.num_of_sets == 1 && bob_uf.num_of_sets == 1) ? removed_edges : -1
end
5 changes: 5 additions & 0 deletions 1701-1800/1791_find_center_of_star_graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @param {Integer[][]} edges
# @return {Integer}
def find_center(edges)
(edges[0] & edges[1]).first
end
Loading