Skip to content

Commit 40471f0

Browse files
Merge pull request #7 from theforestvn88/august
August
2 parents 02b2c55 + 923e547 commit 40471f0

10 files changed

+256
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# @param {Integer[]} nums
2+
# @param {Integer} k
3+
# @return {Integer}
4+
def split_array(nums, k)
5+
# count number of sub-array that has sum <= largest-sum
6+
count_sub_arrs = lambda { |largest_sum|
7+
count = 0
8+
sum = 0
9+
nums.each { |num|
10+
if sum + num <= largest_sum
11+
sum += num
12+
else
13+
count += 1
14+
sum = num
15+
end
16+
}
17+
18+
count
19+
}
20+
21+
# the smaller the largest-sum is, the more sub-arrays we can divide
22+
# so we need to find the max largest-sum that we can divide nums into more than or equal k sub-arrays
23+
# that is also the min largest-sum we can divide nums into k sub-arrays
24+
25+
low = nums.max
26+
high = nums.sum + 1
27+
28+
while low < high
29+
mid = (low + high)/2
30+
count = count_sub_arrs.call(mid)
31+
32+
if count >= k # still be able to divide nums more than k sub-arrays
33+
low = mid + 1
34+
else
35+
high = mid
36+
end
37+
end
38+
39+
low
40+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# @param {Integer[][]} points
2+
# @return {Integer}
3+
def number_of_boomerangs(points)
4+
count = 0
5+
(0...points.length).each { |i|
6+
dist_counter = Hash.new(0)
7+
(0...points.length).each { |j|
8+
dist = (points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2
9+
# for each new boomerang point j, there're 2 boomerangs combine by j and each previous boomerang points
10+
count += 2 * dist_counter[dist]
11+
dist_counter[dist] += 1
12+
}
13+
# we also can count boomerangs each i by sum of Permutation(k, 2)
14+
# count += dist_counter.values.reduce(0) { |sum, k| sum += k*(k-1) }
15+
}
16+
17+
count
18+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# @param {Integer[]} nums
2+
# @param {Integer} k
3+
# @return {Integer}
4+
def find_pairs(nums, k)
5+
count = 0
6+
appear = Hash.new(0)
7+
8+
nums.each { |num|
9+
next if appear[num] >= (k == 0 ? 2 : 1)
10+
11+
if k == 0
12+
count += appear[num] == 1 ? 1 : 0
13+
else
14+
count += (appear[num-k] >= 1 ? 1 : 0) + (appear[num+k] >= 1 ? 1 : 0)
15+
end
16+
appear[num] += 1
17+
}
18+
19+
count
20+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# @param {String} s
2+
# @return {Boolean}
3+
def check_record(s)
4+
a_count = 0
5+
l_count = 0
6+
l_prev = -1
7+
(0...s.length).each { |i|
8+
if s[i] == 'A'
9+
a_count += 1
10+
return false if a_count >= 2
11+
elsif s[i] == 'L'
12+
if l_prev == i - 1
13+
l_count += 1
14+
return false if l_count >= 3
15+
else
16+
l_count = 1
17+
end
18+
l_prev = i
19+
end
20+
}
21+
22+
true
23+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# @param {String} equation
2+
# @return {String}
3+
def solve_equation(equation)
4+
# summary: ax + b
5+
count_x = lambda { |str|
6+
a = 0
7+
b = 0
8+
num = nil
9+
sign = 1
10+
str.each_char { |c|
11+
if c == 'x'
12+
a += sign * (num || 1)
13+
num = 0
14+
elsif c == '+'
15+
b += sign * num.to_i
16+
num = nil
17+
sign = 1
18+
elsif c == '-'
19+
b += sign * num.to_i
20+
num = nil
21+
sign = -1
22+
else
23+
num = (num || 0) * 10 + c.to_i
24+
end
25+
}
26+
b += sign * num.to_i
27+
28+
return [a, b]
29+
}
30+
31+
left, right = equation.split('=')
32+
al, bl = count_x.call(left)
33+
ar, br = count_x.call(right)
34+
35+
a = al - ar
36+
b = br - bl
37+
38+
if a == 0
39+
b == 0 ? "Infinite solutions" : "No solution"
40+
elsif b % a != 0
41+
"No solution"
42+
else
43+
"x=#{b/a}"
44+
end
45+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# @param {String[]} cpdomains
2+
# @return {String[]}
3+
def subdomain_visits(cpdomains)
4+
counter = Hash.new(0)
5+
cpdomains.each { |cp|
6+
count, domain = cp.split(' ')
7+
count = count.to_i
8+
9+
i = domain.length
10+
loop do
11+
i = domain.rindex('.', i-1)
12+
break if i.nil?
13+
14+
counter[domain[i+1..]] += count
15+
end
16+
counter[domain] += count
17+
}
18+
19+
counter.map { |domain, count|
20+
"#{count} #{domain}"
21+
}
22+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# @param {String[]} queries
2+
# @param {String[]} words
3+
# @return {Integer[]}
4+
def num_smaller_by_frequency(queries, words)
5+
f = lambda { |str|
6+
count = 0
7+
curr = 'z'
8+
str.each_char { |c|
9+
if curr == c
10+
count += 1
11+
elsif curr.ord > c.ord
12+
curr = c
13+
count = 1
14+
end
15+
}
16+
count
17+
}
18+
19+
word_sorted = words.map { |word| f.call(word) }.sort
20+
21+
ans = []
22+
queries.each { |q|
23+
freq = f.call(q)
24+
if i = word_sorted.bsearch_index { |w_freq| w_freq > freq }
25+
ans << (word_sorted.length - i)
26+
else
27+
ans << 0
28+
end
29+
}
30+
31+
ans
32+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# @param {Integer[]} nums
2+
# @return {Integer}
3+
def min_swaps(nums)
4+
# sliding-window size = total ones
5+
total_ones = nums.count { |num| num == 1 }
6+
# circular
7+
nums += nums
8+
# number of swaps = number of zeros
9+
zeros = 0
10+
(0...total_ones).each { |i|
11+
zeros += (nums[i] == 0 ? 1 : 0)
12+
}
13+
min = zeros
14+
15+
(total_ones...nums.length).each { |i|
16+
zeros += (nums[i] == 0 ? 1 : 0) - (nums[i-total_ones] == 0 ? 1 : 0)
17+
min = [min, zeros].min
18+
}
19+
20+
min
21+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @param {String[]} details
2+
# @return {Integer}
3+
def count_seniors(details)
4+
details.count { |d| d[-4..-3].to_i > 60 }
5+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for singly-linked list.
2+
# class ListNode
3+
# attr_accessor :val, :next
4+
# def initialize(val = 0, _next = nil)
5+
# @val = val
6+
# @next = _next
7+
# end
8+
# end
9+
# @param {Integer[]} nums
10+
# @param {ListNode} head
11+
# @return {ListNode}
12+
def modified_list(nums, head)
13+
nums_hash = {}
14+
nums.each { |num| nums_hash[num] = true }
15+
16+
head = head.next while nums_hash.has_key?(head.val)
17+
18+
prev_node = head
19+
node = head.next
20+
until node.nil?
21+
unless nums_hash.has_key?(node.val)
22+
prev_node.next = node
23+
prev_node = node
24+
end
25+
node = node.next
26+
end
27+
prev_node.next = node
28+
29+
head
30+
end

0 commit comments

Comments
 (0)