网站需求分析怎么写,网站建设的几点体会,新网站应该怎么做seo,怎么防止网站攻击题目
给你一个整数数组 nums #xff0c;请你找出数组中乘积最大的非空连续 子数组 #xff08;该子数组中至少包含一个数字#xff09;#xff0c;并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
示例 1:
输入: nums [2,3,-2,4] 输出: 6 解释: 子数…题目
给你一个整数数组 nums 请你找出数组中乘积最大的非空连续 子数组 该子数组中至少包含一个数字并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
示例 1:
输入: nums [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6。
解
class Solution {public int maxProduct(int[] nums) {int n nums.length;int[][] dp new int[n][2];dp[0][0] nums[0];dp[0][1] nums[0];int max nums[0];for (int i 1; i n; i) {if (nums[i] 0) {dp[i][0] Math.max(nums[i], dp[i - 1][0] * nums[i]);dp[i][1] Math.min(nums[i], dp[i - 1][1] * nums[i]);} else {dp[i][0] Math.max(nums[i], dp[i - 1][1] * nums[i]);dp[i][1] Math.min(nums[i], dp[i - 1][0] * nums[i]);}max Math.max(max, dp[i][0]);}return max;}
}