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

一、最大连续子序列和

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. ActionContext 与 ServletActionContext获取Session的异同

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  2. Java 序列化和反序列化(三)Serializable 源码分析 - 2

    目录 Java 序列化和反序列化(三)Serializable 源码分析 - 2 1. ObjectStreamField 1.1 数据结构 1.2 构造函数 2. ObjectStreamClass ...

  3. keepalived 参数中文说明

    GLOBAL CONFIGURATION Global definitions global_defs { notification_email { admin@example.com } notif ...

  4. 启动多个mysql实例

    如果使用./support-files/mysql.server 文件启动,则默认读取/etc/my.cnf 配置文件,这种方式有时候启动不了 会提示报错 :The server quit witho ...

  5. spring boot Swagger2(version=2.7.0) 注解@ApiImplicitParam的属性dataType值为”自定义泛型“应用

    注解: @ApiImplicitParams @ApiImplicitParam    name="需注解的API输入参数", value="接收参数的意义描述" ...

  6. 【胡策篇】题解 (UOJ 192 + CF938G + SPOJ DIVCNT2)

    和泉纱雾与烟花大会 题目来源: UOJ 192 最强跳蚤 (只改了数据范围) 官方题解: 在这里哦~(说的很详细了 我都没啥好说的了) 题目大意: 求树上各边权乘积是完全平方数的路径数量. 这种从\( ...

  7. eclipse创建MAVEN项目是出现Could not resolve archetype的解决办法

    eclipse第一次创建MAVEN项目时出现这个问题,查了很多文档改了没用,后来问了别人知道是maven中央仓库下载插件包失败就会报错. 解决办法: 用国内阿里云镜像会好很多 在settings.xm ...

  8. filter的使用场景

    1.filter的使用场景:假如有一个对象数组A,获取数组中的指定类型的对象放到B数组中 我们在ES5先进行for循环遍历数组,再进行if 判断,如果数组中某个对象的类型符合要求,push 到一个新数 ...

  9. leetcood学习笔记-202-快乐数

    题目描述: 方法一:比较笨的办法,根据题意,如果变成1返回True,如果出现重复返回False 看到下面有位朋友用的是dict,我用了list,两个都跑了一下似乎list快一点? class Solu ...

  10. Batch - FOR %%a %%b

    总结 %%a refers to the name of the variable your for loop will write to. Quoted from for /?: FOR %vari ...