这两个问题类似,都可利用动态规划思想求解。

一、最大连续子序列和

https://leetcode.com/problems/maximum-subarray/description/

https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/

The core ideas are the same:

        currentMax = max(nums[i], some_operation(currentMax, nums[i])).

For each element, we have 2 options: put it inside a consecutive subarray, or start a new subarray with it.

#include <vector>

int maxSubArray(std::vector<int>& nums)
{
if (nums.empty())
{
return ;
} int currMax = nums[];
int maxResult = nums[]; int size = (int)nums.size();
for (int i = ; i < size; ++i)
{
currMax = std::max(nums[i], currMax + nums[i]);
maxResult = std::max(maxResult, currMax);
} return maxResult;
}

二、最大连续子序列积

https://stackoverflow.com/questions/25590930/maximum-product-subarray

https://leetcode.com/problems/maximum-product-subarray/description/

https://www.geeksforgeeks.org/maximum-product-subarray/

https://www.geeksforgeeks.org/maximum-product-subarray-added-negative-product-case/

https://www.geeksforgeeks.org/maximum-product-subarray-set-2-using-two-traversals/

#include <vector>

int maxProduct(std::vector<int>& nums)
{
if (nums.empty())
{
return ;
} int size = (int)nums.size(); int currMax = nums[];
int currMin = nums[];
int maxResult = nums[]; for (int i = ; i < size; ++i)
{
int t_currMax = currMax * nums[i];
int t_currMin = currMin * nums[i]; currMax = max(nums[i], max(t_currMax, t_currMin));
currMin = min(nums[i], min(t_currMax, t_currMin));
maxResult = max(maxResult, currMax);
} return maxResult;
}

Algo: maxSubArray vs. maxProduct的更多相关文章

  1. MaxSubArray 最大子数列和

    public int maxSubArray(int[] A) { int newsum=A[0]; int max=A[0]; for(int i=1;i<A.length;i++){ new ...

  2. maxSubArray

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

  3. Atitit s2018.6 s6 doc list on com pc.docx Atitit s2018.6 s6 doc list on com pc.docx  Aitit algo fix 算法系列补充.docx Atiitt 兼容性提示的艺术 attilax总结.docx Atitit 应用程序容器化总结 v2 s66.docx Atitit file cms api

    Atitit s2018.6 s6  doc list on com pc.docx Atitit s2018.6 s6  doc list on com pc.docx  Aitit algo fi ...

  4. algo: 冒泡排序(Java实现)

    package com.liuxian.algo; public class MySortClass implements Comparable<MySortClass> { public ...

  5. 【algo&ds】4.B树、字典树、红黑树、跳表

    上一节内容[algo&ds]4.树和二叉树.完全二叉树.满二叉树.二叉查找树.平衡二叉树.堆.哈夫曼树.散列表 7.B树 B树的应用可以参考另外一篇文章 8.字典树Trie Trie 树,也叫 ...

  6. [Algo] 87. Max Product Of Cutting Rope

    Given a rope with positive integer-length n, how to cut the rope into m integer-length parts with le ...

  7. ALGO基础(一)—— 排序

    ALGO基础(一)-- 排序 冒选插希快归堆,以下均为从小到大排 1 冒泡排序 描述: 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一 ...

  8. 蓝桥杯 algo——6 安慰奶牛 (最小生成树)

    问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是一个奶牛的家.FJ计 划除去P条道路中尽可能多的道路 ...

  9. [MIT Intro. to algo]Lecture 1: 课程介绍,算法优势,插入算法和归并算法分析,渐近符号

    The theoretical study of computer program performance and resource useage.   First, analysis and the ...

随机推荐

  1. Keepalived 双主虚拟路由配置实例

    Keepalived 双主虚拟路由配置实例 演示前说明: 2台centos7.2 主机:node-00,node-01 VIP1:10.1.38.19预定node-00占有 VIP2:10.1.38. ...

  2. 高级Java必看的10本书

    1.深入理解Java虚拟机:JVM高级特性与最佳实践 本书共分为五大部分,围绕内存管理.执行子系统.程序编译与优化.高效并发等核心主题对JVM进行了全面而深入的分析,深刻揭示了JVM的工作原理. 2. ...

  3. python库argparse中type的新奇指定方法

    最近在看一些项目的源码,总是能学到好多东西. 关于arparse中type的类型指定 不止可以指定常规类型,还可以加一些自己类型判断,具体用法如下(来源): def str2bool(v): &quo ...

  4. 用户态和内核态&操作系统

    用户态和内核态 内核态:cpu可以访问内存的所有数据,包括外围设备,例如硬盘,网卡,cpu也可以将自己从一个程序切换到另一个程序. 用户态:只能受限的访问内存,且不允许访问外围设备,占用cpu的能力被 ...

  5. vue与webpack

    由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和config里面一些相关的配置,所以刚好趁此机会将所有配置文件看一遍,理一理思路,也便于以后修 ...

  6. 2018年初面试Java(1.5年经验)

    xml文档如何解析 控制反转如何实现 http://www.cnblogs.com/qf123/p/8602972.html struts2和springmvc的区别 http://www.cnblo ...

  7. 读书笔记一、numpy基础--创建数组

    创建ndarray   (1)使用array函数 接受一切序列型的对象(包括其他数组),然后产生一个新的含有传入数据的numpy数组. import numpy as np #将一个由数值组成列表作为 ...

  8. react 组件的构造函数

    constructor 函数时组件最先执行的函数 class childen extends react.Component{ constructor(props){ super(props); th ...

  9. VC++6.0环境中输出特殊字符

    该方法最靠谱:https://blog.csdn.net/xiaofeilong321/article/details/8713002 输出特殊字符需使用扩展的ASCII码. 修改控制台显示设置: ( ...

  10. FTP 连接模式 (主动模式被动模式)

    FTP是有两种传输的模式的,主动模式和被动模式,一个完整的FTP文件传输需要建立两种类型的连接,一种为文件传输下命令,称为控制连接,另一种实现真正的文件传输,称为数据连接. 1. 控制连接客户端希望与 ...