当前位置:网站首页>152. Product maximum subarray
152. Product maximum subarray
2022-07-23 14:59:00 【ATTACH_ Fine】
subject
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 :
!!! Be careful
For multiplication , We need to pay attention , Negative number multiplied by negative number , Will become a positive number , So when solving this problem, we need Maintain two variables , Current maximum , And the minimum , The minimum value may be negative , But maybe the next step is to multiply by a negative number , The current maximum becomes the minimum , And the minimum becomes the maximum .
Code
class Solution {
public int maxProduct(int[] nums) {
int len = nums.length;
int[] maxNum = new int[len];
int[] minNum = new int[len];
int ans = nums[0];
// initialization
maxNum[0] = nums[0];
minNum[0] = nums[0];
for(int i = 1; i < len; i++){
maxNum[i] = Math.max(nums[i],Math.max(maxNum[i-1]* nums[i] , minNum[i-1] * nums[i]));
minNum[i] = Math.min(nums[i],Math.min(minNum[i-1]*nums[i],maxNum[i-1] * nums[i]));
ans = Math.max(ans,maxNum[i]);
}
return ans;
}
}
边栏推荐
- Advanced operation and maintenance 02
- FastAPI应用加入Nacos
- leetcode: 17. 电话号码的字母组合
- day18
- [untitled]
- 【软件测试】盘一盘工作中遇到的 Redis 异常测试
- [test platform development] 23. interface assertion function - save interface assertion and edit echo
- [software test] MQ abnormal test encountered in disk-to-disk work
- 什么是Promise?Promise有什么好处
- 利用shell脚本实现封禁扫描频率过高的ip
猜你喜欢
![[test platform development] 21. complete sending interface request and display response header information](/img/53/42411ceb6e0e304355ddc396ea2922.png)
[test platform development] 21. complete sending interface request and display response header information

Quick introduction to PKI system

OpenHarmony南向学习笔记——Hi3861+HC-SR04超声波检测
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

mysql唯一索引无重复值报错重复

c语言:深度刨析const关键字

基于双目相机拍摄图像的深度信息提取和目标测距matlab仿真

Qu'est - ce que le codage par titre?

QT document reading notes audio example analysis

运维高级作业03
随机推荐
[turn] functional area division based on poi ()
Prometheus入门使用(三)
什么是Per-Title编码?
FastAPI应用加入Nacos
基于simulink的双闭环矢量控制的电压型PWM整流器仿真
Deep learning single image 3D face reconstruction
真人踩过的坑,告诉你避免自动化测试常犯的10个错误
反射调用事务方法导致事务失效
【测试平台开发】二十、完成编辑页发送接口请求功能
易基因|靶基因DNA甲基化测序(Target-BS)
338. Bit count
AI acceleration gesture recognition experience based on efr32mg24
NVIDIA vid2vid paper reproduction
Reflection invokes transaction methods, resulting in transaction invalidation
cmake笔记
c语言:深度刨析const关键字
读写锁ReadWriteLock还是不够快?再试试S…
Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing
[software test] MQ abnormal test encountered in disk-to-disk work
Feignclient utilise un tutoriel détaillé (illustration)