【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.
求连续子序列最大乘积,类似于求连续子序列最大和,但是乘法还涉及到符号的问题,比较方便的方法是同时维护到当前元素的最大乘积和最小乘积。
class Solution {
public:
int maxProduct(int A[], int n) {
int max_ending_here = A[];
int min_ending_here = A[];
int max_so_far = A[];
for (int i = ; i < n; ++i) {
int p_max = A[i] * max_ending_here;
int p_min = A[i] * min_ending_here;
max_ending_here = max(A[i], max(p_max, p_min));
min_ending_here = min(A[i], min(p_max, p_min));
max_so_far = max(max_so_far, max_ending_here);
}
return max_so_far;
}
};
【Leetcode】Maximum Product Subarray的更多相关文章
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】【Medium】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【leetcode】Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
随机推荐
- canvas二进制字符下落
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...
- Unity调试设置
[Unity调试设置] 1.Mac中,"Unity"->"Preferences...". Windows中,"Edit"->& ...
- zookeeper更进一步(数据模型、watcher及shell命令)
ZooKeeper数据模型 ZooKeeper 的数据模型,在结构上和标准文件系统的非常相似,拥有一个层次的命名空间,都是采用树形层次结构,ZooKeeper 树中的每个节点被称为—Znode.和文件 ...
- yii2 源码分析1从入口开始
我是在 backend 一步步打印的 很多地方我也是很模糊 .后来发现一位大神的文章(http://www.yiichina.com/tutorial/773) 参考文章自己动手开始写的 至于后来的 ...
- 百度Ueditor编辑器取消多图上传对话框中的图片搜索
百度Ueditor确实是一个非常强悍的编辑器,功能强大!但是实际开发需求复杂,总会有各种不符合要求的,比如想要取消多图上传的“图片搜索”选项卡(这个图片搜索真心难用)! 以ueditor 1.4.3为 ...
- =面试题:java面试基本方向 背1 有用 项目二技术学完再看
一.Java基础 1. 集合框架A)集合中泛型优点? 将运行期的ClaasCastException 转到编译期异常. 泛型还提供通配符 1)HashMap---允许一个键为null,允许多个值为n ...
- jQuery--基础知识速查表
一.jQuery选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的 ...
- vue.js的一些模板指令简述
1.模板指令都是写在<template></template>html里面 v-text : value是什么就显示什么,如果value里面有html的标签,也会直接显示出 ...
- 何为软件的Alpha、Beta、RC和GA发布版本?
简介 一个软件或者一个功能在发布时,通常会有Beta版这么一说.我很熟悉,差不多知道是什么意思,但没去深究,感觉上就是一个可以用但不保证功能稳定的版本. 直到昨天我看到了 MariaDB 数据库发布标 ...
- C# winform中Setting.settings 相关知识点
1.在Settings.settings文件中定义配置字段.包含字段名.类型.范围.值四部分的属性. 字段名.类型和值类似编程中字段的定义一样使用,不再过多的解释.重点讲一下”范围“字段的含义与区别. ...