解题思路:
1. 边界条件,长度小于等于2,就返回数组长度
2. 单调递增子序列的长度
3. 反向单调递减子序列长度
class Solution: def __get_dp_up(self, numberList): """ 获取单调递增的最长子序列(可以不连续) """ dp_up = [1 for i in range(len(numberList))] for i in range(1, len(numberList)): for j in range(0, i): if numberList[i] > numberList[j]: dp_up[i] = max(dp_up[i], dp_up[j] + 1) return dp_up def mountainSequence(self , numberList ): # write code here # 边界条件 if len(numberList) <= 2: return len(numberList) # 单调递增,最长序列 dp_up = self.__get_dp_up(numberList) # 单调递减,最长序列 dp_down = self.__get_dp_up(numberList[::-1])[::-1] rnt = dp_up[0] + dp_down[0] for i in range(1, len(dp_up)): rnt = max(rnt, dp_up[i] + dp_down[i]) return rnt - 1