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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions 0401-0500/0410_split_array_largest_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def split_array(nums, k)
# count number of sub-array that has sum <= largest-sum
count_sub_arrs = lambda { |largest_sum|
count = 0
sum = 0
nums.each { |num|
if sum + num <= largest_sum
sum += num
else
count += 1
sum = num
end
}

count
}

# the smaller the largest-sum is, the more sub-arrays we can divide
# so we need to find the max largest-sum that we can divide nums into more than or equal k sub-arrays
# that is also the min largest-sum we can divide nums into k sub-arrays

low = nums.max
high = nums.sum + 1

while low < high
mid = (low + high)/2
count = count_sub_arrs.call(mid)

if count >= k # still be able to divide nums more than k sub-arrays
low = mid + 1
else
high = mid
end
end

low
end
18 changes: 18 additions & 0 deletions 0401-0500/0447_number_of_boomerangs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @param {Integer[][]} points
# @return {Integer}
def number_of_boomerangs(points)
count = 0
(0...points.length).each { |i|
dist_counter = Hash.new(0)
(0...points.length).each { |j|
dist = (points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2
# for each new boomerang point j, there're 2 boomerangs combine by j and each previous boomerang points
count += 2 * dist_counter[dist]
dist_counter[dist] += 1
}
# we also can count boomerangs each i by sum of Permutation(k, 2)
# count += dist_counter.values.reduce(0) { |sum, k| sum += k*(k-1) }
}

count
end
20 changes: 20 additions & 0 deletions 0501-0600/0532_k_diff_pairs_in_an_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def find_pairs(nums, k)
count = 0
appear = Hash.new(0)

nums.each { |num|
next if appear[num] >= (k == 0 ? 2 : 1)

if k == 0
count += appear[num] == 1 ? 1 : 0
else
count += (appear[num-k] >= 1 ? 1 : 0) + (appear[num+k] >= 1 ? 1 : 0)
end
appear[num] += 1
}

count
end
23 changes: 23 additions & 0 deletions 0501-0600/0551_student_attendance_record_i.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @param {String} s
# @return {Boolean}
def check_record(s)
a_count = 0
l_count = 0
l_prev = -1
(0...s.length).each { |i|
if s[i] == 'A'
a_count += 1
return false if a_count >= 2
elsif s[i] == 'L'
if l_prev == i - 1
l_count += 1
return false if l_count >= 3
else
l_count = 1
end
l_prev = i
end
}

true
end
45 changes: 45 additions & 0 deletions 0601-0700/0640_solve_the_equation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# @param {String} equation
# @return {String}
def solve_equation(equation)
# summary: ax + b
count_x = lambda { |str|
a = 0
b = 0
num = nil
sign = 1
str.each_char { |c|
if c == 'x'
a += sign * (num || 1)
num = 0
elsif c == '+'
b += sign * num.to_i
num = nil
sign = 1
elsif c == '-'
b += sign * num.to_i
num = nil
sign = -1
else
num = (num || 0) * 10 + c.to_i
end
}
b += sign * num.to_i

return [a, b]
}

left, right = equation.split('=')
al, bl = count_x.call(left)
ar, br = count_x.call(right)

a = al - ar
b = br - bl

if a == 0
b == 0 ? "Infinite solutions" : "No solution"
elsif b % a != 0
"No solution"
else
"x=#{b/a}"
end
end
22 changes: 22 additions & 0 deletions 0801-0900/0811_subdomain_visit_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# @param {String[]} cpdomains
# @return {String[]}
def subdomain_visits(cpdomains)
counter = Hash.new(0)
cpdomains.each { |cp|
count, domain = cp.split(' ')
count = count.to_i

i = domain.length
loop do
i = domain.rindex('.', i-1)
break if i.nil?

counter[domain[i+1..]] += count
end
counter[domain] += count
}

counter.map { |domain, count|
"#{count} #{domain}"
}
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @param {String[]} queries
# @param {String[]} words
# @return {Integer[]}
def num_smaller_by_frequency(queries, words)
f = lambda { |str|
count = 0
curr = 'z'
str.each_char { |c|
if curr == c
count += 1
elsif curr.ord > c.ord
curr = c
count = 1
end
}
count
}

word_sorted = words.map { |word| f.call(word) }.sort

ans = []
queries.each { |q|
freq = f.call(q)
if i = word_sorted.bsearch_index { |w_freq| w_freq > freq }
ans << (word_sorted.length - i)
else
ans << 0
end
}

ans
end
21 changes: 21 additions & 0 deletions 2101-2200/2134_minimum_swaps_to_group_all_1s_together_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @param {Integer[]} nums
# @return {Integer}
def min_swaps(nums)
# sliding-window size = total ones
total_ones = nums.count { |num| num == 1 }
# circular
nums += nums
# number of swaps = number of zeros
zeros = 0
(0...total_ones).each { |i|
zeros += (nums[i] == 0 ? 1 : 0)
}
min = zeros

(total_ones...nums.length).each { |i|
zeros += (nums[i] == 0 ? 1 : 0) - (nums[i-total_ones] == 0 ? 1 : 0)
min = [min, zeros].min
}

min
end
5 changes: 5 additions & 0 deletions 2601-2700/2678_number_of_senior_citizens.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @param {String[]} details
# @return {Integer}
def count_seniors(details)
details.count { |d| d[-4..-3].to_i > 60 }
end
30 changes: 30 additions & 0 deletions 3201-3300/3217_delete_nodes_from-linked_list_present_in_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# @param {Integer[]} nums
# @param {ListNode} head
# @return {ListNode}
def modified_list(nums, head)
nums_hash = {}
nums.each { |num| nums_hash[num] = true }

head = head.next while nums_hash.has_key?(head.val)

prev_node = head
node = head.next
until node.nil?
unless nums_hash.has_key?(node.val)
prev_node.next = node
prev_node = node
end
node = node.next
end
prev_node.next = node

head
end