当前位置:网站首页>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;
}
}
边栏推荐
- HAVE FUN | “飞船计划”活动最新进展
- 如何快速检查钢网开口面积比是否符合 IPC7525
- 1亿单身男女“在线相亲”,撑起130亿IPO
- 第九届 蓝桥杯 决赛 交换次数
- Build an all in one application development platform, light flow, and establish a code free industry benchmark
- [summary of knowledge] summary of notes on using SVN in PHP
- Pycharm terminal enables virtual environment
- Geoserver2.18 series (5): connect to SQLSERVER database
- ByteDance Android gold, silver and four analysis, Android interview question app
- LeetCode 1986. 完成任务的最少工作时间段 每日一题
猜你喜欢
Sort out several important Android knowledge and advanced Android development interview questions
【DesignMode】代理模式(proxy pattern)
《产品经理必读:五种经典的创新思维模型》的读后感
作为Android开发程序员,android高级面试
As an Android Developer programmer, Android advanced interview
最新高频Android面试题目分享,带你一起探究Android事件分发机制
Personal notes of graphics (1)
[C language] question set of X
字节跳动高工面试,轻松入门flutter
掌握这个提升路径,面试资料分享
随机推荐
值得一看,面试考点与面试技巧
【PHP】PHP接口继承及接口多继承原理与实现方法
JS中null NaN undefined这三个值有什么区别
字节跳动Android面试,知识点总结+面试题解析
ATM system
最新高频Android面试题目分享,带你一起探究Android事件分发机制
Advanced C language -- function pointer
Laravel5.1 Routing - routing packets
Pisa-Proxy SQL 解析之 Lex & Yacc
Introduction to ThinkPHP URL routing
[designmode] flyweight pattern
Personal notes of graphics (2)
The difference and working principle between compiler and interpreter
Sqlserver2014+: create indexes while creating tables
正在准备面试,分享面经
Geoserver2.18 series (5): connect to SQLSERVER database
Find tags in prefab in unity editing mode
最新Android面试合集,android视频提取音频
LeetCode 1654. 到家的最少跳跃次数 每日一题
LeetCode 120. 三角形最小路径和 每日一题