5.Maximum Product Subarray-Leetcode
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的更多相关文章
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- Maximum Product Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Maximum Product Subarray - LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
随机推荐
- 单片机stm32零基础入门之--初识STM32 标准库
CMSIS 标准及库层次关系 因为基于Cortex 系列芯片采用的内核都是相同的,区别主要为核外的片上外设的差异,这些差异却导致软件在同内核,不同外设的芯片上移植困难.为了解决不同的芯片厂商生产的Co ...
- STP生成树协议在二层环境中的应用
一 STP简介 1.单词: rstp快速生成树协议 filter过滤 protection保护 2.作用: 通过阻塞特定接口来防止二层交换环路,从而做到既可以提高网络可靠性的同时又能避免环路带来的问题 ...
- Python super(Todo,self).__init__() TypeError: super() argument 1 must be type, not classobj
示例如下 class A(): def __init__(self):pass class B(A): def __init__(self): super(A, self).__init__() 当调 ...
- 字符串匹配 ?kmp : hash
给定一个模式串S,以及一个模板串P,所有字符串中只包含大小写英文字母以及阿拉伯数字. 模板串P在模式串S中多次作为子串出现. 求出模板串P在模式串S中所有出现的位置的起始下标. 输入格式 第一行输入整 ...
- 使用ssh连接到centos7中docker容器
任务: 使用ssh连接到centos7中docker容器 实验步骤: 实验环境搭建,详情请看上一篇. 因为docker中容器的ip通常来说是和真机以及centos7的ip不属于一个网段,因此直接访问是 ...
- HashMap 中的一个“坑”!
最近公司新来了一个小伙伴,问了磊哥一个比较"奇怪"的问题,这个问题本身的难度并不大,但比较"隐蔽",那究竟是什么问题呢?接下来我们一起来看. 起因 最近公司 ...
- Hello World之编译链接装载与执行(1)
一:前言 我打算写一系列博客来说说我对Hello World在计算机中的生命旅程的理解,我是一名软件工程专业的大三学生,有关这个问题我主要的参考书有<深入理解计算机系统>.<现代操作 ...
- CentOS服务器的网络配置与部署
1.系统安装与软件安装 1.1选择CentOs7.9release版本用作所研发系统部署服务器,官网以及所选择镜像为地址为:http://ftp.sjtu.edu.cn/centos/7.9.2009 ...
- springboot入门之版本依赖和自动配置原理
前言 Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that ...
- rocketmq广播消息的(五)
一.简介 广播消费指的是:一条消息被多个consumer消费,即使这些consumer属于同一个ConsumerGroup,消息也会被ConsumerGroup中的每个Consumer都消费一次,广播 ...