[leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
题意:
求最大乘积子数组
思路:
1. the sign(I mean, if it is positive or negative) influence the product value
2. when iterating each item, it can be postive or negative
3. we use max[i] to stand for max product of ith item
4. we use min[i] to stand for min product of ith item
5. so function rule:
max[i]: max(max[i -1]*nums[i], min[i-1]* nums[i], nums[i])
min[i]: min(max[i -1]*nums[i], min[i-1]* nums[i], nums[i])
code
public int maxProduct(int[] nums) {
// initialize
int[] max = new int[nums.length];
int[] min = new int[nums.length];
max[0] = nums[0];
min[0] = nums[0];
int result = nums[0];
// max[i]: max product of ith item
// min[i]: min product of ith item
for(int i = 1; i < nums.length; i++){
max[i] = Math.max(Math.max(max[i-1] * nums[i] , min[i-1]*nums[i]), nums[i]);
min[i] = Math.min(Math.min(max[i-1] * nums[i] , min[i-1]*nums[i]), nums[i]);
result = Math.max(result, max[i]);
}
return result;
}
[leetcode]152. Maximum Product Subarray最大乘积子数组的更多相关文章
- [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
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- C#解leetcode 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152. Maximum Product Subarray最大乘积子数组/是否连续
[抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...
- Leetcode#152 Maximum Product Subarray
原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- SSM框架的配置
主要是这三个配置文件 web.xml(用来加载和初始化下面的配置文件) applicationcontet.xml(就是Spring的配置文件,一般包括声明式失误等等AOP) Sprimgmvc,xm ...
- PythonStudy——Python字典底层实现原理 The underlying implementation principle of Python dictionary
在Python中,字典是通过散列表或说哈希表实现的.字典也被称为关联数组,还称为哈希数组等.也就是说,字典也是一个数组,但数组的索引是键经过哈希函数处理后得到的散列值.哈希函数的目的是使键均匀地分布在 ...
- Yii2中多表关联查询(hasOne、hasMany、join、joinwith)
表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer (id customer_name) 订单表Order (id order_name customer_id ...
- [转]Windows下使用VS2015编译openssl库
转自:http://blog.csdn.net/alger_magic/article/details/52584171 目标:编译vs环境下openssl库 工具: 1. 编译环境win10+vs2 ...
- 计算机信息系统安全保护等级划分准则(GB 17859-1999)
概述 计算机信息系统安全保护等级划分准则(GB 17859-1999) 1 范围 本标准规定了计算机系统安全保护能力的五个等级,即: 第一级:用户自主保护级: 第二级:系统审计保护级: 第三级:安全标 ...
- MySQL内存使用查看方式
使用版本:MySQL 5.7 官方文档 在performance_schema有如下表记录内存使用情况 mysql> show tables like '%memory%summary%'; + ...
- Centos7 安装redis集群哨兵模式
https://blog.csdn.net/lihongtai/article/details/82826809
- 内训--2小时 Word精髓
企业内部使用Word最常见就是用来写产品手册与合同,产品手册是书版(即可以出版,与买到的书类似),合同重点是修订.Word的精髓在于样式,或者说在企业办公使用,学会样式就可以了.什么域.宏.VBA几乎 ...
- js运算符逻辑!和instanceof的优先级
写js时间长了,运算符优先级很可能自然而然的就形成习惯了,也不需要特别注意优先级的问题. 至少到目前为止,我也没有真正了解过js当中所有运算符的具体优先级.也没有出过什么重大的问题. 但是直到今天,在 ...
- 一针见血tomcat
一针见血tomcat 一.Tomcat各组件认知 1 Tomcat架构说明 Tomcat是一个基于JAVA的WEB容器,其实现了JAVA EE中的 Servlet 与 jsp 规范,与Nginx ap ...