[LeetCode] Maximum Product Subarray 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
,a,b,c,d,e,f,g,h,
- 判断输入个数。
- 设初始值,当前计算值curval = 1.
- 从左遍历数组。
- 如果遇到0,判断最大值(eg:-1,0,-2),curval =1,继续。
- 如果非0,那么curval 乘以该值,更新返回值。
- 从右遍历数组,重复4.5步,判断0就不要需要了。
#include <iostream>
using namespace std; class Solution {
public:
int maxProduct(int A[], int n) {
if(n<) return A[];
int retMax=A[];
int curval=;
for(int i=;i<n;i++){
if(A[i]==){
if(>retMax) retMax=;
curval=;
continue;
}
curval*=A[i];
if(curval>retMax) retMax=curval;
}
curval = ;
for(int i=n-;i>=;i--){
if(A[i]==){
curval = ;
continue;
}
curval*= A[i];
if(curval>retMax) retMax= curval;
}
return retMax;
}
}; int main()
{
int a[]={-,,-};
Solution sol;
cout<<sol.maxProduct(a,sizeof(a)/sizeof(int))<<endl;
return ;
}
官方解法中结合了求最小值的解答,是一个标准的dp 过程,
[LeetCode] Maximum Product Subarray 连续数列最大积的更多相关文章
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 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 最大子序列积
题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [leetcode]Maximum Product Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...
- DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...
随机推荐
- Python9-MySQL索引-外键-day43
1.以ATM引出DBMS2.MySQL -服务端 -客户端3.通信交流 -授权 -SQL语句 -数据库 create database db1 default charset=utf8; drop d ...
- stm32串口——标志位学习
/* 在USART的发送端有2个寄存器,一个是程序可以看到的USART_DR寄存器,另一个是程序看不到的移位寄存器,对应USART数据发送有两个标志,一个是TXE=发送数据寄存器空,另一个是TC=发送 ...
- North American Invitational Programming Contest (NAIPC) 2016
(待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...
- [USACO]玉米实验(单调队列)
Description 约翰决定培育新的玉米品种以提高奶牛的产奶效率.约翰所有的土地被分成 N ×N 块,其中第 r行第 c 列的玉米质量为 Ar,c.他打算找一块质量比较均匀的土地开始自己的实验.经 ...
- Linux文件属性之文件权限介绍
1)用ls -li 查看文件列表字段 红色代表的是inode 黄色代表的是文件权限 黄色里面的第一个 - 表示文件的类型(普通类型文件) d 表示目录(directory) l 表示链接文件(link ...
- shell脚本入门基础知识
shell 脚本的第一行 #!/bin/bash #!/bin/sh 其实,sh是bash的一个软链接 sh -> bash 变量,字母下划线开头(好像是没有类型的) #普通变量 var1=ni ...
- Kali 远程登陆SSH
一.配置SSH 编辑/etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,将NO修改为YES //可以用密码登陆 将PermitRootLogin ...
- Redis数据结构以及Strings型操作
Redis数据结构图: Strings型 <String key,String value>: keys * 查看所有key get 获取key的value值 append 向key对 ...
- CLOUDSTACK FOR HYPER-V
原文地址:http://zhu.vn/archives/1040 我这里是内网测试环境,宿主机为Server 2012R2 ,虚拟化技术为HYPER-V,域环境来的(不是域环境玩不了). 先给宿主机安 ...
- TortoiseSVN 同时检索多人的提交日志记录(如图操作)