前置题目是300, 对于每个数字, 求得包括其的左递增子序列长度, 和包括其的递减右子列长度, 相加减-即可.
class Solution:
def calculate_increase_num(self, nums):
if not nums:
return nums
longest_val_list = []
longest_index_list = []
lis_list = []
longest_val_list.append(nums[0])
longest_index_list.append(0)
lis_list.append(1)
for i in range(1, len(nums)):
val = nums[i]
if val > longest_val_list[-1]:
longest_val_list.append(val)
longest_index_list.append(i)
lis_list.append(len(longest_val_list))
continue
start = 0
end = len(longest_val_list) - 1
while start < end:
mid = (start + end) // 2
if longest_val_list[mid] >= val:
end = mid
else:
start = mid + 1
longest_val_list[start] = val
lis_list.append(start + 1)
return lis_list
def minimumMountainRemovals(self, nums: List[int]) -> int:
left_increase_num_list = self.calculate_increase_num(nums)
right_increase_num_list = self.calculate_increase_num(nums[::-1])[::-1]
length = 0
for i in range(len(left_increase_num_list)):
a = left_increase_num_list[i]
b = right_increase_num_list[i]
if a == 1 or b == 1:
continue
tmp = a + b - 1
if tmp > length:
length = tmp
result = len(left_increase_num_list) - length
return result