Skip to content

Commit 8a70139

Browse files
authored
feat: update solutions to lc problems: No.451,453 (#3825)
1 parent ffd082d commit 8a70139

File tree

10 files changed

+80
-111
lines changed

10 files changed

+80
-111
lines changed

solution/0400-0499/0451.Sort Characters By Frequency/README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ tags:
7171

7272
### 方法一:哈希表 + 排序
7373

74-
我们用哈希表 $cnt$ 统计字符串 $s$ 中每个字符出现的次数,然后将 $cnt$ 中的键值对按照出现次数降序排序,最后按照排序后的顺序拼接字符串即可。
74+
我们用哈希表 $\textit{cnt}$ 统计字符串 $s$ 中每个字符出现的次数,然后将 $\textit{cnt}$ 中的键值对按照出现次数降序排序,最后按照排序后的顺序拼接字符串即可。
7575

7676
时间复杂度 $O(n + k \times \log k)$,空间复杂度 $O(n + k)$,其中 $n$ 为字符串 $s$ 的长度,而 $k$ 为不同字符的个数。
7777

@@ -200,15 +200,16 @@ class Solution {
200200
* @return String
201201
*/
202202
function frequencySort($s) {
203-
for ($i = 0; $i < strlen($s); $i++) {
204-
$hashtable[$s[$i]] += 1;
205-
}
206-
arsort($hashtable);
207-
$keys = array_keys($hashtable);
208-
for ($j = 0; $j < count($keys); $j++) {
209-
$rs = $rs . str_repeat($keys[$j], $hashtable[$keys[$j]]);
203+
$cnt = array_count_values(str_split($s));
204+
$cs = array_keys($cnt);
205+
usort($cs, function ($a, $b) use ($cnt) {
206+
return $cnt[$b] <=> $cnt[$a];
207+
});
208+
$ans = '';
209+
foreach ($cs as $c) {
210+
$ans .= str_repeat($c, $cnt[$c]);
210211
}
211-
return $rs;
212+
return $ans;
212213
}
213214
}
214215
```

solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ Note that &#39;A&#39; and &#39;a&#39; are treated as two different characters.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Hash Table + Sorting
71+
72+
We use a hash table $\textit{cnt}$ to count the occurrences of each character in the string $s$. Then, we sort the key-value pairs in $\textit{cnt}$ in descending order by the number of occurrences. Finally, we concatenate the characters according to the sorted order.
73+
74+
The time complexity is $O(n + k \times \log k)$, and the space complexity is $O(n + k)$, where $n$ is the length of the string $s$, and $k$ is the number of distinct characters.
7175

7276
<!-- tabs:start -->
7377

@@ -194,15 +198,16 @@ class Solution {
194198
* @return String
195199
*/
196200
function frequencySort($s) {
197-
for ($i = 0; $i < strlen($s); $i++) {
198-
$hashtable[$s[$i]] += 1;
199-
}
200-
arsort($hashtable);
201-
$keys = array_keys($hashtable);
202-
for ($j = 0; $j < count($keys); $j++) {
203-
$rs = $rs . str_repeat($keys[$j], $hashtable[$keys[$j]]);
201+
$cnt = array_count_values(str_split($s));
202+
$cs = array_keys($cnt);
203+
usort($cs, function ($a, $b) use ($cnt) {
204+
return $cnt[$b] <=> $cnt[$a];
205+
});
206+
$ans = '';
207+
foreach ($cs as $c) {
208+
$ans .= str_repeat($c, $cnt[$c]);
204209
}
205-
return $rs;
210+
return $ans;
206211
}
207212
}
208213
```

solution/0400-0499/0451.Sort Characters By Frequency/Solution.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ class Solution {
44
* @return String
55
*/
66
function frequencySort($s) {
7-
for ($i = 0; $i < strlen($s); $i++) {
8-
$hashtable[$s[$i]] += 1;
7+
$cnt = array_count_values(str_split($s));
8+
$cs = array_keys($cnt);
9+
usort($cs, function ($a, $b) use ($cnt) {
10+
return $cnt[$b] <=> $cnt[$a];
11+
});
12+
$ans = '';
13+
foreach ($cs as $c) {
14+
$ans .= str_repeat($c, $cnt[$c]);
915
}
10-
arsort($hashtable);
11-
$keys = array_keys($hashtable);
12-
for ($j = 0; $j < count($keys); $j++) {
13-
$rs = $rs . str_repeat($keys[$j], $hashtable[$keys[$j]]);
14-
}
15-
return $rs;
16+
return $ans;
1617
}
1718
}

solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README.md

+8-34
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,28 @@ tags:
5757

5858
### 方法一:数学
5959

60-
我们不妨记数组 $nums$ 的最小值为 $mi$,数组的和为 $s$,数组的长度为 $n$。
60+
我们不妨记数组 $\textit{nums}$ 的最小值为 $\textit{mi}$,数组的和为 $\textit{s}$,数组的长度为 $\textit{n}$。
6161

62-
假设最小操作次数为 $k$,最终数组的所有元素都为 $x$,则有:
62+
假设最小操作次数为 $\textit{k}$,最终数组的所有元素都为 $\textit{x}$,则有:
6363

6464
$$
6565
\begin{aligned}
66-
s + (n - 1) \times k &= n \times x \\
67-
x &= mi + k \\
66+
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{x} \\
67+
\textit{x} &= \textit{mi} + \textit{k} \\
6868
\end{aligned}
6969
$$
7070

7171
将第二个式子代入第一个式子,得到:
7272

7373
$$
7474
\begin{aligned}
75-
s + (n - 1) \times k &= n \times (mi + k) \\
76-
s + (n - 1) \times k &= n \times mi + n \times k \\
77-
k &= s - n \times mi \\
75+
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times (\textit{mi} + \textit{k}) \\
76+
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{mi} + \textit{n} \times \textit{k} \\
77+
\textit{k} &= \textit{s} - \textit{n} \times \textit{mi} \\
7878
\end{aligned}
7979
$$
8080

81-
因此,最小操作次数为 $s - n \times mi$。
81+
因此,最小操作次数为 $\textit{s} - \textit{n} \times \textit{mi}$。
8282

8383
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
8484

@@ -153,30 +153,4 @@ function minMoves(nums: number[]): number {
153153

154154
<!-- solution:end -->
155155

156-
<!-- solution:start -->
157-
158-
### 方法二
159-
160-
<!-- tabs:start -->
161-
162-
#### Java
163-
164-
```java
165-
class Solution {
166-
public int minMoves(int[] nums) {
167-
int s = 0;
168-
int mi = 1 << 30;
169-
for (int x : nums) {
170-
s += x;
171-
mi = Math.min(mi, x);
172-
}
173-
return s - mi * nums.length;
174-
}
175-
}
176-
```
177-
178-
<!-- tabs:end -->
179-
180-
<!-- solution:end -->
181-
182156
<!-- problem:end -->

solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README_EN.md

+26-27
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,32 @@ tags:
5454

5555
<!-- solution:start -->
5656

57-
### Solution 1
57+
### Solution 1: Mathematics
58+
59+
Let the minimum value of the array $\textit{nums}$ be $\textit{mi}$, the sum of the array be $\textit{s}$, and the length of the array be $\textit{n}$.
60+
61+
Assume the minimum number of operations is $\textit{k}$, and the final value of all elements in the array is $\textit{x}$. Then we have:
62+
63+
$$
64+
\begin{aligned}
65+
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{x} \\
66+
\textit{x} &= \textit{mi} + \textit{k} \\
67+
\end{aligned}
68+
$$
69+
70+
Substituting the second equation into the first equation, we get:
71+
72+
$$
73+
\begin{aligned}
74+
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times (\textit{mi} + \textit{k}) \\
75+
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{mi} + \textit{n} \times \textit{k} \\
76+
\textit{k} &= \textit{s} - \textit{n} \times \textit{mi} \\
77+
\end{aligned}
78+
$$
79+
80+
Therefore, the minimum number of operations is $\textit{s} - \textit{n} \times \textit{mi}$.
81+
82+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array.
5883

5984
<!-- tabs:start -->
6085

@@ -127,30 +152,4 @@ function minMoves(nums: number[]): number {
127152

128153
<!-- solution:end -->
129154

130-
<!-- solution:start -->
131-
132-
### Solution 2
133-
134-
<!-- tabs:start -->
135-
136-
#### Java
137-
138-
```java
139-
class Solution {
140-
public int minMoves(int[] nums) {
141-
int s = 0;
142-
int mi = 1 << 30;
143-
for (int x : nums) {
144-
s += x;
145-
mi = Math.min(mi, x);
146-
}
147-
return s - mi * nums.length;
148-
}
149-
}
150-
```
151-
152-
<!-- tabs:end -->
153-
154-
<!-- solution:end -->
155-
156155
<!-- problem:end -->

solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution2.java

-11
This file was deleted.

solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ tags:
4040
<pre>
4141
<strong>输入:</strong>word = "CAKE"
4242
<strong>输出:</strong>3
43-
<strong>解释:
44-
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
45-
手指 1 在字母 'C' 上 -&gt; 移动距离 = 0
46-
手指 1 在字母 'A' 上 -&gt; 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
47-
手指 2 在字母 'K' 上 -&gt; 移动距离 = 0
48-
手指 2 在字母 'E' 上 -&gt; 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
43+
<strong>解释:
44+
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
45+
手指 1 在字母 'C' 上 -&gt; 移动距离 = 0
46+
手指 1 在字母 'A' 上 -&gt; 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
47+
手指 2 在字母 'K' 上 -&gt; 移动距离 = 0
48+
手指 2 在字母 'E' 上 -&gt; 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
4949
总距离 = 3
5050
</pre>
5151

solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ tags:
3838
<pre>
3939
<strong>Input:</strong> word = &quot;CAKE&quot;
4040
<strong>Output:</strong> 3
41-
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
42-
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
43-
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
44-
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
45-
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
41+
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
42+
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
43+
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
44+
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
45+
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
4646
Total distance = 3
4747
</pre>
4848

solution/3200-3299/3232.Find if Digit Game Can Be Won/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tags:
8080

8181
### 方法一:求和
8282

83-
根据题目描述,只要个位数之和不等于两位数之和,那么小红一定可以选择一个较大的和来获胜
83+
根据题目描述,只要个位数之和不等于两位数之和,那么 Alice 一定可以选择一个较大的和来获胜
8484

8585
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
8686

solution/3200-3299/3232.Find if Digit Game Can Be Won/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ tags:
7878

7979
### Solution 1: Summation
8080

81-
According to the problem description, as long as the sum of the units digits is not equal to the sum of the tens digits, Xiaohong can always choose a larger sum to win.
81+
According to the problem description, as long as the sum of the units digits is not equal to the sum of the tens digits, Alice can always choose a larger sum to win.
8282

8383
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
8484

0 commit comments

Comments
 (0)