当前位置:网站首页>LeetCode 152. Product maximum subarray daily question
LeetCode 152. Product maximum subarray daily question
2022-07-07 16:58:00 【@Little safflower】
Problem description
Give you an array of integers nums , Please find the non empty continuous subarray with the largest product in the array ( The subarray contains at least one number ), And returns the product of the subarray .
The answer to the test case is 32- position Integers .
Subarray Is a continuous subsequence of an array .
Example 1:
Input : nums = [2,3,-2,4]
Output : 6
explain : Subarray [2,3] There is a maximum product 6.
Example 2:Input : nums = [-2,0,-1]
Output : 0
explain : The result can't be 2, because [-2,-1] It's not a subarray .
Tips :
1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
nums The product of any prefix or suffix of Guarantee It's a 32- position Integerssource : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-product-subarray
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int maxProduct(int[] nums) {
if(nums.length == 1) return nums[0];
int ans = 0;
int positive = 1;
int negative = 1;
for(int n : nums){
if(positive <= 0) positive = 1;
if(negative >= 0) negative = 1;
if(n < 0){
int t = negative;
negative = positive * n;
positive = t * n;
}else {
negative *= n;
positive *= n;
}
if(positive > ans) ans = positive;
}
return ans;
}
}
边栏推荐
- 二叉搜索树(基操篇)
- null == undefined
- 谈谈 SAP 系统的权限管控和事务记录功能的实现
- URL和URI的关系
- LeetCode 1626. 无矛盾的最佳球队 每日一题
- QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
- Talk about the realization of authority control and transaction record function of SAP system
- Lowcode: four ways to help transportation companies enhance supply chain management
- Lie cow count (spring daily question 53)
- 1亿单身男女“在线相亲”,撑起130亿IPO
猜你喜欢
随机推荐
编程模式-表驱动编程
【医学分割】attention-unet
os、sys、random标准库主要功能
应用在温度检测仪中的温度传感芯片
QT视频传输
Imitate the choice of enterprise wechat conference room
A tour of gRPC:03 - proto序列化/反序列化
AutoLISP series (3): function function 3
null == undefined
Cesium (4): the reason why gltf model is very dark after loading
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Opencv configuration 2019vs
爬虫(17) - 面试(2) | 爬虫面试题库
Prometheus API deletes all data of a specified job
Find tags in prefab in unity editing mode
time标准库
【图像传感器】相关双采样CDS
Read PG in data warehouse in one article_ stat
全网“追杀”钟薛高
[summary of knowledge] summary of notes on using SVN in PHP