Skip to content

Commit fd47409

Browse files
authored
feat: update solutions to lc problems: No.1589,1705 (doocs#3882)
* No.1589.Maximum Sum Obtained of Any Permutation * No.1705.Maximum Number of Eaten Apples
1 parent 1316a46 commit fd47409

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ requests[1] -> nums[0] + nums[1] = 3 + 5 = 8
7878

7979
我们观察发现,对于一次查询操作,会返回该查询区间 $[l, r]$ 中的所有元素之和。而题目要求的是所有查询操作的结果之和的最大值,也即是说,我们要累计所有查询操作的结果,使得这些结果之和最大。因此,如果一个下标 $i$ 在查询操作中出现的次数越多,那么我们就应该赋给下标 $i$ 一个较大的值,这样才能使得所有查询操作的结果之和最大。
8080

81-
因此,我们可以用差分数组的思想,统计每个下标在查询操作中出现的次数,然后对这些次数从小到大进行排序,然后对数组 $nums$ 也从小到大进行排序,这样就能保证每个下标 $i$ 在查询操作中出现的次数越多,该下标对应的值 $nums[i]$ 就越大。接下来,我们只需要将这些下标对应的值 $nums[i]$ 与其在查询操作中出现的次数相乘,然后累加起来,就是所有查询操作的结果之和的最大值。
81+
因此,我们可以用差分数组的思想,统计每个下标在查询操作中出现的次数,然后对这些次数从小到大进行排序,然后对数组 $\textit{nums}$ 也从小到大进行排序,这样就能保证每个下标 $i$ 在查询操作中出现的次数越多,该下标对应的值 $\textit{nums}[i]$ 就越大。接下来,我们只需要将这些下标对应的值 $\textit{nums}[i]$ 与其在查询操作中出现的次数相乘,然后累加起来,就是所有查询操作的结果之和的最大值。
8282

83-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
83+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8484

8585
<!-- tabs:start -->
8686

solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/README_EN.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<pre>
3434
<strong>Input:</strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
3535
<strong>Output:</strong> 19
36-
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:
36+
<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:
3737
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
3838
requests[1] -&gt; nums[0] + nums[1] = 2 + 1 = 3
3939
Total sum: 8 + 3 = 11.
@@ -75,7 +75,13 @@ Total sum: 11 + 8 = 19, which is the best that you can do.
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Difference Array + Sorting + Greedy
79+
80+
We observe that for a query operation, it returns the sum of all elements in the query interval $[l, r]$. The problem requires the maximum sum of the results of all query operations, which means we need to accumulate the results of all query operations to maximize the sum. Therefore, if an index $i$ appears more frequently in the query operations, we should assign a larger value to index $i$ to maximize the sum of the results of all query operations.
81+
82+
Therefore, we can use the idea of a difference array to count the number of times each index appears in the query operations, then sort these counts in ascending order, and also sort the array $\textit{nums}$ in ascending order. This ensures that the more frequently an index $i$ appears in the query operations, the larger the value $\textit{nums}[i]$ corresponding to that index will be. Next, we only need to multiply the values $\textit{nums}[i]$ corresponding to these indices by the number of times they appear in the query operations, and then sum them up to get the maximum sum of the results of all query operations.
83+
84+
Time complexity $O(n \times \log n)$, space complexity $O(n)$. Where $n$ is the length of the array $\textit{nums}$.
7985

8086
<!-- tabs:start -->
8187

solution/1700-1799/1705.Maximum Number of Eaten Apples/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ tags:
7373

7474
因此,我们可以用优先队列(小根堆)存储苹果的腐烂时间以及对应苹果的数量,每次从优先队列中取出腐烂时间最小的苹果,然后将其数量减一,若减一后苹果的数量不为零,则将其重新放入优先队列中。若苹果已经腐烂,则从优先队列中弹出。
7575

76-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `apples``days` 的长度。
76+
时间复杂度 $O(n \times \log n + M)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{days}$ 的长度,而 $M = \max(\textit{days})$
7777

7878
<!-- tabs:start -->
7979

@@ -132,23 +132,28 @@ class Solution {
132132
#### C++
133133

134134
```cpp
135-
using pii = pair<int, int>;
136-
137135
class Solution {
138136
public:
139137
int eatenApples(vector<int>& apples, vector<int>& days) {
138+
using pii = pair<int, int>;
140139
priority_queue<pii, vector<pii>, greater<pii>> q;
141140
int n = days.size();
142141
int ans = 0, i = 0;
143142
while (i < n || !q.empty()) {
144-
if (i < n && apples[i]) q.emplace(i + days[i] - 1, apples[i]);
145-
while (!q.empty() && q.top().first < i) q.pop();
143+
if (i < n && apples[i]) {
144+
q.emplace(i + days[i] - 1, apples[i]);
145+
}
146+
while (!q.empty() && q.top().first < i) {
147+
q.pop();
148+
}
146149
if (!q.empty()) {
147150
auto [t, v] = q.top();
148151
q.pop();
149152
--v;
150153
++ans;
151-
if (v && t > i) q.emplace(t, v);
154+
if (v && t > i) {
155+
q.emplace(t, v);
156+
}
152157
}
153158
++i;
154159
}

solution/1700-1799/1705.Maximum Number of Eaten Apples/README_EN.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ tags:
6868

6969
### Solution 1: Greedy + Priority Queue
7070

71-
We can greedily choose the apple that is most likely to rot among the unrotten apples, so that we can eat as many apples as possible.
71+
We can greedily choose the apples that are closest to rotting among the unrotten apples, so that we can eat as many apples as possible.
7272

73-
Therefore, we can use a priority queue (min heap) to store the rotting time of the apples and the corresponding number of apples. Each time we take out the apple with the smallest rotting time from the priority queue, then reduce its quantity by one. If the quantity of the apple is not zero after reduction, we put it back into the priority queue. If the apple has rotted, we pop it out from the priority queue.
73+
Therefore, we can use a priority queue (min-heap) to store the rotting time of the apples and the corresponding number of apples. Each time, we take out the apples with the smallest rotting time from the priority queue, then decrement their quantity by one. If the quantity is not zero after decrementing, we put them back into the priority queue. If the apples have already rotted, we remove them from the priority queue.
7474

75-
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `apples` or `days`.
75+
The time complexity is $O(n \times \log n + M)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{days}$, and $M = \max(\textit{days})$.
7676

7777
<!-- tabs:start -->
7878

@@ -131,23 +131,28 @@ class Solution {
131131
#### C++
132132

133133
```cpp
134-
using pii = pair<int, int>;
135-
136134
class Solution {
137135
public:
138136
int eatenApples(vector<int>& apples, vector<int>& days) {
137+
using pii = pair<int, int>;
139138
priority_queue<pii, vector<pii>, greater<pii>> q;
140139
int n = days.size();
141140
int ans = 0, i = 0;
142141
while (i < n || !q.empty()) {
143-
if (i < n && apples[i]) q.emplace(i + days[i] - 1, apples[i]);
144-
while (!q.empty() && q.top().first < i) q.pop();
142+
if (i < n && apples[i]) {
143+
q.emplace(i + days[i] - 1, apples[i]);
144+
}
145+
while (!q.empty() && q.top().first < i) {
146+
q.pop();
147+
}
145148
if (!q.empty()) {
146149
auto [t, v] = q.top();
147150
q.pop();
148151
--v;
149152
++ans;
150-
if (v && t > i) q.emplace(t, v);
153+
if (v && t > i) {
154+
q.emplace(t, v);
155+
}
151156
}
152157
++i;
153158
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
using pii = pair<int, int>;
2-
31
class Solution {
42
public:
53
int eatenApples(vector<int>& apples, vector<int>& days) {
4+
using pii = pair<int, int>;
65
priority_queue<pii, vector<pii>, greater<pii>> q;
76
int n = days.size();
87
int ans = 0, i = 0;
98
while (i < n || !q.empty()) {
10-
if (i < n && apples[i]) q.emplace(i + days[i] - 1, apples[i]);
11-
while (!q.empty() && q.top().first < i) q.pop();
9+
if (i < n && apples[i]) {
10+
q.emplace(i + days[i] - 1, apples[i]);
11+
}
12+
while (!q.empty() && q.top().first < i) {
13+
q.pop();
14+
}
1215
if (!q.empty()) {
1316
auto [t, v] = q.top();
1417
q.pop();
1518
--v;
1619
++ans;
17-
if (v && t > i) q.emplace(t, v);
20+
if (v && t > i) {
21+
q.emplace(t, v);
22+
}
1823
}
1924
++i;
2025
}
2126
return ans;
2227
}
23-
};
28+
};

0 commit comments

Comments
 (0)