Skip to content

feat: update solutions to lc problems: No.451,453 #3825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions solution/0400-0499/0451.Sort Characters By Frequency/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tags:

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

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

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

Expand Down Expand Up @@ -200,15 +200,16 @@ class Solution {
* @return String
*/
function frequencySort($s) {
for ($i = 0; $i < strlen($s); $i++) {
$hashtable[$s[$i]] += 1;
}
arsort($hashtable);
$keys = array_keys($hashtable);
for ($j = 0; $j < count($keys); $j++) {
$rs = $rs . str_repeat($keys[$j], $hashtable[$keys[$j]]);
$cnt = array_count_values(str_split($s));
$cs = array_keys($cnt);
usort($cs, function ($a, $b) use ($cnt) {
return $cnt[$b] <=> $cnt[$a];
});
$ans = '';
foreach ($cs as $c) {
$ans .= str_repeat($c, $cnt[$c]);
}
return $rs;
return $ans;
}
}
```
Expand Down
23 changes: 14 additions & 9 deletions solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ Note that &#39;A&#39; and &#39;a&#39; are treated as two different characters.

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + Sorting

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.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -194,15 +198,16 @@ class Solution {
* @return String
*/
function frequencySort($s) {
for ($i = 0; $i < strlen($s); $i++) {
$hashtable[$s[$i]] += 1;
}
arsort($hashtable);
$keys = array_keys($hashtable);
for ($j = 0; $j < count($keys); $j++) {
$rs = $rs . str_repeat($keys[$j], $hashtable[$keys[$j]]);
$cnt = array_count_values(str_split($s));
$cs = array_keys($cnt);
usort($cs, function ($a, $b) use ($cnt) {
return $cnt[$b] <=> $cnt[$a];
});
$ans = '';
foreach ($cs as $c) {
$ans .= str_repeat($c, $cnt[$c]);
}
return $rs;
return $ans;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ class Solution {
* @return String
*/
function frequencySort($s) {
for ($i = 0; $i < strlen($s); $i++) {
$hashtable[$s[$i]] += 1;
$cnt = array_count_values(str_split($s));
$cs = array_keys($cnt);
usort($cs, function ($a, $b) use ($cnt) {
return $cnt[$b] <=> $cnt[$a];
});
$ans = '';
foreach ($cs as $c) {
$ans .= str_repeat($c, $cnt[$c]);
}
arsort($hashtable);
$keys = array_keys($hashtable);
for ($j = 0; $j < count($keys); $j++) {
$rs = $rs . str_repeat($keys[$j], $hashtable[$keys[$j]]);
}
return $rs;
return $ans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ tags:

### 方法一:数学

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

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

$$
\begin{aligned}
s + (n - 1) \times k &= n \times x \\
x &= mi + k \\
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{x} \\
\textit{x} &= \textit{mi} + \textit{k} \\
\end{aligned}
$$

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

$$
\begin{aligned}
s + (n - 1) \times k &= n \times (mi + k) \\
s + (n - 1) \times k &= n \times mi + n \times k \\
k &= s - n \times mi \\
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times (\textit{mi} + \textit{k}) \\
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{mi} + \textit{n} \times \textit{k} \\
\textit{k} &= \textit{s} - \textit{n} \times \textit{mi} \\
\end{aligned}
$$

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

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

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

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Java

```java
class Solution {
public int minMoves(int[] nums) {
int s = 0;
int mi = 1 << 30;
for (int x : nums) {
s += x;
mi = Math.min(mi, x);
}
return s - mi * nums.length;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,32 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Mathematics

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}$.

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:

$$
\begin{aligned}
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{x} \\
\textit{x} &= \textit{mi} + \textit{k} \\
\end{aligned}
$$

Substituting the second equation into the first equation, we get:

$$
\begin{aligned}
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times (\textit{mi} + \textit{k}) \\
\textit{s} + (\textit{n} - 1) \times \textit{k} &= \textit{n} \times \textit{mi} + \textit{n} \times \textit{k} \\
\textit{k} &= \textit{s} - \textit{n} \times \textit{mi} \\
\end{aligned}
$$

Therefore, the minimum number of operations is $\textit{s} - \textit{n} \times \textit{mi}$.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array.

<!-- tabs:start -->

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

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Java

```java
class Solution {
public int minMoves(int[] nums) {
int s = 0;
int mi = 1 << 30;
for (int x : nums) {
s += x;
mi = Math.min(mi, x);
}
return s - mi * nums.length;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ tags:
<pre>
<strong>输入:</strong>word = "CAKE"
<strong>输出:</strong>3
<strong>解释:
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
手指 1 在字母 'C' 上 -&gt; 移动距离 = 0
手指 1 在字母 'A' 上 -&gt; 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
手指 2 在字母 'K' 上 -&gt; 移动距离 = 0
手指 2 在字母 'E' 上 -&gt; 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
<strong>解释:
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
手指 1 在字母 'C' 上 -&gt; 移动距离 = 0
手指 1 在字母 'A' 上 -&gt; 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
手指 2 在字母 'K' 上 -&gt; 移动距离 = 0
手指 2 在字母 'E' 上 -&gt; 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
总距离 = 3
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ tags:
<pre>
<strong>Input:</strong> word = &quot;CAKE&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is:
Finger 1 on letter &#39;C&#39; -&gt; cost = 0
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2
Finger 2 on letter &#39;K&#39; -&gt; cost = 0
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1
Total distance = 3
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ tags:

### 方法一:求和

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ tags:

### Solution 1: Summation

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.
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.

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

Expand Down