当前位置:网站首页>Force deduction 152 question multiplier maximum subarray
Force deduction 152 question multiplier maximum subarray
2022-07-06 13:54:00 【AI is actually very simple】
152. The largest subarray of multipliers
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 .
Strategy : This topic is the topic of dynamic programming , You can solve the answer from the bottom up , Traverse the entire array to calculate the current maximum value with imax For recording , Constantly update and finally solve the answer . Because there are negative numbers in the array , Will change the maximum value to the minimum value , The minimum becomes the maximum , So we need to imin Record the minimum value . When seeking the first i Number of hours : If the value of this point is positive , Then the maximum value of this point may be the maximum value of the previous number multiplied by the value of this point , Or the value of this point (imax=fmax(imaxnums[i],nums[i])) The minimum value is (imin=fmin(iminnums[i],nums[i]);); If the value of this point is negative , Exchange the maximum value with the minimum value , Then solve .
Code :
int maxProduct(int* nums, int numsSize){
int imax=1,imin=1,max=INT_MIN;
for(int i=0;i<numsSize;i++){
if(nums[i]<0){
// When a negative number is encountered, the maximum value will become the minimum value , The minimum becomes the maximum Exchange position at this time
int temp=imin;
imin=imax;
imax=temp;
}
imax=fmax(imax*nums[i],nums[i]);
imin=fmin(imin*nums[i],nums[i]);
max=fmax(max,imax); // Compare the past maximum with the maximum at this point
}
return max;
}
边栏推荐
- 杂谈0516
- 甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
- Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
- Nuxtjs quick start (nuxt2)
- [面試時]——我如何講清楚TCP實現可靠傳輸的機制
- Mixlab unbounded community white paper officially released
- Read only error handling
- The latest tank battle 2022 full development notes-1
- MATLAB打开.m文件乱码解决办法
- 1. Preliminary exercises of C language (1)
猜你喜欢
随机推荐
Miscellaneous talk on May 27
自定义RPC项目——常见问题及详解(注册中心)
Strengthen basic learning records
Analysis of penetration test learning and actual combat stage
【数据库 三大范式】一看就懂
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
Nuxtjs quick start (nuxt2)
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
Cookie和Session的区别
杂谈0516
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
Inaki Ading
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
[modern Chinese history] Chapter V test
C语言入门指南
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
2. First knowledge of C language (2)
【educoder数据库实验 索引】
7-1 输出2到n之间的全部素数(PTA程序设计)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication









