题目: https://leetcode.cn/problems/maximum-number-of-removable-characters/
思路是按照官方题解实现二分,但是最后三个测试用例总是会卡超时,求大家帮我优化一下,或者写一个能通过测试用例的答案出来
def maximum_removals(s, tp, removable)
left = 0
@m = s.size
@n = tp.size
right = [removable.size(),@m - @n].min
while left < right
mid = (right - left + 1) / 2 + left
if can_remove?(s,tp,removable, mid)
left = mid
else
right = mid - 1
end
end
left
end
def can_remove?(s, tp, removable, k)
r = []
(0...k).each do |i|
r[removable[i]] = true
end
i = 0
j = 0
while i < @m do
if s[i] == tp[j] && !r[i]
j += 1
return true if j == @n
end
i += 1
end
false
end