diff --git a/solution/0100-0199/0165.Compare Version Numbers/README_EN.md b/solution/0100-0199/0165.Compare Version Numbers/README_EN.md
index 365ead7268eb8..5244f1689efd8 100644
--- a/solution/0100-0199/0165.Compare Version Numbers/README_EN.md
+++ b/solution/0100-0199/0165.Compare Version Numbers/README_EN.md
@@ -6,14 +6,9 @@
## Description
-
Given two version numbers, version1
and version2
, compare them.
+Given two version strings, version1
and version2
, compare them. A version string consists of revisions separated by dots '.'
. The value of the revision is its integer conversion ignoring leading zeros.
-
-
-Version numbers consist of one or more revisions joined by a dot '.'
. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33
and 0.1
are valid version numbers.
-
-To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1
and 001
are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0
. For example, version 1.0
is less than version 1.1
because their revision 0s are the same, but their revision 1s are 0
and 1
respectively, and 0 < 1
.
+To compare version strings, compare their revision values in left-to-right order. If one of the version strings has fewer revisions, treat the missing revision values as 0
.
Return the following:
@@ -26,27 +21,39 @@
Example 1:
-
-Input: version1 = "1.01", version2 = "1.001"
-Output: 0
-Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
-
+
+
Input: version1 = "1.2", version2 = "1.10"
+
+
Output: -1
+
+
Explanation:
+
+
version1's second revision is "2" and version2's second revision is "10": 2 < 10, so version1 < version2.
+
Example 2:
-
-Input: version1 = "1.0", version2 = "1.0.0"
-Output: 0
-Explanation: version1 does not specify revision 2, which means it is treated as "0".
-
+
+
Input: version1 = "1.01", version2 = "1.001"
+
+
Output: 0
+
+
Explanation:
+
+
Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
+
Example 3:
-
-Input: version1 = "0.1", version2 = "1.1"
-Output: -1
-Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
-
+
+
Input: version1 = "1.0", version2 = "1.0.0.0"
+
+
Output: 0
+
+
Explanation:
+
+
version1 has less revisions, which means every missing revision are treated as "0".
+
Constraints:
diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md
index 91ba70764ed0c..ddca8ffaf7618 100644
--- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md
+++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README.md
@@ -10,7 +10,7 @@
有 n
名工人。 给定两个数组 quality
和 wage
,其中,quality[i]
表示第 i
名工人的工作质量,其最低期望工资为 wage[i]
。
-现在我们想雇佣 k
名工人组成一个工资组。在雇佣 一组 k
名工人时,我们必须按照下述规则向他们支付工资:
+现在我们想雇佣 k
名工人组成一个 工资组。在雇佣 一组 k
名工人时,我们必须按照下述规则向他们支付工资:
- 对工资组中的每名工人,应当按其工作质量与同组其他工人的工作质量的比例来支付工资。
@@ -24,14 +24,14 @@
-示例 1:
+示例 1:
输入: quality = [10,20,5], wage = [70,50,30], k = 2
输出: 105.00000
解释: 我们向 0 号工人支付 70,向 2 号工人支付 35。
-示例 2:
+示例 2:
输入: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
diff --git a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md
index 1c9396b3c66f8..51b31be8668a3 100644
--- a/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md
+++ b/solution/0800-0899/0857.Minimum Cost to Hire K Workers/README_EN.md
@@ -8,11 +8,11 @@
There are n
workers. You are given two integer arrays quality
and wage
where quality[i]
is the quality of the ith
worker and wage[i]
is the minimum wage expectation for the ith
worker.
-We want to hire exactly k
workers to form a paid group. To hire a group of k
workers, we must pay them according to the following rules:
+We want to hire exactly k
workers to form a paid group. To hire a group of k
workers, we must pay them according to the following rules:
- - Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
- Every worker in the paid group must be paid at least their minimum wage expectation.
+ - In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
Given the integer k
, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5
of the actual answer will be accepted.
diff --git a/solution/1100-1199/1112.Highest Grade For Each Student/README.md b/solution/1100-1199/1112.Highest Grade For Each Student/README.md
index 11bd9ce0add27..3fcf11f814b38 100644
--- a/solution/1100-1199/1112.Highest Grade For Each Student/README.md
+++ b/solution/1100-1199/1112.Highest Grade For Each Student/README.md
@@ -25,8 +25,6 @@ grade 不会为 NULL。
编写解决方案,找出每位学生获得的最高成绩和它所对应的科目,若科目成绩并列,取 course_id
最小的一门。查询结果需按 student_id
增序进行排序。
-以 任意顺序 返回结果表。
-
查询结果格式如下所示。
diff --git a/solution/2000-2099/2077.Paths in Maze That Lead to Same Room/README.md b/solution/2000-2099/2077.Paths in Maze That Lead to Same Room/README.md
index a5802c08cfc02..1ebcc2e560a79 100644
--- a/solution/2000-2099/2077.Paths in Maze That Lead to Same Room/README.md
+++ b/solution/2000-2099/2077.Paths in Maze That Lead to Same Room/README.md
@@ -8,7 +8,7 @@
-迷宫由 n
个从 1
到 n
的房间组成,有些房间由走廊连接。给定一个二维整数数组 corridors
,其中 corridors[i] = [room1i, room2i]
表示有一条走廊连接 room1i
和room2i
,允许迷宫中的一个人从 room1i
到 room1i
,反之亦然。
+迷宫由 n
个从 1
到 n
的房间组成,有些房间由走廊连接。给定一个二维整数数组 corridors
,其中 corridors[i] = [room1i, room2i]
表示有一条走廊连接 room1i
和room2i
,允许迷宫中的一个人从 room1i
到 room2i
,反之亦然。
迷宫的设计者想知道迷宫有多让人困惑。迷宫的 混乱分数 是 长度为 3 的不同的环的数量。
diff --git a/solution/2100-2199/2105.Watering Plants II/README.md b/solution/2100-2199/2105.Watering Plants II/README.md
index 114f059f467aa..1689bc12472cd 100644
--- a/solution/2100-2199/2105.Watering Plants II/README.md
+++ b/solution/2100-2199/2105.Watering Plants II/README.md
@@ -14,11 +14,9 @@
- Alice 按 从左到右 的顺序给植物浇水,从植物
0
开始。Bob 按 从右到左 的顺序给植物浇水,从植物 n - 1
开始。他们 同时 给植物浇水。
- - 如果没有足够的水 完全 浇灌下一株植物,他 / 她会立即重新灌满浇水罐。
- - 不管植物需要多少水,浇水所耗费的时间都是一样的。
- - 不能 提前重新灌满水罐。
- - 每株植物都可以由 Alice 或者 Bob 来浇水。
- - 如果 Alice 和 Bob 到达同一株植物,那么当前水罐中水更多的人会给这株植物浇水。如果他俩水量相同,那么 Alice 会给这株植物浇水。
+ - 无论需要多少水,为每株植物浇水所需的时间都是相同的。
+ - 如果 Alice/Bob 水罐中的水足以 完全 灌溉植物,他们 必须 给植物浇水。否则,他们 首先(立即)重新装满罐子,然后给植物浇水。
+ - 如果 Alice 和 Bob 到达同一株植物,那么当前水罐中水 更多 的人会给这株植物浇水。如果他俩水量相同,那么 Alice 会给这株植物浇水。
给你一个下标从 0 开始的整数数组 plants
,数组由 n
个整数组成。其中,plants[i]
为第 i
株植物需要的水量。另有两个整数 capacityA
和 capacityB
分别表示 Alice 和 Bob 水罐的容量。返回两人浇灌所有植物过程中重新灌满水罐的 次数 。
diff --git a/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md b/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md
index 7f33612356aa8..fc9c072a6e37d 100644
--- a/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md
+++ b/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md
@@ -32,7 +32,7 @@
输入:nums = [1,7,9,2,5], lower = 11, upper = 11
输出:1
-解释:只有单个公平数对:(2,9) 。
+解释:只有单个公平数对:(2,3) 。
diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/README.md b/solution/3100-3199/3141.Maximum Hamming Distances/README.md
index fad89a0244cd1..5cf76b155d39d 100644
--- a/solution/3100-3199/3141.Maximum Hamming Distances/README.md
+++ b/solution/3100-3199/3141.Maximum Hamming Distances/README.md
@@ -1,4 +1,4 @@
-# [3141. Maximum Hamming Distances 🔒](https://leetcode.cn/problems/maximum-hamming-distances)
+# [3141. 最大海明距离 🔒](https://leetcode.cn/problems/maximum-hamming-distances)
[English Version](/solution/3100-3199/3141.Maximum%20Hamming%20Distances/README_EN.md)
@@ -8,55 +8,57 @@
-Given an array nums
and an integer m
, with each element nums[i]
satisfying 0 <= nums[i] < 2m
, return an array answer
. The answer
array should be of the same length as nums
, where each element answer[i]
represents the maximum Hamming distance between nums[i]
and any other element nums[j]
in the array.
+给定一个数组 nums
和一个整数 m
,每个元素 nums[i]
满足 0 <= nums[i] < 2m
,返回数组 answer
。answer
数组应该与 nums
有相同的长度,每个元素 answer[i]
表示 nums[i]
和数组中其它任何元素 nums[j]
的最大 海明距离。
-The Hamming distance between two binary integers is defined as the number of positions at which the corresponding bits differ (add leading zeroes if needed).
+两个二进制整数之间的 海明距离 定义为对应位上二进制位不同的数量(如果需要,添加前置零)。
-Example 1:
+
+示例 1:
-
Input: nums = [9,12,9,11], m = 4
+
输入:nums = [9,12,9,11], m = 4
-
Output: [2,3,2,3]
+
输出:[2,3,2,3]
-
Explanation:
+
解释:
-
The binary representation of nums = [1001,1100,1001,1011]
.
+
二进制表示为 nums = [1001,1100,1001,1011]
。
-
The maximum hamming distances for each index are:
+
每个下标的最大海明距离为:
- nums[0]
: 1001 and 1100 have a distance of 2.
- nums[1]
: 1100 and 1011 have a distance of 3.
- nums[2]
: 1001 and 1100 have a distance of 2.
- nums[3]
: 1011 and 1100 have a distance of 3.
+ nums[0]
:1001 与 1100 距离为 2。
+ nums[1]
:1100 与 1011 距离为 3。
+ nums[2]
:1001 与 1100 距离为 2。
+ nums[3]
:1011 与 1100 距离为 3。
-Example 2:
+示例 2:
-
Input: nums = [3,4,6,10], m = 4
+
输入:nums = [3,4,6,10], m = 4
-
Output: [3,3,2,3]
+
输出:[3,3,2,3]
-
Explanation:
+
解释:
-
The binary representation of nums = [0011,0100,0110,1010]
.
+
二进制表示为 nums = [0011,0100,0110,1010]
。
-
The maximum hamming distances for each index are:
+
每个下标的最大海明距离为:
- nums[0]
: 0011 and 0100 have a distance of 3.
- nums[1]
: 0100 and 0011 have a distance of 3.
- nums[2]
: 0110 and 1010 have a distance of 2.
- nums[3]
: 1010 and 0100 have a distance of 3.
+ nums[0]
:0011 与 0100 距离为 3。
+ nums[1]
:0100 与 0011 距离为 3。
+ nums[2]
:0110 与 1010 距离为 2。
+ nums[3]
:1010 与 0100 距离为 3。
-Constraints:
+
+提示:
1 <= m <= 17
@@ -66,24 +68,165 @@
## 解法
-### 方法一
+### 方法一:逆向思维 + BFS
+
+题目需要我们求出数组每个元素和其他元素的最大海明距离,我们不妨换个角度思考,我们对每个元素取反,然后求出其到数组中其他元素的最小海明距离,那么 $m$ 减去这个最小海明距离就是我们要求的最大海明距离。
+
+我们可以使用广度优先搜索来求出每个取反的元素到其他元素的最小海明距离。
+
+具体步骤如下:
+
+1. 初始化一个数组 $\text{dist}$,数组长度为 $2^m$,用来记录每个取反后的元素到其他元素的最小海明距离,初始时全部置为 $-1$。
+2. 遍历数组 $\text{nums}$,将每个元素的取反值置为 $0$,并将其加入队列 $\text{q}$。
+3. 从 $k = 1$ 开始,不断遍历队列 $\text{q}$,每次遍历时,将队列中的元素取出,然后对其进行 $m$ 次取反操作,将取反后的元素加入队列 $\text{t}$,并将其到原元素的最小海明距离置为 $k$。
+4. 重复步骤 3,直到队列为空。
+
+最后,我们遍历数组 $\text{nums}$,将每个元素取反后的值作为下标,从 $\text{dist}$ 数组中取出对应的最小海明距离,然后用 $m$ 减去这个值,就是我们要求的最大海明距离。
+
+时间复杂度 $O(2^m)$,空间复杂度 $O(2^m)$。其中 $m$ 为题目给定的整数。
```python
-
+class Solution:
+ def maxHammingDistances(self, nums: List[int], m: int) -> List[int]:
+ dist = [-1] * (1 << m)
+ for x in nums:
+ dist[x] = 0
+ q = nums
+ k = 1
+ while q:
+ t = []
+ for x in q:
+ for i in range(m):
+ y = x ^ (1 << i)
+ if dist[y] == -1:
+ t.append(y)
+ dist[y] = k
+ q = t
+ k += 1
+ return [m - dist[x ^ ((1 << m) - 1)] for x in nums]
```
```java
-
+class Solution {
+ public int[] maxHammingDistances(int[] nums, int m) {
+ int[] dist = new int[1 << m];
+ Arrays.fill(dist, -1);
+ Deque q = new ArrayDeque<>();
+ for (int x : nums) {
+ dist[x] = 0;
+ q.offer(x);
+ }
+ for (int k = 1; !q.isEmpty(); ++k) {
+ for (int t = q.size(); t > 0; --t) {
+ int x = q.poll();
+ for (int i = 0; i < m; ++i) {
+ int y = x ^ (1 << i);
+ if (dist[y] == -1) {
+ q.offer(y);
+ dist[y] = k;
+ }
+ }
+ }
+ }
+ for (int i = 0; i < nums.length; ++i) {
+ nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
+ }
+ return nums;
+ }
+}
```
```cpp
-
+class Solution {
+public:
+ vector maxHammingDistances(vector& nums, int m) {
+ int dist[1 << m];
+ memset(dist, -1, sizeof(dist));
+ queue q;
+ for (int x : nums) {
+ dist[x] = 0;
+ q.push(x);
+ }
+ for (int k = 1; q.size(); ++k) {
+ for (int t = q.size(); t; --t) {
+ int x = q.front();
+ q.pop();
+ for (int i = 0; i < m; ++i) {
+ int y = x ^ (1 << i);
+ if (dist[y] == -1) {
+ dist[y] = k;
+ q.push(y);
+ }
+ }
+ }
+ }
+ for (int& x : nums) {
+ x = m - dist[x ^ ((1 << m) - 1)];
+ }
+ return nums;
+ }
+};
```
```go
+func maxHammingDistances(nums []int, m int) []int {
+ dist := make([]int, 1< 0; k++ {
+ t := []int{}
+ for _, x := range q {
+ for i := 0; i < m; i++ {
+ y := x ^ (1 << i)
+ if dist[y] == -1 {
+ dist[y] = k
+ t = append(t, y)
+ }
+ }
+ }
+ q = t
+ }
+ for i, x := range nums {
+ nums[i] = m - dist[x^(1< -1);
+ const q: number[] = [];
+ for (const x of nums) {
+ dist[x] = 0;
+ q.push(x);
+ }
+ for (let k = 1; q.length; ++k) {
+ const t: number[] = [];
+ for (const x of q) {
+ for (let i = 0; i < m; ++i) {
+ const y = x ^ (1 << i);
+ if (dist[y] === -1) {
+ dist[y] = k;
+ t.push(y);
+ }
+ }
+ }
+ q.splice(0, q.length, ...t);
+ }
+ for (let i = 0; i < nums.length; ++i) {
+ nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
+ }
+ return nums;
+}
```
diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md b/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md
index 1c759d9a2d497..3ddd50fc3c85e 100644
--- a/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md
+++ b/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md
@@ -64,24 +64,165 @@
## Solutions
-### Solution 1
+### Solution 1: Reverse Thinking + BFS
+
+The problem requires us to find the maximum Hamming distance between each element and other elements in the array. We can think in reverse: for each element, we take its complement and find the minimum Hamming distance to other elements in the array. Then, the maximum Hamming distance we are looking for is $m$ minus this minimum Hamming distance.
+
+We can use Breadth-First Search (BFS) to find the minimum Hamming distance from each complemented element to other elements.
+
+The specific steps are as follows:
+
+1. Initialize an array $\text{dist}$ with a length of $2^m$ to record the minimum Hamming distance from each complemented element to other elements. Initially, all are set to $-1$.
+2. Traverse the array $\text{nums}$, set the complement of each element to $0$, and add it to the queue $\text{q}$.
+3. Starting from $k = 1$, continuously traverse the queue $\text{q}$. Each time, take out the elements in the queue, perform $m$ complement operations on them, add the complemented elements to the queue $\text{t}$, and set the minimum Hamming distance to the original element to $k$.
+4. Repeat step 3 until the queue is empty.
+
+Finally, we traverse the array $\text{nums}$, take the complement of each element as the index, and take out the corresponding minimum Hamming distance from the $\text{dist}$ array. Then, $m$ minus this value is the maximum Hamming distance we are looking for.
+
+The time complexity is $O(2^m)$, and the space complexity is $O(2^m)$, where $m$ is the integer given in the problem.
```python
-
+class Solution:
+ def maxHammingDistances(self, nums: List[int], m: int) -> List[int]:
+ dist = [-1] * (1 << m)
+ for x in nums:
+ dist[x] = 0
+ q = nums
+ k = 1
+ while q:
+ t = []
+ for x in q:
+ for i in range(m):
+ y = x ^ (1 << i)
+ if dist[y] == -1:
+ t.append(y)
+ dist[y] = k
+ q = t
+ k += 1
+ return [m - dist[x ^ ((1 << m) - 1)] for x in nums]
```
```java
-
+class Solution {
+ public int[] maxHammingDistances(int[] nums, int m) {
+ int[] dist = new int[1 << m];
+ Arrays.fill(dist, -1);
+ Deque q = new ArrayDeque<>();
+ for (int x : nums) {
+ dist[x] = 0;
+ q.offer(x);
+ }
+ for (int k = 1; !q.isEmpty(); ++k) {
+ for (int t = q.size(); t > 0; --t) {
+ int x = q.poll();
+ for (int i = 0; i < m; ++i) {
+ int y = x ^ (1 << i);
+ if (dist[y] == -1) {
+ q.offer(y);
+ dist[y] = k;
+ }
+ }
+ }
+ }
+ for (int i = 0; i < nums.length; ++i) {
+ nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
+ }
+ return nums;
+ }
+}
```
```cpp
-
+class Solution {
+public:
+ vector maxHammingDistances(vector& nums, int m) {
+ int dist[1 << m];
+ memset(dist, -1, sizeof(dist));
+ queue q;
+ for (int x : nums) {
+ dist[x] = 0;
+ q.push(x);
+ }
+ for (int k = 1; q.size(); ++k) {
+ for (int t = q.size(); t; --t) {
+ int x = q.front();
+ q.pop();
+ for (int i = 0; i < m; ++i) {
+ int y = x ^ (1 << i);
+ if (dist[y] == -1) {
+ dist[y] = k;
+ q.push(y);
+ }
+ }
+ }
+ }
+ for (int& x : nums) {
+ x = m - dist[x ^ ((1 << m) - 1)];
+ }
+ return nums;
+ }
+};
```
```go
+func maxHammingDistances(nums []int, m int) []int {
+ dist := make([]int, 1< 0; k++ {
+ t := []int{}
+ for _, x := range q {
+ for i := 0; i < m; i++ {
+ y := x ^ (1 << i)
+ if dist[y] == -1 {
+ dist[y] = k
+ t = append(t, y)
+ }
+ }
+ }
+ q = t
+ }
+ for i, x := range nums {
+ nums[i] = m - dist[x^(1< -1);
+ const q: number[] = [];
+ for (const x of nums) {
+ dist[x] = 0;
+ q.push(x);
+ }
+ for (let k = 1; q.length; ++k) {
+ const t: number[] = [];
+ for (const x of q) {
+ for (let i = 0; i < m; ++i) {
+ const y = x ^ (1 << i);
+ if (dist[y] === -1) {
+ dist[y] = k;
+ t.push(y);
+ }
+ }
+ }
+ q.splice(0, q.length, ...t);
+ }
+ for (let i = 0; i < nums.length; ++i) {
+ nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
+ }
+ return nums;
+}
```
diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/Solution.cpp b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.cpp
new file mode 100644
index 0000000000000..268f21327b2f7
--- /dev/null
+++ b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.cpp
@@ -0,0 +1,29 @@
+class Solution {
+public:
+ vector maxHammingDistances(vector& nums, int m) {
+ int dist[1 << m];
+ memset(dist, -1, sizeof(dist));
+ queue q;
+ for (int x : nums) {
+ dist[x] = 0;
+ q.push(x);
+ }
+ for (int k = 1; q.size(); ++k) {
+ for (int t = q.size(); t; --t) {
+ int x = q.front();
+ q.pop();
+ for (int i = 0; i < m; ++i) {
+ int y = x ^ (1 << i);
+ if (dist[y] == -1) {
+ dist[y] = k;
+ q.push(y);
+ }
+ }
+ }
+ }
+ for (int& x : nums) {
+ x = m - dist[x ^ ((1 << m) - 1)];
+ }
+ return nums;
+ }
+};
\ No newline at end of file
diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/Solution.go b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.go
new file mode 100644
index 0000000000000..27593800adda3
--- /dev/null
+++ b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.go
@@ -0,0 +1,28 @@
+func maxHammingDistances(nums []int, m int) []int {
+ dist := make([]int, 1< 0; k++ {
+ t := []int{}
+ for _, x := range q {
+ for i := 0; i < m; i++ {
+ y := x ^ (1 << i)
+ if dist[y] == -1 {
+ dist[y] = k
+ t = append(t, y)
+ }
+ }
+ }
+ q = t
+ }
+ for i, x := range nums {
+ nums[i] = m - dist[x^(1< q = new ArrayDeque<>();
+ for (int x : nums) {
+ dist[x] = 0;
+ q.offer(x);
+ }
+ for (int k = 1; !q.isEmpty(); ++k) {
+ for (int t = q.size(); t > 0; --t) {
+ int x = q.poll();
+ for (int i = 0; i < m; ++i) {
+ int y = x ^ (1 << i);
+ if (dist[y] == -1) {
+ q.offer(y);
+ dist[y] = k;
+ }
+ }
+ }
+ }
+ for (int i = 0; i < nums.length; ++i) {
+ nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
+ }
+ return nums;
+ }
+}
\ No newline at end of file
diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/Solution.py b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.py
new file mode 100644
index 0000000000000..3b1cf740aa74e
--- /dev/null
+++ b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.py
@@ -0,0 +1,18 @@
+class Solution:
+ def maxHammingDistances(self, nums: List[int], m: int) -> List[int]:
+ dist = [-1] * (1 << m)
+ for x in nums:
+ dist[x] = 0
+ q = nums
+ k = 1
+ while q:
+ t = []
+ for x in q:
+ for i in range(m):
+ y = x ^ (1 << i)
+ if dist[y] == -1:
+ t.append(y)
+ dist[y] = k
+ q = t
+ k += 1
+ return [m - dist[x ^ ((1 << m) - 1)] for x in nums]
diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/Solution.ts b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.ts
new file mode 100644
index 0000000000000..3b2eddf52e9cf
--- /dev/null
+++ b/solution/3100-3199/3141.Maximum Hamming Distances/Solution.ts
@@ -0,0 +1,25 @@
+function maxHammingDistances(nums: number[], m: number): number[] {
+ const dist: number[] = Array.from({ length: 1 << m }, () => -1);
+ const q: number[] = [];
+ for (const x of nums) {
+ dist[x] = 0;
+ q.push(x);
+ }
+ for (let k = 1; q.length; ++k) {
+ const t: number[] = [];
+ for (const x of q) {
+ for (let i = 0; i < m; ++i) {
+ const y = x ^ (1 << i);
+ if (dist[y] === -1) {
+ dist[y] = k;
+ t.push(y);
+ }
+ }
+ }
+ q.splice(0, q.length, ...t);
+ }
+ for (let i = 0; i < nums.length; ++i) {
+ nums[i] = m - dist[nums[i] ^ ((1 << m) - 1)];
+ }
+ return nums;
+}
diff --git a/solution/README.md b/solution/README.md
index 4bd803d68e46a..9dfe1771d8b49 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -3151,7 +3151,7 @@
| 3138 | [同位字符串连接的最小长度](/solution/3100-3199/3138.Minimum%20Length%20of%20Anagram%20Concatenation/README.md) | `哈希表`,`字符串`,`计数` | 中等 | 第 396 场周赛 |
| 3139 | [使数组中所有元素相等的最小开销](/solution/3100-3199/3139.Minimum%20Cost%20to%20Equalize%20Array/README.md) | `贪心`,`数组`,`枚举` | 困难 | 第 396 场周赛 |
| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | `数据库` | 中等 | 🔒 |
-| 3141 | [Maximum Hamming Distances](/solution/3100-3199/3141.Maximum%20Hamming%20Distances/README.md) | | 困难 | 🔒 |
+| 3141 | [最大海明距离](/solution/3100-3199/3141.Maximum%20Hamming%20Distances/README.md) | | 困难 | 🔒 |
## 版权