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

一、最大连续子序列和

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. 虚拟机安装VMware Tools, 安装gcc编译器

    一.虚拟机安装VMware Tools 1.虚拟机=>安装VMware Tools 2.打开文件,将下载的压缩包VMwareTools-10.3.10-12406962.tar.gz移动到指定安 ...

  2. Java的核心优势

    Java为消费类智能电子产品而设计,但智能家电产品并没有像最初想象的那样拥有大的发展.然而90年代,Internet却进入了爆发式发展阶段,一夜之间,大家都在忙着将自己的计算机连接到网络上.这个时侯, ...

  3. pytest----fixture(1)--使用fixture执行配置及销毁逻辑

    1使用fixture执行配 置及销毁;非常灵活 使用. 2数据共享:在 conftest.py配置里写方 法可以实现数据共享, 不需要import导入.可 以跨文件共享 3scope的层次及神 奇的y ...

  4. 一个服务io占满,服务器无响应

    (1).服务器io占满,服务无响应, sar -q -f  /var/log/sa/sa28 上图显示plist-sz 增加了一倍 plist-sz 说明:进程列表中的进程(processes)和线程 ...

  5. ubuntu查看时间同步服务器的匹配源

    当服务器时间与设定好的同步时间源的时间有差异的时候,一般都需要先查看本机的时间同步服务功能是否在正常的运转,以及同步的时间源是哪里,在这里为大家提供一个检查时间用的命令. ubuntu版本 servi ...

  6. 微信小程序之自定义组件

    在微信小程序项目中 肯定会存在很多功能和样式上相似的部分 面对这种情况 只是单单的ctrl+c ctrl+v 就显得很low了,而且也不便于后期维护那么这时候 使用微信小程序中的自定义组件功能就很合适 ...

  7. Java 二叉树遍历相关操作

    BST二叉搜索树节点定义: /** * BST树的节点类型 * @param <T> */ class BSTNode<T extends Comparable<T>&g ...

  8. Android下载Android源码

    使用Git,命令是:git clone http://android.googlesource.com/platform/frameworks/base.git

  9. kafka集群安装和使用

    kafka(1)kafka是一个分布式的消息缓存系统(2)kafka集群中的服务器都叫做broker(3)kafka有两类客户端,一个叫做producer(消息生产者),一类叫做consumer(消息 ...

  10. LeetCode 707. Design Linked List (设计链表)

    题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...