当前位置:网站首页>leetcode:1856. 子数组最小乘积的最大值
leetcode:1856. 子数组最小乘积的最大值
2022-06-24 06:32:00 【OceanStar的学习笔记】
题目来源
题目描述
给定一个只包含正数的数组arr,arr中任何一个子数组sub,一定都可以算出(sub累加和 )* (sub中的最小值)是什么,
那么所有子数组中,这个值最大是多少?
题目解析
题意
子数组是连续的。每一个子数组的累加和都可以算出来,这段数组的最小值也能求得,然后 A指标 = (sub累加和 )* (sub中的最小值)。
现在我们要求出当前数组中哪个子数组的A指标最大。
思考
问题:
- 以0位置的 3 3 3作为最小值的子数组有哪些?
- [ 3 ] : 3 ∗ 3 = 9 [3]:3*3 = 9 [3]:3∗3=9
- [ 3 , 4 ] : 3 ∗ 7 = 21 [3,4]:3*7=21 [3,4]:3∗7=21,选它
- 以 1 − − > 4 1-->4 1−−>4作为最小值的子数组有哪些?
- 4 : 4 ∗ 4 = 16 4:4*4=16 4:4∗4=16
- 以 2 − − > 2 2-->2 2−−>2作为最小值的子数组有哪些?
- 得到:[3,4,2,3,4,6]$
- 2尽量往左扩大,最多能扩到 [ 3 , 4 , 2 [3,4,2 [3,4,2
- 2尽量往右扩张,最大能扩到…2,3,4,6]
- 为什么要尽量扩张,因为 指 标 A = s u m ∗ n u m 指标A = sum * num 指标A=sum∗num,指标A要尽量大,而num是固定的,所以sum应该尽量大
- [ 3 , 4 , 2 , 3 , 4 , 6 ] [3,4,2,3,4,6] [3,4,2,3,4,6]含有 2 2 2的所有子数组的指标A都会比2小
- 得到:[3,4,2,3,4,6]$
- 以 3 − − > 3 3-->3 3−−>3作为最小值的子数组有哪些?
- 。。。
- 以 4 − − > 4 4-->4 4−−>4作为最小值的子数组有哪些?
- 。。。
- 以 5 − − > 6 5-->6 5−−>6作为最小值的子数组有哪些?
- 。。。
第一个要解决的问题:
- 如何找出以 x x x位置作为最小值的子数组的指标A最大呢?
- x尽量往左看,找到比x更小的那个索引,假设为left
- x尽量往右看,找到比x更小的那个索引,假设为right
- 然后[left+1…right+1]这个区间范围内的所有数组全要,得到sum,然后sum * 2
第二个要解决的问题:求sum
- 用前缀和
注意,这里,单调栈不需要用链表。举个例子
(1)遍历数组,并入栈
- 1—>4出栈,需要结算答案,得到
- 1—>4:0—>3,2—>3
- 也就是对于1—>4来说,它左扩不到0—>3位置,右扩不到2—>3,因此它只有它自己,{4},得到4*4 = 16
那现在2—>3能入栈吗?不能,因为栈顶元素也是3,所以0—>3出栈:
- 0—>3出栈
- 左边比我小的数:没有
- 右边比我小的数:如果按照原来单调性的性质,应该是2—>3,但是实际上不对,应该是4—>3。怎么处理呢?
- 无需处理,错了就错了,因为 0—>3和2—>3、4—>3是联通的,有朝一日会自动纠正的
- 也就是说得到了子数组{3,4},得到了7*3=21
现在栈变为:
继续遍历:
3—>4出栈:
- 左边比 3—>4小的数:2—>3
- 右边比 3—>4小的数:4—>3
- 所以 3—>4向左扩到2—>3(不包括)为止,向右扩到4—>3(不包括)为止。得到子数组[4],最终得到指标A=4*4=16
2–>3也需要出栈:
- 左边比2–>3小的数:无
- 右边比2–>3小的数:4—>3
继续遍历:5—>1可以入栈
5—>1可以入栈吗?不可以,为了满足栈单调增长,必须将4—>3出栈, 5—>1才能入栈
- 4—>3出栈
- 右边比 4—>3小的数:5—>1,往右扩展到索引5(不包括)位置
- 左边比4—>3小的数:没有,所以4—>3可以一直往左扩大,将索引4左边的所有数都放入到当前子数组中
- 最终得到的子数组:[3、4、3、4、3]
小结:反正最后一个能算对,所以之前错了就错了,no care
实现
边栏推荐
- Nine possibilities of high CPU utilization
- 创客教育给教师发展带来的挑战
- The new version of Tencent Youtu ncnn is suitable for domestic CPUs, and the maximum speed is increased by 70 times
- Web automation test (3): Selenium basic course of web function automation test
- Introduction to QWidget attribute table in QT Designer
- Tencent security release data security compliance capability map
- Excel data extraction technique: a universal formula for extracting numbers from mixed text
- When easynvs is deployed on the project site, easynvr cannot view the corresponding channel. Troubleshooting
- Record of waic 2021 round table conference 𞓜 cross border dialogue: current situation and future of AI and sustainable development
- Small programs import Excel data in batches, and cloud development database exports CVS garbled code solution
猜你喜欢
ServiceStack. Source code analysis of redis (connection and connection pool)
The product layout is strengthened, the transformation of digital intelligence is accelerated, and FAW Toyota has hit 2022million annual sales
云上本地化运营,东非第一大电商平台Kilimall的出海经
puzzle(019.1)Hook、Gear
【JUC系列】Executor框架之CompletionFuture
[fault announcement] one stored procedure brings down the entire database
基于三维GIS系统的智慧水库管理应用
创客教育给教师发展带来的挑战
Oracle case: ohasd crash on AIX
Manual for automatic testing and learning of anti stepping pits, one for each tester
随机推荐
Wireshark grabs the RTSP stream of easynvr without displaying RTSP. Solution
Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.
Analysis of official template of wechat personnel recruitment management system (I)
Produce kubeconfig with permission control
The difference between ArrayList and LinkedList and the principle of using scene locality
10 year old drivers who have been engaged in software testing tell you what type of software is suitable for automation
Authoritative recognition! Tencent cloud data security Zhongtai was selected as the 2021 pioneer practice case
What is an enterprise mailbox domain name? How to register an enterprise mailbox domain name
Oracle case: ohasd crash on AIX
Easyscreen live streaming component pushes RTSP streams to easydarwin for operation process sharing
EEG microstate as a continuous phenomenon
【JUC系列】Executor框架之CompletionFuture
puzzle(019.1)Hook、Gear
Attack and defense enlightenment: chromium component risk analysis and convergence
[5 minutes to play lighthouse] take you to the light kubernetes release k3s
Architecture: rest and HATEOAS
What is the difference between level 1, level 2 and level 3 domain names? How to register domain names
How does easyplayer RTSP configure sending heartbeat information to the server?
【二叉数学习】—— 树的介绍
Go current limiting component package rate source code analysis