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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @param {String} s
# @return {String}
def reverse_parentheses(s)
result = []
s.each_char { |c|
if c == ")"
rev_str = []
until (rc = result.pop) == "("
rev_str << rc
end
result += rev_str
else
result << c
end
}

result.join
end
18 changes: 18 additions & 0 deletions 1501-1600/1598_crawler_log_folder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @param {String[]} logs
# @return {Integer}
def min_operations(logs)
step_down = 0
logs.each do |log|
case log
when "../"
step_down -= 1
step_down = 0 if step_down < 0
when "./"
# do nothing
else
step_down += 1
end
end

step_down
end
12 changes: 12 additions & 0 deletions 1701-1800/1701_average_waiting_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @param {Integer[][]} customers
# @return {Float}
def average_waiting_time(customers)
sum = 0
curr_time = 0
customers.each do |(start_time, duration)|
curr_time = [start_time, curr_time].max + duration
sum += curr_time - start_time
end

sum.to_f/customers.length
end
50 changes: 50 additions & 0 deletions 1701-1800/1717_maximum_score_from_removing_substrings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# @param {String} s
# @param {Integer} x
# @param {Integer} y
# @return {Integer}
def maximum_gain(s, x, y)
# explain why greedy work
# assume "ba" > "ab"
# in case: xbax, which x is not b nor b, of course we will remove ba
# in case: abax, since ba > ab, so we will remove ba
# in case: xbaa, we have no choice but remove ba
# in case: abaa, since ba > ab, so we will remove ba
# in case: bbax, we have no choice but remove ba
# in case: bbab, we have no choice but remove ba
# in case: abab, ba + ab > ab + ab => so we will remove ba first
# in case: bbaa, of course, 2 ba
# in case: baxx, which x is not b nor b, of course we will remove ba
# in case: baax, of course we will remove ba
# in case: baaa, same as above
# in case: baab, ba + ab
# in case: baba, of course, 2 ba
# in case: babx, x is not a, ba > ab, so remove ba first
# in case: xaba, x is not b, ba > ab, so remove ba first
# in case: xbba, no matter x, remove ba first
# ==> Any case, remove the substring which gain more points first
#
sum = 0
max_sub = x >= y ? "ab" : "ba"
max_point = x >= y ? x : y
min_point = x >= y ? y : x
stack = []
s << "."
s.each_char { |c|
if c == "a" || c == "b"
if !stack.empty? && stack[-1] == max_sub[0] && c == max_sub[1]
sum += max_point
stack.pop
else
stack << c
end
else
# since above remove any max-sub "ba" or "ab"
# so now stack is something like "aaaa...bbbb..." or "bbb...aaa..."
a_count = stack.count("a")
sum += [a_count, stack.length - a_count].min * min_point
stack = []
end
}

sum
end
5 changes: 5 additions & 0 deletions 1901-2000/1929_concatenation_of_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @param {Integer[]} nums
# @return {Integer[]}
def get_concatenation(nums)
nums * 2
end
17 changes: 17 additions & 0 deletions 1901-2000/1961_check_if_string_is_a_prefix_of_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @param {String} s
# @param {String[]} words
# @return {Boolean}
def is_prefix_string(s, words)
i = 0
j = 0
while i < s.length && j < words.length
if s[i...i+words[j].length] == words[j]
i += words[j].length
j += 1
else
return false
end
end

i == s.length && j <= words.length
end
13 changes: 13 additions & 0 deletions 2201-2300/2221_find_triangular_sum_of_an_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @param {Integer[]} nums
# @return {Integer}
def triangular_sum(nums)
len = nums.length
while len > 1
(0...len-1).each { |i|
nums[i] = (nums[i] + nums[i+1]) % 10
}
len -= 1
end

nums.first
end
6 changes: 6 additions & 0 deletions 2501-2600/2545_sort_the_students_by_their_kth_score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @param {Integer[][]} score
# @param {Integer} k
# @return {Integer[][]}
def sort_the_students(score, k)
score.sort_by! { |row| -row[k] }
end
10 changes: 10 additions & 0 deletions 2601-2700/2640_find_the_score_of_all_prefixes_of_an_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# @param {Integer[]} nums
# @return {Integer[]}
def find_prefix_score(nums)
sum = 0
curr_max = 0
nums.map { |num|
curr_max = [curr_max, num].max
sum += curr_max + num
}
end
27 changes: 27 additions & 0 deletions 2701-2800/2751_robot_collisions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @param {Integer[]} positions
# @param {Integer[]} healths
# @param {String} directions
# @return {Integer[]}
def survived_robots_healths(positions, healths, directions)
order = [positions, healths, directions.chars].transpose.sort_by(&:first)
stack = []
order.each { |(p, h, d)|
while !stack.empty? && stack[-1][-1] == 'R' && d == 'L'
if stack[-1][-2] == h
stack.pop
h = 0
elsif stack[-1][-2] < h
stack.pop
h -= 1
else
stack[-1][-2] -= 1
h = 0
end
break if h == 0
end
stack << [p, h, d] if h > 0
}

remain = stack.map { |(p, h, d)| [p, h] }.to_h
positions.select { |p| remain.has_key?(p) }.map { |p| remain[p] }
end