当前位置:网站首页>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
实现
边栏推荐
- Easyrtc call error `failed to execute'send'on'rtcdatachannel'
- Analysis on the influence of "network security policy issued successively" on Enterprises
- Multi objective Optimization Practice Based on esmm model -- shopping mall
- Kangaroo cloud: the overall architecture and key technical points of building a real-time computing platform based on Flink
- 35岁危机?内卷成程序员代名词了
- Printer connection mode
- Word cannot copy and paste processing method
- Comparison of common layout solutions (media query, percentage, REM and vw/vh)
- Analysis of official template of wechat personnel recruitment management system (III)
- Urban Waterlogging Monitoring and early warning system
猜你喜欢

Enter the software test pit!!! Software testing tools commonly used by software testers software recommendations

Manual for automatic testing and learning of anti stepping pits, one for each tester

【JUC系列】Executor框架之CompletionFuture

目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”
Fault analysis | using --force to batch import data leads to partial data loss

35岁危机?内卷成程序员代名词了

The product layout is strengthened, the transformation of digital intelligence is accelerated, and FAW Toyota has hit 2022million annual sales

云上本地化运营,东非第一大电商平台Kilimall的出海经

创客教育给教师发展带来的挑战

基于三维GIS系统的智慧水库管理应用
随机推荐
基于三维GIS系统的智慧水库管理应用
Get the short video! Batch download of Kwai video (with source code)
Why the computer can't start
Intranet environment request Tencent cloud 3.0 API details
Distributed cache breakdown
Member management system PC side building tutorial (I)
puzzle(019.1)Hook、Gear
Oracle case: ohasd crash on AIX
Produce kubeconfig with permission control
Feign request return value inverse sequence localdatetime exception record
线程安全与实现方法
EEG microstate as a continuous phenomenon
WordPress pill applet build applet from zero to one [pagoda panel installation configuration]
Deploy DNS server using dnsmasq
What is the difference between level 1, level 2 and level 3 domain names? How to register domain names
How does go limit the flow of services?
Station B collapsed. Let's talk to the injured programmers
Analysis of official template of wechat personnel recruitment management system (II)
Flexible use of distributed locks to solve the problem of repeated data insertion
How to choose CMS website system for website construction