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. 面试不再慌,终于有人把TCP讲明白了。。。

    前言 TCP(Transmission Control Protocol,传输控制协议) 是计算机网络的的重要组成部分,也是网络编程的重要内容,还有我们平时接触最多的 HTTP 也是基于 TCP 实现 ...

  2. Spring Cloud Alibaba 使用Nacos作为服务注册中心

    为什么需要注册中心? 在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用:服务管理,核心是有个服务注册表,心跳机制动态维护 : 服务注册 创建普通Spring ...

  3. 2021NOI同步赛

    \(NOI\) 网上同步赛 明白了身为菜鸡的自己和普通人的差距 DAY1 \(T1\) 轻重边 [题目描述] 小 W 有一棵 \(n\) 个结点的树,树上的每一条边可能是轻边或者重边.接下来你需要对树 ...

  4. Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定

    题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...

  5. SI Macro

    获取 buf 里的 symbol cbuf = BufListCount() msg(cbuf) ibuf = 0 while (ibuf < cbuf) { hbuf = BufListIte ...

  6. hdu 1503 Advanced Fruits(DP)

    题意: 将两个英文单词进行合并.[最长公共子串只要保留一份] 输出合并后的英文单词. 思路: 求最长公共子串. 记录路径: mark[i][j]=-1:从mark[i-1][j]转移而来. mark[ ...

  7. prometheus(5)之consul服务自动发现及pushgetway

    pushgetway(push上传metric数据) Pushgateway简介 Pushgateway是prometheus的一个组件,prometheus server默认是通过exporter主 ...

  8. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

  9. linux 入门系列-基础性知识

    1:初探linux-基于centos7 运维和服务器硬件组合 两种登录方式:(1)-------root:管理员登录权限较高,不建议初学者使用格式: [root@centos7 jinlong]# ( ...

  10. Part 61 to 63 Talking about partial class and partial method in C#

    部分类和部分方法 部分类是用partial声明的类,它允许我们把一个类分割成两个或多个类,当应用程序编译的完成的时候,多个部分类会结合成一个类,同时partial关键字也可以用于声明结构和接口. 那么 ...