//数组中连续数之和最大
int maxSubArray(vector nums){
if (nums.size() == 0)
return 0;
int *dp = new int[nums.size()];
int r = nums[0];
dp[0] = nums[0]; //这里可以不必要使用数组
for (int i = 1; i < nums.size(); i++){
int temp = nums[i];
dp[i] = max(temp,dp[i-1]+temp);
r = max(dp[i],r);
}
return r;
}
//乘法就是要跟踪一下局部的最大值和最小值即可,因为乘法最小值乘以负数也可能出现最大值
int maxProduct(vector nums) {
if (nums.size() == 0) return 0;
int* maxValue = new int[nums.size()];
int* minValue = new int[nums.size()];
int r = nums[0];
maxValue[0] = r;
minValue[0] = r;
for (int i = 1; i < nums.size(); i++) {
int n = nums[i];
int a = maxValue[i - 1] * n;
int b = minValue[i - 1] * n;
maxValue[i] = max(n, max(a, b));
minValue[i] = min(n, min(a, b));
r = max(maxValue[i], r);
}
return r;
}