Skip to content

Commit 8de13d1

Browse files
committed
feat: add biweekly contest 169
1 parent 34bfb23 commit 8de13d1

File tree

43 files changed

+2446
-5
lines changed

Some content is hidden

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

43 files changed

+2446
-5
lines changed

solution/2500-2599/2528.Maximize the Minimum Powered City/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ tags:
4545
<pre>
4646
<strong>Input:</strong> stations = [1,2,4,5,0], r = 1, k = 2
4747
<strong>Output:</strong> 5
48-
<strong>Explanation:</strong>
49-
One of the optimal ways is to install both the power stations at city 1.
48+
<strong>Explanation:</strong>
49+
One of the optimal ways is to install both the power stations at city 1.
5050
So stations will become [1,4,4,5,0].
5151
- City 0 is provided by 1 + 4 = 5 power stations.
5252
- City 1 is provided by 1 + 4 + 4 = 9 power stations.
@@ -62,7 +62,7 @@ Since it is not possible to obtain a larger power, we return 5.
6262
<pre>
6363
<strong>Input:</strong> stations = [4,4,4,4], r = 0, k = 3
6464
<strong>Output:</strong> 4
65-
<strong>Explanation:</strong>
65+
<strong>Explanation:</strong>
6666
It can be proved that we cannot make the minimum power of a city greater than 4.
6767
</pre>
6868

solution/3700-3799/3731.Find Missing Elements/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 简单
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3731.Find%20Missing%20Elements/README.md
5+
rating: 1217
6+
source: 第 474 场周赛 Q1
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3731.Find Missing Elements/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Easy
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3731.Find%20Missing%20Elements/README_EN.md
5+
rating: 1217
6+
source: Weekly Contest 474 Q1
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3732.Maximum Product of Three Elements After One Replacement/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3732.Maximum%20Product%20of%20Three%20Elements%20After%20One%20Replacement/README.md
5+
rating: 1529
6+
source: 第 474 场周赛 Q2
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3732.Maximum Product of Three Elements After One Replacement/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3732.Maximum%20Product%20of%20Three%20Elements%20After%20One%20Replacement/README_EN.md
5+
rating: 1529
6+
source: Weekly Contest 474 Q2
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3733.Minimum Time to Complete All Deliveries/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 中等
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3733.Minimum%20Time%20to%20Complete%20All%20Deliveries/README.md
5+
rating: 1972
6+
source: 第 474 场周赛 Q3
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3733.Minimum Time to Complete All Deliveries/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Medium
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3733.Minimum%20Time%20to%20Complete%20All%20Deliveries/README_EN.md
5+
rating: 1972
6+
source: Weekly Contest 474 Q3
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3734.Lexicographically Smallest Palindromic Permutation Greater Than Target/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3734.Lexicographically%20Smallest%20Palindromic%20Permutation%20Greater%20Than%20Target/README.md
5+
rating: 2330
6+
source: 第 474 场周赛 Q4
57
---
68

79
<!-- problem:start -->

solution/3700-3799/3734.Lexicographically Smallest Palindromic Permutation Greater Than Target/README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: Hard
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3734.Lexicographically%20Smallest%20Palindromic%20Permutation%20Greater%20Than%20Target/README_EN.md
5+
rating: 2330
6+
source: Weekly Contest 474 Q4
57
---
68

79
<!-- problem:start -->
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3736.Minimum%20Moves%20to%20Equal%20Array%20Elements%20III/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3736. 最小操作次数使数组元素相等 III](https://leetcode.cn/problems/minimum-moves-to-equal-array-elements-iii)
10+
11+
[English Version](/solution/3700-3799/3736.Minimum%20Moves%20to%20Equal%20Array%20Elements%20III/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code>。</p>
18+
19+
<p>在一步操作中,你可以将任意单个元素 <code>nums[i]</code> 的值&nbsp;<strong>增加</strong> 1。</p>
20+
21+
<p>返回使数组中的所有元素都&nbsp;<strong>相等&nbsp;</strong>所需的&nbsp;<strong>最小总操作次数&nbsp;</strong>。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">nums = [2,1,3]</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>使所有元素相等的操作如下:</p>
35+
36+
<ul>
37+
<li>将 <code>nums[0] = 2</code> 增加 1, 变为 3。</li>
38+
<li>将 <code>nums[1] = 1</code> 增加 1, 变为 2。</li>
39+
<li>将 <code>nums[1] = 2</code> 增加 1, 变为 3。</li>
40+
</ul>
41+
42+
<p>现在,<code>nums</code> 中的所有元素都等于 3。最小总操作次数为 <code>3</code>。</p>
43+
</div>
44+
45+
<p><strong class="example">示例 2:</strong></p>
46+
47+
<div class="example-block">
48+
<p><strong>输入:</strong> <span class="example-io">nums = [4,4,5]</span></p>
49+
50+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
51+
52+
<p><strong>解释:</strong></p>
53+
54+
<p>使所有元素相等的操作如下:</p>
55+
56+
<ul>
57+
<li>将 <code>nums[0] = 4</code> 增加 1, 变为 5。</li>
58+
<li>将 <code>nums[1] = 4</code> 增加 1, 变为 5。</li>
59+
</ul>
60+
61+
<p>现在,<code>nums</code> 中的所有元素都等于 5。最小总操作次数为 <code>2</code>。</p>
62+
</div>
63+
64+
<p>&nbsp;</p>
65+
66+
<p><strong>提示:</strong></p>
67+
68+
<ul>
69+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
70+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
71+
</ul>
72+
73+
<!-- description:end -->
74+
75+
## 解法
76+
77+
<!-- solution:start -->
78+
79+
### 方法一: 计算总和与最大值
80+
81+
本题要求将数组中的所有元素变为相等,且每次只能将某个元素增加 1。为了使操作次数最少,我们应当将所有元素都变为数组中的最大值。
82+
83+
因此,我们可以先计算数组的最大值 $\textit{mx}$ 和数组元素的总和 $\textit{s}$,那么将所有元素变为 $\textit{mx}$ 所需的操作次数即为 $\textit{mx} \times n - \textit{s}$,其中 $n$ 是数组的长度。
84+
85+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
86+
87+
<!-- tabs:start -->
88+
89+
#### Python3
90+
91+
```python
92+
class Solution:
93+
def minMoves(self, nums: List[int]) -> int:
94+
n = len(nums)
95+
mx = max(nums)
96+
s = sum(nums)
97+
return mx * n - s
98+
```
99+
100+
#### Java
101+
102+
```java
103+
class Solution {
104+
public int minMoves(int[] nums) {
105+
int n = nums.length;
106+
int mx = 0;
107+
int s = 0;
108+
for (int x : nums) {
109+
mx = Math.max(mx, x);
110+
s += x;
111+
}
112+
return mx * n - s;
113+
}
114+
}
115+
```
116+
117+
#### C++
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int minMoves(vector<int>& nums) {
123+
int n = nums.size();
124+
int mx = 0;
125+
int s = 0;
126+
for (int x : nums) {
127+
mx = max(mx, x);
128+
s += x;
129+
}
130+
return mx * n - s;
131+
}
132+
};
133+
```
134+
135+
#### Go
136+
137+
```go
138+
func minMoves(nums []int) int {
139+
mx, s := 0, 0
140+
for _, x := range nums {
141+
mx = max(mx, x)
142+
s += x
143+
}
144+
return mx*len(nums) - s
145+
}
146+
```
147+
148+
#### TypeScript
149+
150+
```ts
151+
function minMoves(nums: number[]): number {
152+
const n = nums.length;
153+
const mx = Math.max(...nums);
154+
const s = nums.reduce((a, b) => a + b, 0);
155+
return mx * n - s;
156+
}
157+
```
158+
159+
<!-- tabs:end -->
160+
161+
<!-- solution:end -->
162+
163+
<!-- problem:end -->

0 commit comments

Comments
 (0)