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 ...
随机推荐
- ActionContext 与 ServletActionContext获取Session的异同
1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...
- 【转】HTML5标签使用的常见误区
最近组内进行 HTML5标签的学习,方法呢就是大家每人挑选几个标签,自己先去学习,然后给大家作讲解.这个过程大家还是挺有收获的.但是现在HTML5还处在草案阶 段,有些新的标签元素的解释也是经常有变化 ...
- Java 自动检测文本文件编码
private String guessCharset(InputStream is) throws IOException { return new TikaEncodingDetector().g ...
- docker Dockerfile学习---nginx负载均衡tomcat服务
1.此过程在nginx的基础上,也就是上篇博客写的内容. 2.创建项目目录并上传包,解压 $ mkdir centos_tomcat $ cd centos_tomcat $ tar zxvf jdk ...
- 爬虫那些事儿--Http返回码
由于爬虫的抓取也是使用http协议交互.因此需要了解Http的各种返回码所代表的意义,才能判断爬虫的执行结果. 返回码如下: 100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部 ...
- 笔记56 Mybatis快速入门(七)
相关概念介绍(二) 6.一级缓存 <1>在一个session里查询相同id的数据 package mybatis.annotation; import java.io.IOExceptio ...
- Apache 环境变量配置
在path 中加入 C:\__S_D_K__\AndroidApache\apache-ant-1.9.14\bin 我的路径在C盘
- 使用idea开发分布式项目中优化tomact的方法
1. idea内存优化 找到idea安装目录,我的是在D:\IDEA\bin目录中 找到idea.exe.vmoptions和idea64.exe.vmoptions文件 这两个文件全部改成如下配置, ...
- AtCoder ARC061E Snuke's Subway Trip 最短路
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门 Portal 原题目描述在最下面. \(n(1 ...
- 英语影视台词---The Professor
英语影视台词---The Professor 一.总结 一句话总结: brilliant and liberty:厉害且自在 understand and forgive and not care:f ...