f(j+1)为以下标j结尾的连续子序列最大乘积值(1)

状态转移方程如何表示呢:

这里我们知道A[j]可能为正数(或0)或负数,那么当A[j]为正数,期望前j个乘积为正数,若为负数,则期望前面的为负数。故我们需定义两个函数来确定我们的状态转移方程:

fmax(j+1)=max(max(fmax(j)∗A[j],A[j]),fmin(j)∗A[j])

fmin(j+1)=min(min(fmin(j)∗A[j],A[j]),fmax(j)∗A[j])(2)

class Solution{
public:
int maxProduct(vector<int>& nums) {
int n=nums.size();
assert(n>0);
if(n==1)return nums[0];
int *A=&nums[0];
int fmax=nums[0],fmin=nums[0];
int final_res=fmax;
for(int j=1;j<n;j++)
{
int pre_fmax = fmax;
fmax=max(max(fmax*A[j],A[j]),fmin*A[j]);
fmin=min(min(fmin*A[j],A[j]),pre_fmax*A[j]);
if(final_res<fmax)final_res = fmax;
}
return final_res;
}
};

5.Maximum Product Subarray-Leetcode的更多相关文章

  1. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  2. Maximum Product Subarray——LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. Maximum Product Subarray - LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  5. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  6. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  7. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  8. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  9. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  10. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

随机推荐

  1. elasticsearch嵌套对象的映射

    在es中,我们有时候可能需要映射,{ "field" : "xx" , "field01" : [] }这样格式的嵌套对象,默认情况下es会 ...

  2. Noip模拟21(持续翻车)2021.7.20

    读题总是读错是不是没救了... T1 Median 中位数:按顺序排列的一组数据中居于中间位置的数. 能用上的高亮符号都用上了... 当时忘了就离谱.... 理解什么是中位数(真是个憨憨)后就可以开始 ...

  3. Noip模拟8 2021.6.17

    T1 星际旅行 仔细一看,发现像一个欧拉路(简称一笔画). 满足"可以一笔画"的条件是: 1.所有点都有偶数条连边; 2.有偶数个点连奇数条边; 满足以上两个条件的任意一个即可一笔 ...

  4. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  5. 绘制PCB电路原理图的8种方法

    1.选择集成电路,变压器,晶体管等组件,这些组件体积庞大,有许多引脚并在电路中起主要作用,然后从选定的参考引脚中抽取,以减少错误. 2.如果PCB上标有元件编号(如VD870,R330,C466等), ...

  6. swagger3.0(springboot)消除basic-error-controller

    1.新建springboot项目,可以通过https://start.spring.io/快速生成springboot项目. 2.引入jar依赖: <dependency> <gro ...

  7. 【PowereDesigner】使用方法|mysql画图使用|不在跟新

    自己画E-R图时, 运行:Power Designer ..1 ..2 ..3 可以先放两个空的实体,然后,分别修改属性(鼠标右键,最后一项Properties),名称为:学生.课程. ..4 创建一 ...

  8. Ubuntu 安装 mysql 报错 "update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在"

    解决方法: sudo cp /etc/mysql/my.cnf /etc/mysql/mysql.cnf 偷梁换柱-! 如果想更新mysql的源方法如下: wget http://dev.mysql. ...

  9. webpack 打包图片资源

    webpack 打包图片资源 /** * loader: 1. 下载 2. 使用(配置) * plugins:1. 下载 2. 引入 3.使用 */ // 用来拼接绝对路径的方法 const {res ...

  10. Linux mem 2.7 内存错误检测 (KASAN) 详解

    文章目录 1. 简介 2. Shadow 区域初始化 3. 权限的判断 3.1 read/write 3.2 memxxx() 4. 权限的设置 4.1 buddy 4.1.1 kasan_free_ ...