File tree Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change @@ -251,6 +251,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
251251- [ Bus Fare] ( ./problems/Bus-Fare.md ) 👍
252252- [ Minimum Dropping Path Sum] ( ./problems/Minimum-Dropping-Path-Sum.md )
253253- [ Longest-Matrix-Path-Length] ( ./problems/Longest-Matrix-Path-Length.md )
254+ - [ Every Sublist Min Sum] ( ./problems/Every-Sublist-Min-Sum.md )
254255
255256- [ 0002. 两数相加] ( ./problems/2.add-two-numbers.md )
256257- [ 0003. 无重复字符的最长子串] ( ./problems/3.longest-substring-without-repeating-characters.md )
Original file line number Diff line number Diff line change 104104 - [ Number of Substrings with Single Character Difference] ( ./problems/Number-of-Substrings-with-Single-Character-Difference.md )
105105 - [ Bus Fare] ( ./problems/Bus-Fare.md ) 👍
106106 - [ Minimum Dropping Path Sum] ( ./problems/Minimum-Dropping-Path-Sum.md )
107+ - [ Every Sublist Min Sum] ( ./problems/Every-Sublist-Min-Sum.md )
107108 - [ 0002. 两数相加] ( ./problems/2.add-two-numbers.md )
108109 - [ 0003. 无重复字符的最长子串] ( ./problems/3.longest-substring-without-repeating-characters.md )
109110 - [ 0005. 最长回文子串] ( ./problems/5.longest-palindromic-substring.md )
Original file line number Diff line number Diff line change 1+ # 题目地址(Every Sublist Min Sum)
2+
3+ https://binarysearch.com/problems/Every-Sublist-Min-Sum
4+
5+ ## 题目描述
6+
7+ ```
8+ You are given a list of integers nums. Return the sum of min(x) for every sublist x in nums. Mod the result by 10 ** 9 + 7.
9+
10+ Constraints
11+
12+ n ≤ 100,000 where n is the length of nums
13+ Example 1
14+ Input
15+ nums = [1, 2, 4, 3]
16+ Output
17+ 20
18+ Explanation
19+ We have the following sublists and their mins:
20+
21+ min([1]) = 1
22+ min([1, 2]) = 1
23+ min([1, 2, 4]) = 1
24+ min([1, 2, 4, 3]) = 1
25+ min([2]) = 2
26+ min([2, 4]) = 2
27+ min([2, 4, 3]) = 2
28+ min([4]) = 4
29+ min([4, 3]) = 3
30+ min([3]) = 3
31+
32+ ```
33+
34+ ## 前置知识
35+
36+ - 单调栈
37+
38+ ## 公司
39+
40+ - 暂无
41+
42+ ## 单调栈
43+
44+ ### 思路
45+
46+ 我们可以枚举得到答案。具体的枚举策略为:
47+
48+ - 假设以索引 0 的值为最小值且包含索引 0 的子数组个数 c0。 其对答案的贡献为 ` c0 * nums[0] `
49+ - 假设以索引 1 的值为最小值且包含索引 1 的子数组个数 c1。 其对答案的贡献为 ` c1 * nums[1] `
50+ - 。。。
51+ - 假设以索引 n-1 的值为最小值且包含索引 n-1 的子数组个数 cn。其对答案的贡献为 ` cn * nums[n-1] `
52+
53+ 上述答案贡献之和即为最终答案。
54+
55+ 接下来我们考虑分别如何计算上面的子贡献。
56+
57+ 使用单调栈可以很容易地做到这一点,因为单调栈可以回答** 下一个(上一个)更小(大)的元素的位置** 这个问题。
58+
59+ 对于 i 来说,我们想知道下一个更小的位置 r ,以及上一个更小的位置 l。 这样 i 对答案的贡献就是 ` (r-i)*(i-l)*nums[i] `
60+
61+ 代码上,我们处理到 i 的时候,不是计算 i 对答案的贡献,而是计算出从栈中弹出来的索引 last 对答案的贡献。这可以极大的简化代码。具体见下方代码区。
62+
63+ 为了简化逻辑判断,我们可以使用单调栈常用的一个技巧:** 虚拟元素** 。这里我们可以往 nums 后面推入一个比所有 nums 的值都小的数即可。
64+
65+ ### 关键点
66+
67+ - 分别计算以每一个被 pop 出来的为最小数的贡献
68+
69+ ### 代码
70+
71+ 代码支持:Python
72+
73+ Python3 Code:
74+
75+ ``` py
76+
77+ class Solution :
78+ def solve (self , nums ):
79+ nums += [float (' -inf' )]
80+ mod = 10 ** 9 + 7
81+ stack = []
82+ ans = 0
83+
84+ for i, num in enumerate (nums):
85+ while stack and nums[stack[- 1 ]] > num:
86+ last = stack.pop()
87+ left = stack[- 1 ] if stack else - 1
88+ ans += (i - last) * (last - left) * nums[last]
89+ stack.append(i)
90+ return ans % mod
91+
92+ ```
93+
94+ ** 复杂度分析**
95+
96+ 令 n 为 nums 长度
97+
98+ - 时间复杂度:$O(n)$
99+ - 空间复杂度:$O(n)$
100+
101+ 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 46K star 啦。
102+ 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
103+ ![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg )
You can’t perform that action at this time.
0 commit comments