当前位置:网站首页>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;
}
}
边栏推荐
- [applet automation minium] i. framework introduction and environment construction
- Prometheus入门使用(三)
- Mathematical function of MySQL function summary
- Can bus quick understanding
- Common JS modular specification from a code question
- 粒子边界碰撞的处理
- NVIDIA vid2vid paper reproduction
- 转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
- dataframe.groupby学习资料
- 精品国创《少年歌行》数字藏品开售,邀你共铸少年武侠江湖梦
猜你喜欢

转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。

Kettle實現共享數據庫連接及插入更新組件實例

Detailed tutorial of typora drawing bed configuration
![[test platform development] 20. Complete the function of sending interface request on the edit page](/img/ab/fed56b5bec990a25303c327733a8e6.png)
[test platform development] 20. Complete the function of sending interface request on the edit page

Head pose estimation principle and visualization_ Loveliuzz's blog - Programmer's Homestead_ Head posture estimation

Use of KOA framework

21 - vertical traversal of binary tree

Version correspondence between numpy and pytorch

C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)

Common SQL of Oracle Report
随机推荐
【无标题】
初识C语言函数
基于simulink的双闭环矢量控制的电压型PWM整流器仿真
494. Objectives and
Leetcode-227-basic calculator||
The self-developed data products have been iterated for more than a year. Why not buy third-party commercial data platform products?
@FeignClient使用详细教程(图解)
俄方希望有效落实农产品外运“一揽子”协议
21 - 二叉树的垂直遍历
Linux scheduled database backup script
C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)
Kettle实现共享数据库连接及插入更新组件实例
Leetcode: 17. letter combination of phone number
Kettle implémente une connexion de base de données partagée et insère une instance de composant de mise à jour
It is suggested that Siyuan notes can be compatible with third-party sync disks
基于双目相机拍摄图像的深度信息提取和目标测距matlab仿真
c语言:深度刨析const关键字
MySQL的大心脏 — 索引
[array & String & Macro exercise]
Advanced operation and maintenance 02