From 88b86328431f1e06dfc2d6c288800d46b47b9ec4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 30 Nov 2024 08:24:19 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.451,453 --- .../README.md | 19 +++---- .../README_EN.md | 23 ++++---- .../Solution.php | 17 +++--- .../README.md | 42 +++------------ .../README_EN.md | 53 +++++++++---------- .../Solution2.java | 11 ---- .../README.md | 12 ++--- .../README_EN.md | 10 ++-- .../README.md | 2 +- .../README_EN.md | 2 +- 10 files changed, 80 insertions(+), 111 deletions(-) delete mode 100644 solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution2.java diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/README.md b/solution/0400-0499/0451.Sort Characters By Frequency/README.md index 777e1e8ee609a..9c9b8dcdf1f3d 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/README.md +++ b/solution/0400-0499/0451.Sort Characters By Frequency/README.md @@ -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$ 为不同字符的个数。 @@ -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; } } ``` diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md b/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md index 220845820ddcf..38f335f01e962 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md +++ b/solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md @@ -67,7 +67,11 @@ Note that 'A' and 'a' are treated as two different characters. -### 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. @@ -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; } } ``` diff --git a/solution/0400-0499/0451.Sort Characters By Frequency/Solution.php b/solution/0400-0499/0451.Sort Characters By Frequency/Solution.php index b807fc7e2152e..bbee3f2953058 100644 --- a/solution/0400-0499/0451.Sort Characters By Frequency/Solution.php +++ b/solution/0400-0499/0451.Sort Characters By Frequency/Solution.php @@ -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; } } diff --git a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README.md b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README.md index 1c4889b25db7b..3aeaad35138c0 100644 --- a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README.md +++ b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README.md @@ -57,14 +57,14 @@ 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} $$ @@ -72,13 +72,13 @@ $$ $$ \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$ 为数组的长度。 @@ -153,30 +153,4 @@ function minMoves(nums: number[]): number { - - -### 方法二 - - - -#### 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; - } -} -``` - - - - - diff --git a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README_EN.md b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README_EN.md index f2e6d325c882f..a5a5b104e6d2f 100644 --- a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README_EN.md +++ b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/README_EN.md @@ -54,7 +54,32 @@ tags: -### 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. @@ -127,30 +152,4 @@ function minMoves(nums: number[]): number { - - -### Solution 2 - - - -#### 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; - } -} -``` - - - - - diff --git a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution2.java b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution2.java deleted file mode 100644 index 5842dd023cbb9..0000000000000 --- a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution2.java +++ /dev/null @@ -1,11 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md index 1bdffb15143d8..39977f789b7d0 100644 --- a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md +++ b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README.md @@ -40,12 +40,12 @@ tags:
 输入:word = "CAKE"
 输出:3
-解释:
-使用两根手指输入 "CAKE" 的最佳方案之一是:
-手指 1 在字母 'C' 上 -> 移动距离 = 0
-手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
-手指 2 在字母 'K' 上 -> 移动距离 = 0
-手指 2 在字母 'E' 上 -> 移动距离 = 从字母 'K' 到字母 'E' 的距离  = 1
+解释: 
+使用两根手指输入 "CAKE" 的最佳方案之一是: 
+手指 1 在字母 'C' 上 -> 移动距离 = 0 
+手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2 
+手指 2 在字母 'K' 上 -> 移动距离 = 0 
+手指 2 在字母 'E' 上 -> 移动距离 = 从字母 'K' 到字母 'E' 的距离  = 1 
 总距离 = 3
 
diff --git a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md index 5e9170e5ba800..bf427209a9d80 100644 --- a/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md +++ b/solution/1300-1399/1320.Minimum Distance to Type a Word Using Two Fingers/README_EN.md @@ -38,11 +38,11 @@ tags:
 Input: word = "CAKE"
 Output: 3
-Explanation: Using two fingers, one optimal way to type "CAKE" is:
-Finger 1 on letter 'C' -> cost = 0
-Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2
-Finger 2 on letter 'K' -> cost = 0
-Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1
+Explanation: Using two fingers, one optimal way to type "CAKE" is: 
+Finger 1 on letter 'C' -> cost = 0 
+Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2 
+Finger 2 on letter 'K' -> cost = 0 
+Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1 
 Total distance = 3
 
diff --git a/solution/3200-3299/3232.Find if Digit Game Can Be Won/README.md b/solution/3200-3299/3232.Find if Digit Game Can Be Won/README.md index fa664a94c44f0..6ce53712471a4 100644 --- a/solution/3200-3299/3232.Find if Digit Game Can Be Won/README.md +++ b/solution/3200-3299/3232.Find if Digit Game Can Be Won/README.md @@ -80,7 +80,7 @@ tags: ### 方法一:求和 -根据题目描述,只要个位数之和不等于两位数之和,那么小红一定可以选择一个较大的和来获胜。 +根据题目描述,只要个位数之和不等于两位数之和,那么 Alice 一定可以选择一个较大的和来获胜。 时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/3200-3299/3232.Find if Digit Game Can Be Won/README_EN.md b/solution/3200-3299/3232.Find if Digit Game Can Be Won/README_EN.md index b5c3b2927c2e7..272632f9ba921 100644 --- a/solution/3200-3299/3232.Find if Digit Game Can Be Won/README_EN.md +++ b/solution/3200-3299/3232.Find if Digit Game Can Be Won/README_EN.md @@ -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)$.