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

一、最大连续子序列和

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. java MySQl数据库连接

    <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverMan ...

  2. js模块化的历史

    了解模块化开发的历史,可以帮助我们理解 模块化开发的形式是怎么样的,对我们深入学习模块化开发会有很大的帮助: 一.服务器端JS的模块化 nodeJS的出现   ------官网: http://nod ...

  3. the blank final field factors may not have been initialized

    Q1: why we should initialize final field before completion of new instance? final means no changeabl ...

  4. java oop第15章_Socket网络编程

    一.   TCP/IP协议(Transmission Control Protocol/Internet Protocol)传输控制协议/Internet协议,是通信领域的基础.核心协议, 其他的协议 ...

  5. Ubuntu 常用软件记录【持续更新】

    主机之间通信 Shell 管理器: asbru-cm 文件传输工具: filezilla 虚拟化 Virtual box

  6. 关于while循环中的break和continue的区别

    while循环如果不加条件限制的话,它会一直循环下去,那么问题就来了,如果我不用条件去终止while循环的话,那么我该用什么方法去终止呢? 你可以选择两种终止while循环的方法 1.break  强 ...

  7. 检测apache状态,当apache处于非running状态如何脚本启动

    编写脚本内容 #!/bin/bashURL="http://127.0.0.1/"curlit(){curl --connect-timeout 15 --max-time 20 ...

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

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

  9. lterator遍历

    iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制 作用: 1.为各种数据结构,提供一个统一的.简便的访问接口: 2.使得数据结构的成员能够按某种次序排列 3.ES6创造了一种新的 ...

  10. leetcode-126-单词接龙

    题目描述: class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: list) -> list ...