当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid

字节跳动高工面试,轻松入门flutter

【DesignMode】外观模式 (facade patterns)

The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit

Pisa-Proxy SQL 解析之 Lex & Yacc

谈谈 SAP 系统的权限管控和事务记录功能的实现

Pycharm terminal enables virtual environment

Binary search tree (features)

《产品经理必读:五种经典的创新思维模型》的读后感

node:504报错
随机推荐
[Android -- data storage] use SQLite to store data
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
Three. JS series (1): API structure diagram-1
字节跳动Android面试,知识点总结+面试题解析
LeetCode 1654. 到家的最少跳跃次数 每日一题
【DesignMode】代理模式(proxy pattern)
DNS 系列(一):为什么更新了 DNS 记录不生效?
Talk about the realization of authority control and transaction record function of SAP system
Build an all in one application development platform, light flow, and establish a code free industry benchmark
skimage学习(1)
LeetCode 1031. 两个非重叠子数组的最大和 每日一题
Binary search tree (features)
【DesignMode】外观模式 (facade patterns)
Tidb cannot start after modifying the configuration file
HAVE FUN | “飞船计划”活动最新进展
Have fun | latest progress of "spacecraft program" activities
Deep listening array deep listening watch
最新Android高级面试题汇总,Android面试题及答案
作为Android开发程序员,android高级面试
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配