当前位置:网站首页>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;
}
边栏推荐
- Renforcer les dossiers de base de l'apprentissage
- . Net6: develop modern 3D industrial software based on WPF (2)
- [the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
- [modern Chinese history] Chapter 6 test
- Mortal immortal cultivation pointer-1
- Read only error handling
- Programme de jeu de cartes - confrontation homme - machine
- Redis实现分布式锁原理详解
- 简单理解ES6的Promise
- 2. First knowledge of C language (2)
猜你喜欢

SRC mining ideas and methods

C language Getting Started Guide

Using spacedesk to realize any device in the LAN as a computer expansion screen

Yugu p1012 spelling +p1019 word Solitaire (string)

.Xmind文件如何上传金山文档共享在线编辑?

Meituan dynamic thread pool practice ideas, open source

A piece of music composed by buzzer (Chengdu)
![[dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i

A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews

. Net6: develop modern 3D industrial software based on WPF (2)
随机推荐
Inaki Ading
FAQs and answers to the imitation Niuke technology blog project (II)
The latest tank battle 2022 - Notes on the whole development -2
3. C language uses algebraic cofactor to calculate determinant
强化学习基础记录
MySQL锁总结(全面简洁 + 图文详解)
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
Callback function ----------- callback
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
强化學習基礎記錄
Brief introduction to XHR - basic use of XHR
Meituan dynamic thread pool practice ideas, open source
[面试时]——我如何讲清楚TCP实现可靠传输的机制
编写程序,模拟现实生活中的交通信号灯。
附加简化版示例数据库到SqlServer数据库实例中
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
C语言入门指南
5月14日杂谈
MySQL中count(*)的实现方式
Why use redis