当前位置:网站首页>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;
}
边栏推荐
- Mortal immortal cultivation pointer-2
- Brief introduction to XHR - basic use of XHR
- 强化學習基礎記錄
- 透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
- [modern Chinese history] Chapter V test
- 4. Branch statements and loop statements
- 渗透测试学习与实战阶段分析
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- (original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
- Inaki Ading
猜你喜欢
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
canvas基础2 - arc - 画弧线
[hand tearing code] single case mode and producer / consumer mode
C语言入门指南
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
3. Input and output functions (printf, scanf, getchar and putchar)
Using spacedesk to realize any device in the LAN as a computer expansion screen
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
MATLAB打开.m文件乱码解决办法
. How to upload XMIND files to Jinshan document sharing online editing?
随机推荐
4. Branch statements and loop statements
这次,彻底搞清楚MySQL索引
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
[modern Chinese history] Chapter 6 test
UGUI—Text
仿牛客技术博客项目常见问题及解答(二)
SRC挖掘思路及方法
Canvas foundation 1 - draw a straight line (easy to understand)
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
ArrayList的自动扩容机制实现原理
自定义RPC项目——常见问题及详解(注册中心)
7-8 7104 约瑟夫问题(PTA程序设计)
7-7 7003 组合锁(PTA程序设计)
5月27日杂谈
编写程序,模拟现实生活中的交通信号灯。
C语言入门指南
7-9 制作门牌号3.0(PTA程序设计)
Mortal immortal cultivation pointer-1
The difference between abstract classes and interfaces