Skip to content

Commit 76d57ef

Browse files
committed
feat: add solutions to lc problems: No.2656~2663
* No.2656.Maximum Sum With Exactly K Elements * No.2657.Find the Prefix Common Array of Two Arrays * No.2658.Maximum Number of Fish in a Grid * No.2659.Make Array Empty * No.2660.Determine the Winner of a Bowling Game * No.2661.First Completely Painted Row or Column * No.2662.Minimum Cost of a Path With Special Roads * No.2663.Lexicographically Smallest Beautiful String
1 parent f8c17f3 commit 76d57ef

File tree

71 files changed

+4877
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4877
-7
lines changed

solution/0400-0499/0403.Frog Jump/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def dfs(i, k):
1111

1212
n = len(stones)
1313
pos = {s: i for i, s in enumerate(stones)}
14-
return dfs(0, 0)
14+
return dfs(0, 0)

solution/1300-1399/1372.Longest ZigZag Path in a Binary Tree/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<p><strong class="example">Example 1:</strong></p>
2424
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1372.Longest%20ZigZag%20Path%20in%20a%20Binary%20Tree/images/sample_1_1702.png" style="width: 221px; height: 383px;" />
2525
<pre>
26-
<strong>Input:</strong> root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
26+
<strong>Input:</strong> root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1]
2727
<strong>Output:</strong> 3
2828
<strong>Explanation:</strong> Longest ZigZag path in blue nodes (right -&gt; left -&gt; right).
2929
</pre>

solution/2600-2699/2612.Minimum Reverse Operations/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ class TreeMultiSet<T = number> {
939939
}
940940
```
941941

942-
```ts
942+
````ts
943943
function minReverseOperations(
944944
n: number,
945945
p: number,
@@ -1667,7 +1667,7 @@ class TreapMultiSet<T = number> implements ITreapMultiSet<T> {
16671667
yield* this.reverseInOrder(root.left);
16681668
}
16691669
}
1670-
```
1670+
````
16711671

16721672
### **...**
16731673

solution/2600-2699/2612.Minimum Reverse Operations/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ class TreeMultiSet<T = number> {
925925
}
926926
```
927927

928-
```ts
928+
````ts
929929
function minReverseOperations(
930930
n: number,
931931
p: number,
@@ -1653,7 +1653,7 @@ class TreapMultiSet<T = number> implements ITreapMultiSet<T> {
16531653
yield* this.reverseInOrder(root.left);
16541654
}
16551655
}
1656-
```
1656+
````
16571657

16581658
### **...**
16591659

solution/2600-2699/2655.Find Maximal Uncovered Ranges/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def findMaximalUncoveredRanges(self, n: int, ranges: List[List[int]]) -> List[List[int]]:
2+
def findMaximalUncoveredRanges(
3+
self, n: int, ranges: List[List[int]]
4+
) -> List[List[int]]:
35
ranges.sort()
46
last = -1
57
ans = []
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# [2656. K 个元素的最大和](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements)
2+
3+
[English Version](/solution/2600-2699/2656.Maximum%20Sum%20With%20Exactly%20K%20Elements/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code> 和一个整数&nbsp;<code>k</code>&nbsp;。你需要执行以下操作<strong>&nbsp;恰好</strong> <code>k</code>&nbsp;次,最大化你的得分:</p>
10+
11+
<ol>
12+
<li>从 <code>nums</code>&nbsp;中选择一个元素&nbsp;<code>m</code>&nbsp;。</li>
13+
<li>将选中的元素&nbsp;<code>m</code>&nbsp;从数组中删除。</li>
14+
<li>将新元素&nbsp;<code>m + 1</code>&nbsp;添加到数组中。</li>
15+
<li>你的得分增加&nbsp;<code>m</code>&nbsp;。</li>
16+
</ol>
17+
18+
<p>请你返回执行以上操作恰好 <code>k</code>&nbsp;次后的最大得分。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre>
25+
<b>输入:</b>nums = [1,2,3,4,5], k = 3
26+
<b>输出:</b>18
27+
<b>解释:</b>我们需要从 nums 中恰好选择 3 个元素并最大化得分。
28+
第一次选择 5 。和为 5 ,nums = [1,2,3,4,6] 。
29+
第二次选择 6 。和为 6 ,nums = [1,2,3,4,7] 。
30+
第三次选择 7 。和为 5 + 6 + 7 = 18 ,nums = [1,2,3,4,8] 。
31+
所以我们返回 18 。
32+
18 是可以得到的最大答案。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<b>输入:</b>nums = [5,5,5], k = 2
39+
<b>输出:</b>11
40+
<b>解释:</b>我们需要从 nums 中恰好选择 2 个元素并最大化得分。
41+
第一次选择 5 。和为 5 ,nums = [5,5,6] 。
42+
第二次选择 6 。和为 6 ,nums = [5,5,7] 。
43+
所以我们返回 11 。
44+
11 是可以得到的最大答案。
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
53+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
54+
<li><code>1 &lt;= k &lt;= 100</code></li>
55+
</ul>
56+
57+
## 解法
58+
59+
<!-- 这里可写通用的实现逻辑 -->
60+
61+
**方法一:贪心 + 数学**
62+
63+
我们注意到,要使得最终的得分最大,我们应该尽可能地使得每次选择的元素最大。因此,我们第一次选择数组中的最大元素 $x$,第二次选择 $x+1$,第三次选择 $x+2$,以此类推,直到第 $k$ 次选择 $x+k-1$。这样的选择方式可以保证每次选择的元素都是当前数组中的最大值,因此最终的得分也是最大的。答案即为 $k$ 个 $x$ 的和加上 $0+1+2+\cdots+(k-1)$,即 $k \times x + (k - 1) \times k / 2$。
64+
65+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
66+
67+
<!-- tabs:start -->
68+
69+
### **Python3**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```python
74+
class Solution:
75+
def maximizeSum(self, nums: List[int], k: int) -> int:
76+
x = max(nums)
77+
return k * x + k * (k - 1) // 2
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
class Solution {
86+
public int maximizeSum(int[] nums, int k) {
87+
int x = 0;
88+
for (int v : nums) {
89+
x = Math.max(x, v);
90+
}
91+
return k * x + k * (k - 1) / 2;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int maximizeSum(vector<int>& nums, int k) {
102+
int x = *max_element(nums.begin(), nums.end());
103+
return k * x + k * (k - 1) / 2;
104+
}
105+
};
106+
```
107+
108+
### **Go**
109+
110+
```go
111+
func maximizeSum(nums []int, k int) int {
112+
x := 0
113+
for _, v := range nums {
114+
x = max(x, v)
115+
}
116+
return k*x + k*(k-1)/2
117+
}
118+
119+
func max(a, b int) int {
120+
if a > b {
121+
return a
122+
}
123+
return b
124+
}
125+
```
126+
127+
### **TypeScript**
128+
129+
```ts
130+
function maximizeSum(nums: number[], k: number): number {
131+
const x = Math.max(...nums);
132+
return k * x + (k * (k - 1)) / 2;
133+
}
134+
```
135+
136+
### **...**
137+
138+
```
139+
140+
```
141+
142+
<!-- tabs:end -->
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# [2656. Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements)
2+
3+
[中文文档](/solution/2600-2699/2656.Maximum%20Sum%20With%20Exactly%20K%20Elements/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>. Your task is to perform the following operation <strong>exactly</strong> <code>k</code> times in order to maximize your score:</p>
8+
9+
<ol>
10+
<li>Select an element <code>m</code> from <code>nums</code>.</li>
11+
<li>Remove the selected element <code>m</code> from the array.</li>
12+
<li>Add a new element with a value of <code>m + 1</code> to the array.</li>
13+
<li>Increase your score by <code>m</code>.</li>
14+
</ol>
15+
16+
<p>Return <em>the maximum score you can achieve after performing the operation exactly</em> <code>k</code> <em>times.</em></p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [1,2,3,4,5], k = 3
23+
<strong>Output:</strong> 18
24+
<strong>Explanation:</strong> We need to choose exactly 3 elements from nums to maximize the sum.
25+
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
26+
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
27+
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
28+
So, we will return 18.
29+
It can be proven, that 18 is the maximum answer that we can achieve.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [5,5,5], k = 2
36+
<strong>Output:</strong> 11
37+
<strong>Explanation:</strong> We need to choose exactly 2 elements from nums to maximize the sum.
38+
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
39+
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
40+
So, we will return 11.
41+
It can be proven, that 11 is the maximum answer that we can achieve.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
49+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
50+
<li><code>1 &lt;= k &lt;= 100</code></li>
51+
</ul>
52+
53+
<p>&nbsp;</p>
54+
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
55+
}
56+
.spoiler {overflow:hidden;}
57+
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
58+
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
59+
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
60+
</style>
61+
62+
## Solutions
63+
64+
**Solution 1: Greedy + Mathematics**
65+
66+
We notice that to make the final score maximum, we should make each choice as large as possible. Therefore, we select the largest element $x$ in the array for the first time, $x+1$ for the second time, $x+2$ for the third time, and so on, until the $k$th time we select $x+k-1$. This way of selection ensures that the element selected each time is the largest in the current array, so the final score is also the largest. The answer is $k$ $x$ sum plus $0+1+2+\cdots+(k-1)$, that is, $k \times x + (k - 1) \times k / 2$.
67+
68+
Time complexity is $O(n)$, where $n$ is the length of the array. Space complexity is $O(1)$.
69+
70+
<!-- tabs:start -->
71+
72+
### **Python3**
73+
74+
```python
75+
class Solution:
76+
def maximizeSum(self, nums: List[int], k: int) -> int:
77+
x = max(nums)
78+
return k * x + k * (k - 1) // 2
79+
```
80+
81+
### **Java**
82+
83+
```java
84+
class Solution {
85+
public int maximizeSum(int[] nums, int k) {
86+
int x = 0;
87+
for (int v : nums) {
88+
x = Math.max(x, v);
89+
}
90+
return k * x + k * (k - 1) / 2;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int maximizeSum(vector<int>& nums, int k) {
101+
int x = *max_element(nums.begin(), nums.end());
102+
return k * x + k * (k - 1) / 2;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func maximizeSum(nums []int, k int) int {
111+
x := 0
112+
for _, v := range nums {
113+
x = max(x, v)
114+
}
115+
return k*x + k*(k-1)/2
116+
}
117+
118+
func max(a, b int) int {
119+
if a > b {
120+
return a
121+
}
122+
return b
123+
}
124+
```
125+
126+
### **TypeScript**
127+
128+
```ts
129+
function maximizeSum(nums: number[], k: number): number {
130+
const x = Math.max(...nums);
131+
return k * x + (k * (k - 1)) / 2;
132+
}
133+
```
134+
135+
### **...**
136+
137+
```
138+
139+
```
140+
141+
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
int maximizeSum(vector<int>& nums, int k) {
4+
int x = *max_element(nums.begin(), nums.end());
5+
return k * x + k * (k - 1) / 2;
6+
}
7+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func maximizeSum(nums []int, k int) int {
2+
x := 0
3+
for _, v := range nums {
4+
x = max(x, v)
5+
}
6+
return k*x + k*(k-1)/2
7+
}
8+
9+
func max(a, b int) int {
10+
if a > b {
11+
return a
12+
}
13+
return b
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int maximizeSum(int[] nums, int k) {
3+
int x = 0;
4+
for (int v : nums) {
5+
x = Math.max(x, v);
6+
}
7+
return k * x + k * (k - 1) / 2;
8+
}
9+
}

0 commit comments

Comments
 (0)