视频连接:https://www.bilibili.com/video/BV1kN411R7Vg/
# # # @param arr double浮点型一维数组 # @return double浮点型 # class Solution: def maxProduct(self , arr ): n = len(arr) # 初始化 最大 最小 dp数组 max_dp = [1.0]*n min_dp = [1.0]*n # 赋初值 min_dp[0] = arr[0] max_dp[0] = arr[0] total = arr[0] for i in range(1, n): if arr[i] > 0: # 正*正 = 正 max_dp[i] = max(arr[i], max_dp[i-1]*arr[i]) # 负*正= 负 min_dp[i] = min(arr[i], min_dp[i-1]*arr[i]) else: # 负*负=正 max_dp[i] = max(arr[i], min_dp[i-1]*arr[i]) # 正*负=负 min_dp[i] = min(arr[i], max_dp[i-1]*arr[i]) total = max(total, max_dp[i]) return total