经典题目(6)数组中的最长连续子序列,盛水最多的容器 NC95数组中的最长连续子序列给定无序数组arr返回其中最长的连续序列的长度(要求值连续位置可以不连续,例如 3,4,5,6为连续的自然数思路使用集合的思想对于arr中的数num如果num-1在集合中持续找直到找到最小的数然后再根据最小的数开始找集合中连续的数更新max_length的值class Solution: def MLS(self , arr: List[int]) - int: # write code here sset(arr) max_len0 for num in arr: if num-1 in s: # 注意在列表arr中查找复杂度为o(n)在集合s中查找复杂度为o(1) continue cur_numnum cnt1 while cur_num1 in s: cur_num1 cnt1 max_lenmax(max_len,cur_num) return max_lenBM93盛水最多的容器给定一个数组height长度为n每个数代表坐标轴中的一个点的高度height[i]是在第i点的高度请问从中选2个高度与x轴组成的容器最多能容纳多少水1.你不能倾斜容器2.当n小于2时视为不能形成容器请返回03.数据保证能容纳最多的水不会超过整形范围即不会超过231-1数据范围:0height.length1050height[i]104思路:盛水最多的容器只取决于短边,所以每次只要移动短边class Solution: def maxArea(self , height: List[int]) - int: # write code here left0 rightlen(height)-1 max_water0 water0 while leftright: if height[left]height[right]: water(right-left)*height[left] left1 else: water(right-left)*height[right] right-1 max_watermax(max_water,water) return max_water