Algo: maxSubArray vs. maxProduct
这两个问题类似,都可利用动态规划思想求解。
一、最大连续子序列和
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的更多相关文章
- MaxSubArray 最大子数列和
public int maxSubArray(int[] A) { int newsum=A[0]; int max=A[0]; for(int i=1;i<A.length;i++){ new ...
- maxSubArray
Description: Find the contiguous subarray within an array (containing at least one number) which has ...
- 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 ...
- algo: 冒泡排序(Java实现)
package com.liuxian.algo; public class MySortClass implements Comparable<MySortClass> { public ...
- 【algo&ds】4.B树、字典树、红黑树、跳表
上一节内容[algo&ds]4.树和二叉树.完全二叉树.满二叉树.二叉查找树.平衡二叉树.堆.哈夫曼树.散列表 7.B树 B树的应用可以参考另外一篇文章 8.字典树Trie Trie 树,也叫 ...
- [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 ...
- ALGO基础(一)—— 排序
ALGO基础(一)-- 排序 冒选插希快归堆,以下均为从小到大排 1 冒泡排序 描述: 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一 ...
- 蓝桥杯 algo——6 安慰奶牛 (最小生成树)
问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是一个奶牛的家.FJ计 划除去P条道路中尽可能多的道路 ...
- [MIT Intro. to algo]Lecture 1: 课程介绍,算法优势,插入算法和归并算法分析,渐近符号
The theoretical study of computer program performance and resource useage. First, analysis and the ...
随机推荐
- centos7简单安装配置mariadb
CentOS 7下yum安装MariaDB yum install mariadb mariadb-server systemctl start mariadb #启动mariadb systemct ...
- 时间同步服务器NTP搭建
NTP服务器 NTP(Network Time Protocol)[网络时间协议],它是用来同步网络中各个计算机的时间的协议,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒, ...
- 7年Java后端被淘汰,一路北漂辛酸史。。。
作者:春天花会开foryou oschina.net/question/3465562_2281392 今天分享一位同行的经历: 本人Java开发6年半不到7年的样子. 英语专业,临毕业跟着隔壁专业去 ...
- 用JS获取地址栏参数的方法(转)
方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new RegExp("( ...
- 38-Ubuntu-用户管理-03-usermod指定用户登录shell
简记: 所谓shell就是可以输入终端命令的窗口,shell是一个软件. 1.Ubuntu终端shell介绍 summmer@summmer-virtual-machine:~/桌面$ summmer ...
- 本地调试H5页面
摘要 详细讲述微信H5页面调试(安装在安卓或iOS手机上的),钉钉内H5页面的调试,QQ.微博以及各浏览器上H5页面的调试方法 背景 大学毕业快要一年了,用leader的话说我也是有一年开发经验的前端 ...
- Html+css编写太阳星系
我们都知道太阳系是以太阳为中心的,和所有受到太阳的引力约束天体的集合体.包括八大行星(由离太阳从近到远的顺序:水星.金星.地球.火星.木星.土星.天王星.海王星),而我用html和css所写的就是八大 ...
- 枚举对象中的属性 for . . in
枚举对象中的属性:把对象中所有的属性和值都取出来 使用 for . . . in 语句 语法: for(var 变量 in 对象){ } for . . . in 语句 对象中有几个属性,循 ...
- 微信小程序改变全局变量
假设A为登录页面并将登录获得的用户信息保存到app.js中的全局变量userInfo中,然后在B页面进行使用. app.js globalData:{ userInfo:null, } a.js ...
- (PASS)字符数组\字符串数组 和 字符串 的相互转换
1,字符数组 转换为 字符串 java可以使用两种方法直接将字符数组转为字符串. 方法1:直接在构造String时转换. char[] data = {'a', 'b', 'c'}; String s ...