Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2e663d

Browse files
author
robot
committed
feat: 385,ドル1589ドル
1 parent cdca788 commit d2e663d

File tree

3 files changed

+318
-0
lines changed

3 files changed

+318
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
319319
- [0365. 水壶问题](./problems/365.water-and-jug-problem.md)
320320
- [0378. 有序矩阵中第 K 小的元素](./problems/378.kth-smallest-element-in-a-sorted-matrix.md)
321321
- [0380. 常数时间插入、删除和获取随机元素](./problems/380.insert-delete-getrandom-o1.md)
322+
- [0385. 迷你语法分析器](./problems/385.mini-parser.md)
322323
- [0394. 字符串解码](./problems/394.decode-string.md) 91
323324
- [0416. 分割等和子集](./problems/416.partition-equal-subset-sum.md)
324325
- [0424. 替换后的最长重复字符](./problems/424.longest-repeating-character-replacement.md)
@@ -389,6 +390,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
389390
- [1438. 绝对差不超过限制的最长连续子数组](./problems/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md)
390391
- [1558. 得到目标数组的最少函数调用次数](./problems/1558.minimum-numbers-of-function-calls-to-make-target-array.md)
391392
- [1574. 删除最短的子数组使剩余数组有序](./problems/1574.shortest-subarray-to-be-removed-to-make-array-sorted.md)
393+
- [1589. 所有排列中的最大和](./problems/1589.maximum-sum-obtained-of-any-permutation.md)
392394
- [1631. 最小体力消耗路径](./problems/1631.path-with-minimum-effort.md)
393395
- [1658. 将 x 减到 0 的最小操作数](./problems/1658.minimum-operations-to-reduce-x-to-zero.md)
394396
- [1697. 检查边长度限制的路径是否存在](./problems/1697.checking-existence-of-edge-length-limited-paths.md)
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
## 题目地址(1589. 所有排列中的最大和)
2+
3+
https://leetcode-cn.com/problems/maximum-sum-obtained-of-any-permutation/
4+
5+
## 题目描述
6+
7+
```
8+
有一个整数数组 nums ,和一个查询数组 requests ,其中 requests[i] = [starti, endi] 。第 i 个查询求 nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi] 的结果 ,starti 和 endi 数组索引都是 从 0 开始 的。
9+
10+
你可以任意排列 nums 中的数字,请你返回所有查询结果之和的最大值。
11+
12+
由于答案可能会很大,请你将它对 109 + 7 取余 后返回。
13+
14+
15+
16+
示例 1:
17+
18+
输入:nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
19+
输出:19
20+
解释:一个可行的 nums 排列为 [2,1,3,4,5],并有如下结果:
21+
requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
22+
requests[1] -> nums[0] + nums[1] = 2 + 1 = 3
23+
总和为:8 + 3 = 11。
24+
一个总和更大的排列为 [3,5,4,2,1],并有如下结果:
25+
requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
26+
requests[1] -> nums[0] + nums[1] = 3 + 5 = 8
27+
总和为: 11 + 8 = 19,这个方案是所有排列中查询之和最大的结果。
28+
29+
30+
示例 2:
31+
32+
输入:nums = [1,2,3,4,5,6], requests = [[0,1]]
33+
输出:11
34+
解释:一个总和最大的排列为 [6,5,4,3,2,1] ,查询和为 [11]。
35+
36+
示例 3:
37+
38+
输入:nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
39+
输出:47
40+
解释:一个和最大的排列为 [4,10,5,3,2,1] ,查询结果分别为 [19,18,10]。
41+
42+
43+
44+
提示:
45+
46+
n == nums.length
47+
1 <= n <= 105
48+
0 <= nums[i] <= 105
49+
1 <= requests.length <= 10^5
50+
requests[i].length == 2
51+
0 <= starti <= endi < n
52+
```
53+
54+
## 前置知识
55+
56+
- 差分&前缀和
57+
- 贪心
58+
59+
## 公司
60+
61+
- 暂无
62+
63+
## 思路
64+
65+
我们直接将 request 离散化,统计每一个索引没查询的次数。接下来,我们贪心地令 nums 中最大的数的替换到**查询索引最频繁的**,这样才可以使得结果更优。第二大的配对到查询第二频繁的,以此类推。
66+
67+
代码:
68+
69+
```py
70+
class Solution:
71+
def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
72+
counter = collections.Counter()
73+
n = len(nums)
74+
for s, e in requests:
75+
for i in range(s, e+1):
76+
counter[i] += 1
77+
ans = i = 0
78+
nums.sort(reverse=True)
79+
for v in sorted(counter.values(), reverse=True):
80+
ans += v * nums[i]
81+
ans %= 10 ** 9 + 7
82+
i += 1
83+
return ans
84+
```
85+
86+
计算 counter 的时间复杂度是 $O(n*v),ドル其中 n 为 requests 长度,v 为 request[i][1] - request[i][0] 的平均值。也就是说时间复杂度等价于 sum(request[i][1] - request[i][0]),其中 0 <= i < n。
87+
88+
我们可以使用差分技巧,接下来使用前缀和来计算 counter。这可以使计算 counter 的复杂度降低到 $O(n)$。
89+
90+
> 一道飞机乘客的问题也用到了这个技巧,我在前缀和专题中也讲了这道题。
91+
92+
## 关键点
93+
94+
- 差分
95+
96+
## 代码
97+
98+
- 语言支持:Python3
99+
100+
Python3 Code:
101+
102+
```python
103+
104+
class Solution:
105+
def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
106+
counter = collections.Counter()
107+
n = len(nums)
108+
for s, e in requests:
109+
counter[s] += 1
110+
if e + 1 < n:
111+
counter[e + 1] -= 1
112+
for i in range(1, n):
113+
counter[i] += counter[i - 1]
114+
ans = i = 0
115+
nums.sort(reverse=True)
116+
for v in sorted(counter.values(), reverse=True):
117+
ans += v * nums[i]
118+
ans %= 10 ** 9 + 7
119+
i += 1
120+
return ans
121+
122+
```
123+
124+
**复杂度分析**
125+
126+
令 n 为 nums 长度。
127+
128+
- 时间复杂度:$O(nlogn)$
129+
- 空间复杂度:$O(n)$
130+
131+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
132+
133+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
134+
135+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
136+
137+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
138+
139+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

‎problems/385.mini-parser.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
## 题目地址(385. 迷你语法分析器)
2+
3+
https://leetcode-cn.com/problems/385./
4+
5+
## 题目描述
6+
7+
```
8+
给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。
9+
10+
列表中的每个元素只可能是整数或整数嵌套列表
11+
12+
提示:你可以假定这些字符串都是格式良好的:
13+
14+
字符串非空
15+
字符串不包含空格
16+
字符串只包含数字0-9、[、-、,、]
17+
18+
19+
20+
示例 1:
21+
22+
给定 s = "324",
23+
24+
你应该返回一个 NestedInteger 对象,其中只包含整数值 324。
25+
26+
27+
示例 2:
28+
29+
给定 s = "[123,[456,[789]]]",
30+
31+
返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
32+
33+
1. 一个 integer 包含值 123
34+
2. 一个包含两个元素的嵌套列表:
35+
i. 一个 integer 包含值 456
36+
ii. 一个包含一个元素的嵌套列表
37+
a. 一个 integer 包含值 789
38+
39+
```
40+
41+
## 前置知识
42+
43+
-
44+
- 递归
45+
46+
## 公司
47+
48+
- 暂无
49+
50+
## 思路
51+
52+
我们可以直接使用 eval 来将字符串转化为数组。
53+
54+
```py
55+
class Solution:
56+
def deserialize(self, s: str) -> NestedInteger:
57+
def dfs(cur):
58+
if type(cur) == int:
59+
return NestedInteger(cur)
60+
ans = NestedInteger()
61+
for nxt in cur:
62+
ans.add(dfs(nxt))
63+
return ans
64+
65+
return dfs(eval(s))
66+
```
67+
68+
接下来,我们考虑如何实现 eval。
69+
70+
这其实是一个简单的栈 + dfs 题目。力扣中有**非常多的题目都使用了这个题目**,比如一些编码解码的题目,eg:[394. 字符串解码](https://github.com/azl397985856/leetcode/blob/master/problems/394.decode-string.md "394. 字符串解码")
71+
72+
## 关键点
73+
74+
- 栈+递归。遇到 [ 开启新的递归,遇到 ] 返回
75+
76+
## 代码
77+
78+
- 语言支持:Python3
79+
80+
Python3 Code:
81+
82+
```python
83+
84+
# """
85+
# This is the interface that allows for creating nested lists.
86+
# You should not implement it, or speculate about its implementation
87+
# """
88+
#class NestedInteger:
89+
# def __init__(self, value=None):
90+
# """
91+
# If value is not specified, initializes an empty list.
92+
# Otherwise initializes a single integer equal to value.
93+
# """
94+
#
95+
# def isInteger(self):
96+
# """
97+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
98+
# :rtype bool
99+
# """
100+
#
101+
# def add(self, elem):
102+
# """
103+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
104+
# :rtype void
105+
# """
106+
#
107+
# def setInteger(self, value):
108+
# """
109+
# Set this NestedInteger to hold a single integer equal to value.
110+
# :rtype void
111+
# """
112+
#
113+
# def getInteger(self):
114+
# """
115+
# @return the single integer that this NestedInteger holds, if it holds a single integer
116+
# Return None if this NestedInteger holds a nested list
117+
# :rtype int
118+
# """
119+
#
120+
# def getList(self):
121+
# """
122+
# @return the nested list that this NestedInteger holds, if it holds a nested list
123+
# Return None if this NestedInteger holds a single integer
124+
# :rtype List[NestedInteger]
125+
# """
126+
class Solution:
127+
def deserialize(self, s: str) -> NestedInteger:
128+
def dfs(cur):
129+
if type(cur) == int:
130+
return NestedInteger(cur)
131+
ans = NestedInteger()
132+
for nxt in cur:
133+
ans.add(dfs(nxt))
134+
return ans
135+
def to_array(i):
136+
stack = []
137+
num = ''
138+
while i < len(s):
139+
if s[i] == ' ':
140+
i += 1
141+
continue
142+
elif s[i] == ',':
143+
if num:
144+
stack.append(int(num or '0'))
145+
num = ''
146+
elif s[i] == '[':
147+
j, t = to_array(i+1)
148+
stack.append(t)
149+
i = j
150+
elif s[i] == ']':
151+
break
152+
else:
153+
num += s[i]
154+
i += 1
155+
if num:
156+
stack.append(int(num))
157+
return i, stack
158+
return dfs(to_array(0)[1][0])
159+
160+
```
161+
162+
**复杂度分析**
163+
164+
令 n 为 s 长度。
165+
166+
- 时间复杂度:$O(n)$
167+
- 空间复杂度:$O(n)$
168+
169+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
170+
171+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
172+
173+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
174+
175+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
176+
177+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /