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 ...
随机推荐
- docker hub的使用
讲自己的镜像推送到docker hub 一.将自己的image上标签 docker tag ubuntu:18.04 username/ubuntu:18.04 二.登陆自己的docker hub d ...
- vmstat - 报告虚拟内存的统计信息
总览 vmstat [-n] [延时[次数]] vmstat [-V] 描述 vmstat 对系统的进程情况.内存使用情况.交换页和 I/O 块使用情况.中断以及 CPU 使用情况进行统计并报告相应的 ...
- HashMap 1.7 与 1.8 的 区别,说明 1.8 做了哪些优化,如何优化的
JDK1.7用的链表散列结构,JDK1.8用的红黑树 在扩充HashMap的时候,JDK1.7的重新计算hash, JDK1.7只需要看看原来的hash值新增的那个bit是1还是0就好了,是0的话索引 ...
- python操作redis数据
一.环境安装 1.redispy安装 (automatic) C:\Users\Administrator>pip install redis 2.检测是否安装成功 (automatic) C: ...
- c 语言函数分析
第一个参数为指向线程标识符的指针. 第二个参数用来设置线程属性. 第三个参数是线程运行函数的起始地址. 最后一个参数是运行函数的参数. result = pthread_create(&tid ...
- No identifier specified for entity: com.XXX.XXX...
这种情况一般是没有在属性上加@Id注解导致的. @Entity @Data @Table(name = "hl_role_module") public class RoleMod ...
- java----int,string 转化为long
String: 1.返回Long包装类型: String str = "aaa"; long l = Long.parseLong([str]); 2.返回long基本数据类型: ...
- Springboot 上传文件
@PostMapping("/upload")//springboot可以直接扫描resource下的static文件夹下的静态文件 public String upload(@R ...
- Quick BI功能篇之(一):20分钟入门
前言: 最近小编帮助隔壁团队一个小姐姐解决了个大难题:给老板汇报业绩分析,频次提高.效率提升,还得保证团队中的小伙伴们都得有点大数据时代的基本数据能力.小编觉得这么好的经验可以分享给更多志同道合的朋友 ...
- web前端开发2018年12月找工作总结
2018年的冬天额外的冷,由内致外... 作为一名刚刚踏入社会的实习生,可谓是狠狠的体验了一把什么叫社会(同时也感叹父母赚钱真的很不容易) 前几天看见这样一句话"如果你不知道社会的辛苦,要么 ...