1. Título
Dado um número inteiro de matriz, encontre a submatriz contínua com o maior produto na matriz (a submatriz contém pelo menos um número) e retorne o produto correspondente à submatriz.
Exemplo 1:
输入: [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6。
Exemplo 2:
输入: [-2,0,-1]
输出: 0
解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
Dois, referência
1. Recursão
Ideias:
Para obter detalhes, entenda o código e os comentários. O tempo limite de execução, mas você pode treinar para entender a recursão.
Código:
class Solution {
int max = Integer.MIN_VALUE;
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}
helper(nums, 1, 0);
return max;
}
// 1-最大连续子列积
private void helper(int[] nums, int product, int i) {
// Terminator
if (i == nums.length) {
return;
}
// Current logic:比较历史最大值与当前值,历史最大值形成增益则保留,否则丢弃
int select = nums[i] * product;
int max_value = Math.max(nums[i], select); // compare num[i] ? product*num[i]
max = Math.max(max_value, max);
// Drill down:保留有效数据,下一层可能会用到
helper(nums, nums[i], i + 1);
helper(nums, select, i + 1);
}
}
// 2-最大不连续子列积
// private void helper(int[] nums, int product, int i) {
// if (i == nums.length) {
// return;
// }
// int select = nums[i] * product;
// int max_value = Math.max(product, select); // 差异
// max = Math.max(max_value, max);
// helper(nums, product, i + 1); // 重点差异
// helper(nums, select, i + 1);
// }
Complexidade de tempo: O (2 n) O (2 ^ n)O(2n )
Complexidade do espaço: O (n) O (n)O ( n )
2. Recursão + memoização
Idéias: Não.
Código: Nenhum.
Complexidade de tempo: O (n) O (n)
Complexidade do espaço O ( n ) :O (n) O (n)O ( n )
3. Planejamento dinâmico
versão 1
Ideias:
1、状态定义:DP[i][2]
DP[i][0]:最大值
DP[i][1]:最小值
2、转移方程:DP[i]=DP[i-1]*a[i]
DP[i][0] = a[i]>=0 ? DP[i-1][0]*a[i] : DP[i-1][1]*a[i]
DP[i][1] = a[i]>=0 ? DP[i-1][1]*a[i] : DP[i-1][0]*a[i]
return DP[i][0]
Código:
class Solution {
public int maxProduct(int[] nums) {
if (nums==null || nums.length==0) return 0;
int[][] dp = new int[nums.length][2];
int res=nums[0]; dp[0][0]=nums[0]; dp[0][1]=nums[0];
for (int i=1; i<nums.length; i++) {
dp[i][0] = Math.max( Math.max(dp[i-1][0]*nums[i], dp[i-1][1]*nums[i]), nums[i]);
dp[i][1] = Math.min( Math.min(dp[i-1][0]*nums[i], dp[i-1][1]*nums[i]), nums[i]);
res = Math.max(res, dp[i][0]);
}
return res;
}
}
Complexidade de tempo: O (n) O (n)
Complexidade do espaço O ( n ) :O (n) O (n)O ( n )
Versão 2
Ideias:
Pode ser visto acima que DP [i] [0/1] depende apenas do resultado da última operação DP [i-1] [0/1], portanto, a otimização do espaço pode ser realizada aqui. O código específico é o seguinte:
Código:
class Solution {
public int maxProduct(int[] nums) {
if (nums==null || nums.length==0) return 0;
int currMax = nums[0], currMin = nums[0], ans = nums[0];
for (int i = 1; i < nums.length; ++i) {
int mx = currMax, mn = currMin;
currMax = Math.max(mx * nums[i], Math.max(nums[i], mn * nums[i]));
currMin = Math.min(mn * nums[i], Math.min(nums[i], mx * nums[i]));
ans = Math.max(currMax, ans);
}
return ans;
}
}
Complexidade de tempo: O (n) O (n)
Complexidade de espaço O ( n ) :O (1) O (1)O(1)
Três, referência
1. Possivelmente a solução mais simples com complexidade O (n) tempo
2. Submatriz máxima de produto
3. Irmão, tentei o meu melhor