From 7becc6f8021a055a03a7245e7b43d3720f4a3cf2 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 9 May 2024 09:06:09 +0800 Subject: [PATCH] feat: update lc problems --- lcci/16.11.Diving Board/README.md | 2 +- lcci/16.11.Diving Board/README_EN.md | 2 +- lcci/16.14.Best Line/README.md | 6 +- lcci/16.14.Best Line/README_EN.md | 6 +- lcci/16.15.Master Mind/README.md | 6 +- lcci/16.15.Master Mind/README_EN.md | 6 +- .../2105.Watering Plants II/README.md | 183 +++++++++++------- .../2105.Watering Plants II/README_EN.md | 183 +++++++++++------- .../2105.Watering Plants II/Solution.cpp | 29 ++- .../2105.Watering Plants II/Solution.go | 33 ++-- .../2105.Watering Plants II/Solution.java | 28 +-- .../2105.Watering Plants II/Solution.py | 28 ++- .../2105.Watering Plants II/Solution.rs | 32 +++ .../2105.Watering Plants II/Solution.ts | 19 ++ .../README.md | 2 +- .../README_EN.md | 2 +- solution/3100-3199/3136.Valid Word/README.md | 2 +- .../3100-3199/3136.Valid Word/README_EN.md | 2 +- .../README.md | 2 +- .../README_EN.md | 2 +- .../README.md | 2 +- .../README_EN.md | 2 +- .../README.md | 2 +- .../README_EN.md | 2 +- .../README.md | 2 +- .../README_EN.md | 2 +- .../3141.Maximum Hamming Distances/README.md | 91 +++++++++ .../README_EN.md | 89 +++++++++ solution/DATABASE_README.md | 2 +- solution/DATABASE_README_EN.md | 2 +- solution/README.md | 13 +- solution/README_EN.md | 13 +- 32 files changed, 539 insertions(+), 258 deletions(-) create mode 100644 solution/2100-2199/2105.Watering Plants II/Solution.rs create mode 100644 solution/2100-2199/2105.Watering Plants II/Solution.ts create mode 100644 solution/3100-3199/3141.Maximum Hamming Distances/README.md create mode 100644 solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md diff --git a/lcci/16.11.Diving Board/README.md b/lcci/16.11.Diving Board/README.md index a37dfdfcab9c2..ebd4001bbe191 100644 --- a/lcci/16.11.Diving Board/README.md +++ b/lcci/16.11.Diving Board/README.md @@ -121,7 +121,7 @@ class Solution { if shorter == longer { return [shorter * k] } - + var ans = [Int](repeating: 0, count: k + 1) for i in 0...k { ans[i] = longer * i + shorter * (k - i) diff --git a/lcci/16.11.Diving Board/README_EN.md b/lcci/16.11.Diving Board/README_EN.md index acca3d038fff5..7daf5dd08296d 100644 --- a/lcci/16.11.Diving Board/README_EN.md +++ b/lcci/16.11.Diving Board/README_EN.md @@ -131,7 +131,7 @@ class Solution { if shorter == longer { return [shorter * k] } - + var ans = [Int](repeating: 0, count: k + 1) for i in 0...k { ans[i] = longer * i + shorter * (k - i) diff --git a/lcci/16.14.Best Line/README.md b/lcci/16.14.Best Line/README.md index 71795ae5ccdf0..10b9259f33f0a 100644 --- a/lcci/16.14.Best Line/README.md +++ b/lcci/16.14.Best Line/README.md @@ -144,13 +144,13 @@ class Solution { let n = points.count var maxCount = 0 var answer = [Int](repeating: 0, count: 2) - + for i in 0.. ```python class Solution: def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int: - i, j = 0, len(plants) - 1 - ans = 0 a, b = capacityA, capacityB - while i <= j: - if i == j: - if max(capacityA, capacityB) < plants[i]: - ans += 1 - break - if capacityA < plants[i]: - capacityA = a - plants[i] + ans = 0 + i, j = 0, len(plants) - 1 + while i < j: + if a < plants[i]: ans += 1 - else: - capacityA -= plants[i] - if capacityB < plants[j]: - capacityB = b - plants[j] + a = capacityA + a -= plants[i] + if b < plants[j]: ans += 1 - else: - capacityB -= plants[j] - i += 1 - j -= 1 + b = capacityB + b -= plants[j] + i, j = i + 1, j - 1 + ans += i == j and max(a, b) < plants[i] return ans ``` ```java class Solution { public int minimumRefill(int[] plants, int capacityA, int capacityB) { + int a = capacityA, b = capacityB; + int ans = 0; int i = 0, j = plants.length - 1; - int ans = 0, a = capacityA, b = capacityB; - while (i <= j) { - if (i == j) { - if (Math.max(capacityA, capacityB) < plants[i]) { - ++ans; - } - break; - } - if (capacityA < plants[i]) { - capacityA = a - plants[i]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { ++ans; - } else { - capacityA -= plants[i]; + a = capacityA; } - if (capacityB < plants[j]) { - capacityB = b - plants[j]; + a -= plants[i]; + if (b < plants[j]) { ++ans; - } else { - capacityB -= plants[j]; + b = capacityB; } - ++i; - --j; + b -= plants[j]; } + ans += i == j && Math.max(a, b) < plants[i] ? 1 : 0; return ans; } } @@ -138,59 +130,104 @@ class Solution { class Solution { public: int minimumRefill(vector& plants, int capacityA, int capacityB) { + int a = capacityA, b = capacityB; + int ans = 0; int i = 0, j = plants.size() - 1; - int ans = 0, a = capacityA, b = capacityB; - while (i <= j) { - if (i == j) { - if (max(capacityA, capacityB) < plants[i]) ++ans; - break; - } - if (capacityA < plants[i]) { - capacityA = a - plants[i]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { ++ans; - } else - capacityA -= plants[i]; - - if (capacityB < plants[j]) { - capacityB = b - plants[j]; + a = capacityA; + } + a -= plants[i]; + if (b < plants[j]) { ++ans; - } else - capacityB -= plants[j]; - ++i; - --j; + b = capacityB; + } + b -= plants[j]; } + ans += i == j && max(a, b) < plants[i]; return ans; } }; ``` ```go -func minimumRefill(plants []int, capacityA int, capacityB int) int { +func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) { + a, b := capacityA, capacityB i, j := 0, len(plants)-1 - ans, a, b := 0, capacityA, capacityB - for i <= j { - if i == j { - if max(capacityA, capacityB) < plants[i] { - ans++ - } - break - } - if capacityA < plants[i] { - capacityA = a - plants[i] + for ; i < j; i, j = i+1, j-1 { + if a < plants[i] { ans++ - } else { - capacityA -= plants[i] + a = capacityA } - if capacityB < plants[j] { - capacityB = b - plants[j] + a -= plants[i] + if b < plants[j] { ans++ - } else { - capacityB -= plants[j] + b = capacityB } - i++ - j-- + b -= plants[j] + } + if i == j && max(a, b) < plants[i] { + ans++ } - return ans + return +} +``` + +```ts +function minimumRefill(plants: number[], capacityA: number, capacityB: number): number { + let [a, b] = [capacityA, capacityB]; + let ans = 0; + let [i, j] = [0, plants.length - 1]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { + ++ans; + a = capacityA; + } + a -= plants[i]; + if (b < plants[j]) { + ++ans; + b = capacityB; + } + b -= plants[j]; + } + ans += i === j && Math.max(a, b) < plants[i] ? 1 : 0; + return ans; +} +``` + +```rust +impl Solution { + pub fn minimum_refill(plants: Vec, capacity_a: i32, capacity_b: i32) -> i32 { + let mut a = capacity_a; + let mut b = capacity_b; + let mut ans = 0; + let mut i = 0; + let mut j = plants.len() - 1; + + while i < j { + if a < plants[i] { + ans += 1; + a = capacity_a; + } + a -= plants[i]; + + if b < plants[j] { + ans += 1; + b = capacity_b; + } + b -= plants[j]; + + i += 1; + j -= 1; + } + + if i == j && a.max(b) < plants[i] { + ans += 1; + } + + ans + } } ``` diff --git a/solution/2100-2199/2105.Watering Plants II/README_EN.md b/solution/2100-2199/2105.Watering Plants II/README_EN.md index 1d89577cc2843..41e5ba437938f 100644 --- a/solution/2100-2199/2105.Watering Plants II/README_EN.md +++ b/solution/2100-2199/2105.Watering Plants II/README_EN.md @@ -69,63 +69,55 @@ So, the total number of times they have to refill is 0. ## Solutions -### Solution 1 +### Solution 1: Two Pointers + Simulation + +We use two variables $a$ and $b$ to represent the amount of water Alice and Bob have, initially $a = \text{capacityA}$, $b = \text{capacityB}$. Then we use two pointers $i$ and $j$ to point to the head and tail of the plant array, and simulate the process of Alice and Bob watering from both ends to the middle. + +When $i < j$, we judge whether Alice and Bob have enough water to water the plants. If not, we refill the watering cans. Then we update the amount of water $a$ and $b$, and move the pointers $i$ and $j$. Finally, we need to judge whether $i$ and $j$ are equal. If they are equal, we need to judge whether $\max(a, b)$ is less than the amount of water the plant needs. If it is less, we need to refill the watering cans again. + +The time complexity is $O(n)$, where $n$ is the length of the plant array. The space complexity is $O(1)$. ```python class Solution: def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int: - i, j = 0, len(plants) - 1 - ans = 0 a, b = capacityA, capacityB - while i <= j: - if i == j: - if max(capacityA, capacityB) < plants[i]: - ans += 1 - break - if capacityA < plants[i]: - capacityA = a - plants[i] + ans = 0 + i, j = 0, len(plants) - 1 + while i < j: + if a < plants[i]: ans += 1 - else: - capacityA -= plants[i] - if capacityB < plants[j]: - capacityB = b - plants[j] + a = capacityA + a -= plants[i] + if b < plants[j]: ans += 1 - else: - capacityB -= plants[j] - i += 1 - j -= 1 + b = capacityB + b -= plants[j] + i, j = i + 1, j - 1 + ans += i == j and max(a, b) < plants[i] return ans ``` ```java class Solution { public int minimumRefill(int[] plants, int capacityA, int capacityB) { + int a = capacityA, b = capacityB; + int ans = 0; int i = 0, j = plants.length - 1; - int ans = 0, a = capacityA, b = capacityB; - while (i <= j) { - if (i == j) { - if (Math.max(capacityA, capacityB) < plants[i]) { - ++ans; - } - break; - } - if (capacityA < plants[i]) { - capacityA = a - plants[i]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { ++ans; - } else { - capacityA -= plants[i]; + a = capacityA; } - if (capacityB < plants[j]) { - capacityB = b - plants[j]; + a -= plants[i]; + if (b < plants[j]) { ++ans; - } else { - capacityB -= plants[j]; + b = capacityB; } - ++i; - --j; + b -= plants[j]; } + ans += i == j && Math.max(a, b) < plants[i] ? 1 : 0; return ans; } } @@ -135,59 +127,104 @@ class Solution { class Solution { public: int minimumRefill(vector& plants, int capacityA, int capacityB) { + int a = capacityA, b = capacityB; + int ans = 0; int i = 0, j = plants.size() - 1; - int ans = 0, a = capacityA, b = capacityB; - while (i <= j) { - if (i == j) { - if (max(capacityA, capacityB) < plants[i]) ++ans; - break; - } - if (capacityA < plants[i]) { - capacityA = a - plants[i]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { ++ans; - } else - capacityA -= plants[i]; - - if (capacityB < plants[j]) { - capacityB = b - plants[j]; + a = capacityA; + } + a -= plants[i]; + if (b < plants[j]) { ++ans; - } else - capacityB -= plants[j]; - ++i; - --j; + b = capacityB; + } + b -= plants[j]; } + ans += i == j && max(a, b) < plants[i]; return ans; } }; ``` ```go -func minimumRefill(plants []int, capacityA int, capacityB int) int { +func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) { + a, b := capacityA, capacityB i, j := 0, len(plants)-1 - ans, a, b := 0, capacityA, capacityB - for i <= j { - if i == j { - if max(capacityA, capacityB) < plants[i] { - ans++ - } - break - } - if capacityA < plants[i] { - capacityA = a - plants[i] + for ; i < j; i, j = i+1, j-1 { + if a < plants[i] { ans++ - } else { - capacityA -= plants[i] + a = capacityA } - if capacityB < plants[j] { - capacityB = b - plants[j] + a -= plants[i] + if b < plants[j] { ans++ - } else { - capacityB -= plants[j] + b = capacityB } - i++ - j-- + b -= plants[j] + } + if i == j && max(a, b) < plants[i] { + ans++ } - return ans + return +} +``` + +```ts +function minimumRefill(plants: number[], capacityA: number, capacityB: number): number { + let [a, b] = [capacityA, capacityB]; + let ans = 0; + let [i, j] = [0, plants.length - 1]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { + ++ans; + a = capacityA; + } + a -= plants[i]; + if (b < plants[j]) { + ++ans; + b = capacityB; + } + b -= plants[j]; + } + ans += i === j && Math.max(a, b) < plants[i] ? 1 : 0; + return ans; +} +``` + +```rust +impl Solution { + pub fn minimum_refill(plants: Vec, capacity_a: i32, capacity_b: i32) -> i32 { + let mut a = capacity_a; + let mut b = capacity_b; + let mut ans = 0; + let mut i = 0; + let mut j = plants.len() - 1; + + while i < j { + if a < plants[i] { + ans += 1; + a = capacity_a; + } + a -= plants[i]; + + if b < plants[j] { + ans += 1; + b = capacity_b; + } + b -= plants[j]; + + i += 1; + j -= 1; + } + + if i == j && a.max(b) < plants[i] { + ans += 1; + } + + ans + } } ``` diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.cpp b/solution/2100-2199/2105.Watering Plants II/Solution.cpp index f5b6e8daa9d6f..313e675b6f626 100644 --- a/solution/2100-2199/2105.Watering Plants II/Solution.cpp +++ b/solution/2100-2199/2105.Watering Plants II/Solution.cpp @@ -1,27 +1,22 @@ class Solution { public: int minimumRefill(vector& plants, int capacityA, int capacityB) { + int a = capacityA, b = capacityB; + int ans = 0; int i = 0, j = plants.size() - 1; - int ans = 0, a = capacityA, b = capacityB; - while (i <= j) { - if (i == j) { - if (max(capacityA, capacityB) < plants[i]) ++ans; - break; - } - if (capacityA < plants[i]) { - capacityA = a - plants[i]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { ++ans; - } else - capacityA -= plants[i]; - - if (capacityB < plants[j]) { - capacityB = b - plants[j]; + a = capacityA; + } + a -= plants[i]; + if (b < plants[j]) { ++ans; - } else - capacityB -= plants[j]; - ++i; - --j; + b = capacityB; + } + b -= plants[j]; } + ans += i == j && max(a, b) < plants[i]; return ans; } }; \ No newline at end of file diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.go b/solution/2100-2199/2105.Watering Plants II/Solution.go index d9ed9bb5031a0..3ad4119919c5e 100644 --- a/solution/2100-2199/2105.Watering Plants II/Solution.go +++ b/solution/2100-2199/2105.Watering Plants II/Solution.go @@ -1,27 +1,20 @@ -func minimumRefill(plants []int, capacityA int, capacityB int) int { +func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) { + a, b := capacityA, capacityB i, j := 0, len(plants)-1 - ans, a, b := 0, capacityA, capacityB - for i <= j { - if i == j { - if max(capacityA, capacityB) < plants[i] { - ans++ - } - break - } - if capacityA < plants[i] { - capacityA = a - plants[i] + for ; i < j; i, j = i+1, j-1 { + if a < plants[i] { ans++ - } else { - capacityA -= plants[i] + a = capacityA } - if capacityB < plants[j] { - capacityB = b - plants[j] + a -= plants[i] + if b < plants[j] { ans++ - } else { - capacityB -= plants[j] + b = capacityB } - i++ - j-- + b -= plants[j] + } + if i == j && max(a, b) < plants[i] { + ans++ } - return ans + return } \ No newline at end of file diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.java b/solution/2100-2199/2105.Watering Plants II/Solution.java index dc8659ede232c..708a843b48ba0 100644 --- a/solution/2100-2199/2105.Watering Plants II/Solution.java +++ b/solution/2100-2199/2105.Watering Plants II/Solution.java @@ -1,29 +1,21 @@ class Solution { public int minimumRefill(int[] plants, int capacityA, int capacityB) { + int a = capacityA, b = capacityB; + int ans = 0; int i = 0, j = plants.length - 1; - int ans = 0, a = capacityA, b = capacityB; - while (i <= j) { - if (i == j) { - if (Math.max(capacityA, capacityB) < plants[i]) { - ++ans; - } - break; - } - if (capacityA < plants[i]) { - capacityA = a - plants[i]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { ++ans; - } else { - capacityA -= plants[i]; + a = capacityA; } - if (capacityB < plants[j]) { - capacityB = b - plants[j]; + a -= plants[i]; + if (b < plants[j]) { ++ans; - } else { - capacityB -= plants[j]; + b = capacityB; } - ++i; - --j; + b -= plants[j]; } + ans += i == j && Math.max(a, b) < plants[i] ? 1 : 0; return ans; } } \ No newline at end of file diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.py b/solution/2100-2199/2105.Watering Plants II/Solution.py index a74f07cc8273f..b7c43599f9d08 100644 --- a/solution/2100-2199/2105.Watering Plants II/Solution.py +++ b/solution/2100-2199/2105.Watering Plants II/Solution.py @@ -1,23 +1,17 @@ class Solution: def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int: - i, j = 0, len(plants) - 1 - ans = 0 a, b = capacityA, capacityB - while i <= j: - if i == j: - if max(capacityA, capacityB) < plants[i]: - ans += 1 - break - if capacityA < plants[i]: - capacityA = a - plants[i] + ans = 0 + i, j = 0, len(plants) - 1 + while i < j: + if a < plants[i]: ans += 1 - else: - capacityA -= plants[i] - if capacityB < plants[j]: - capacityB = b - plants[j] + a = capacityA + a -= plants[i] + if b < plants[j]: ans += 1 - else: - capacityB -= plants[j] - i += 1 - j -= 1 + b = capacityB + b -= plants[j] + i, j = i + 1, j - 1 + ans += i == j and max(a, b) < plants[i] return ans diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.rs b/solution/2100-2199/2105.Watering Plants II/Solution.rs new file mode 100644 index 0000000000000..7fc62a6553867 --- /dev/null +++ b/solution/2100-2199/2105.Watering Plants II/Solution.rs @@ -0,0 +1,32 @@ +impl Solution { + pub fn minimum_refill(plants: Vec, capacity_a: i32, capacity_b: i32) -> i32 { + let mut a = capacity_a; + let mut b = capacity_b; + let mut ans = 0; + let mut i = 0; + let mut j = plants.len() - 1; + + while i < j { + if a < plants[i] { + ans += 1; + a = capacity_a; + } + a -= plants[i]; + + if b < plants[j] { + ans += 1; + b = capacity_b; + } + b -= plants[j]; + + i += 1; + j -= 1; + } + + if i == j && a.max(b) < plants[i] { + ans += 1; + } + + ans + } +} diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.ts b/solution/2100-2199/2105.Watering Plants II/Solution.ts new file mode 100644 index 0000000000000..efad64718cd82 --- /dev/null +++ b/solution/2100-2199/2105.Watering Plants II/Solution.ts @@ -0,0 +1,19 @@ +function minimumRefill(plants: number[], capacityA: number, capacityB: number): number { + let [a, b] = [capacityA, capacityB]; + let ans = 0; + let [i, j] = [0, plants.length - 1]; + for (; i < j; ++i, --j) { + if (a < plants[i]) { + ++ans; + a = capacityA; + } + a -= plants[i]; + if (b < plants[j]) { + ++ans; + b = capacityB; + } + b -= plants[j]; + } + ans += i === j && Math.max(a, b) < plants[i] ? 1 : 0; + return ans; +} diff --git a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md index 5dc8a2f33d27b..460cd7fdb889f 100644 --- a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md +++ b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md @@ -2,7 +2,7 @@ [English Version](/solution/3100-3199/3135.Equalize%20Strings%20by%20Adding%20or%20Removing%20Characters%20at%20Ends/README_EN.md) - + ## 题目描述 diff --git a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md index bd619d0c68743..3d8d3e4c1eeb5 100644 --- a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md +++ b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/3100-3199/3135.Equalize%20Strings%20by%20Adding%20or%20Removing%20Characters%20at%20Ends/README.md) - + ## Description diff --git a/solution/3100-3199/3136.Valid Word/README.md b/solution/3100-3199/3136.Valid Word/README.md index 73513daa9a4db..5f149cd5cf644 100644 --- a/solution/3100-3199/3136.Valid Word/README.md +++ b/solution/3100-3199/3136.Valid Word/README.md @@ -2,7 +2,7 @@ [English Version](/solution/3100-3199/3136.Valid%20Word/README_EN.md) - + ## 题目描述 diff --git a/solution/3100-3199/3136.Valid Word/README_EN.md b/solution/3100-3199/3136.Valid Word/README_EN.md index 41ef4ab512afa..d05cfbcf6611d 100644 --- a/solution/3100-3199/3136.Valid Word/README_EN.md +++ b/solution/3100-3199/3136.Valid Word/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/3100-3199/3136.Valid%20Word/README.md) - + ## Description diff --git a/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README.md b/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README.md index c21fdd45e1cba..1bb2e60daf078 100644 --- a/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README.md +++ b/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README.md @@ -2,7 +2,7 @@ [English Version](/solution/3100-3199/3137.Minimum%20Number%20of%20Operations%20to%20Make%20Word%20K-Periodic/README_EN.md) - + ## 题目描述 diff --git a/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README_EN.md b/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README_EN.md index 3723f63685145..5d30d0d0b66df 100644 --- a/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README_EN.md +++ b/solution/3100-3199/3137.Minimum Number of Operations to Make Word K-Periodic/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/3100-3199/3137.Minimum%20Number%20of%20Operations%20to%20Make%20Word%20K-Periodic/README.md) - + ## Description diff --git a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md index 280f31c245581..f79dc1a58ecdd 100644 --- a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md +++ b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md @@ -2,7 +2,7 @@ [English Version](/solution/3100-3199/3138.Minimum%20Length%20of%20Anagram%20Concatenation/README_EN.md) - + ## 题目描述 diff --git a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md index acf72f3233988..74ae8d27ad889 100644 --- a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md +++ b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/3100-3199/3138.Minimum%20Length%20of%20Anagram%20Concatenation/README.md) - + ## Description diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md index 169211b50fbec..bf88b5e9676bf 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md @@ -2,7 +2,7 @@ [English Version](/solution/3100-3199/3139.Minimum%20Cost%20to%20Equalize%20Array/README_EN.md) - + ## 题目描述 diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md index c6bf4bb572da8..0460736ff9f35 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/3100-3199/3139.Minimum%20Cost%20to%20Equalize%20Array/README.md) - + ## Description diff --git a/solution/3100-3199/3140.Consecutive Available Seats II/README.md b/solution/3100-3199/3140.Consecutive Available Seats II/README.md index 7afa956075774..b36860194590e 100644 --- a/solution/3100-3199/3140.Consecutive Available Seats II/README.md +++ b/solution/3100-3199/3140.Consecutive Available Seats II/README.md @@ -2,7 +2,7 @@ [English Version](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) - + ## 题目描述 diff --git a/solution/3100-3199/3140.Consecutive Available Seats II/README_EN.md b/solution/3100-3199/3140.Consecutive Available Seats II/README_EN.md index 98ed2eb6a9d00..87dfaa0452efa 100644 --- a/solution/3100-3199/3140.Consecutive Available Seats II/README_EN.md +++ b/solution/3100-3199/3140.Consecutive Available Seats II/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) - + ## Description diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/README.md b/solution/3100-3199/3141.Maximum Hamming Distances/README.md new file mode 100644 index 0000000000000..fad89a0244cd1 --- /dev/null +++ b/solution/3100-3199/3141.Maximum Hamming Distances/README.md @@ -0,0 +1,91 @@ +# [3141. Maximum Hamming Distances 🔒](https://leetcode.cn/problems/maximum-hamming-distances) + +[English Version](/solution/3100-3199/3141.Maximum%20Hamming%20Distances/README_EN.md) + + + +## 题目描述 + + + +

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.

+ +

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:

+ +
+

Input: nums = [9,12,9,11], m = 4

+ +

Output: [2,3,2,3]

+ +

Explanation:

+ +

The binary representation of 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.
  • +
+
+ +

Example 2:

+ +
+

Input: nums = [3,4,6,10], m = 4

+ +

Output: [3,3,2,3]

+ +

Explanation:

+ +

The binary representation of 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.
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= m <= 17
  • +
  • 2 <= nums.length <= 2m
  • +
  • 0 <= nums[i] < 2m
  • +
+ +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md b/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md new file mode 100644 index 0000000000000..1c759d9a2d497 --- /dev/null +++ b/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md @@ -0,0 +1,89 @@ +# [3141. Maximum Hamming Distances 🔒](https://leetcode.com/problems/maximum-hamming-distances) + +[中文文档](/solution/3100-3199/3141.Maximum%20Hamming%20Distances/README.md) + + + +## Description + +

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.

+ +

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:

+ +
+

Input: nums = [9,12,9,11], m = 4

+ +

Output: [2,3,2,3]

+ +

Explanation:

+ +

The binary representation of 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.
  • +
+
+ +

Example 2:

+ +
+

Input: nums = [3,4,6,10], m = 4

+ +

Output: [3,3,2,3]

+ +

Explanation:

+ +

The binary representation of 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.
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= m <= 17
  • +
  • 2 <= nums.length <= 2m
  • +
  • 0 <= nums[i] < 2m
  • +
+ +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 218267d18bc3f..66752af2ddd25 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -277,7 +277,7 @@ | 3118 | [Friday Purchase III](/solution/3100-3199/3118.Friday%20Purchase%20III/README.md) | `数据库` | 中等 | 🔒 | | 3124 | [查找最长的电话](/solution/3100-3199/3124.Find%20Longest%20Calls/README.md) | `数据库` | 中等 | 🔒 | | 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README.md) | `数据库` | 中等 | 🔒 | -| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | | 中等 | 🔒 | +| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | `数据库` | 中等 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index fc602ee8d9055..533d4c885deab 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -275,7 +275,7 @@ Press Control + F(or Command + F on | 3118 | [Friday Purchase III](/solution/3100-3199/3118.Friday%20Purchase%20III/README_EN.md) | `Database` | Medium | 🔒 | | 3124 | [Find Longest Calls](/solution/3100-3199/3124.Find%20Longest%20Calls/README_EN.md) | `Database` | Medium | 🔒 | | 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README_EN.md) | `Database` | Medium | 🔒 | -| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | | Medium | 🔒 | +| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | `Database` | Medium | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index 13ac6c865beba..4bd803d68e46a 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3145,12 +3145,13 @@ | 3132 | [找出与数组相加的整数 II](/solution/3100-3199/3132.Find%20the%20Integer%20Added%20to%20Array%20II/README.md) | `数组`,`双指针`,`枚举`,`排序` | 中等 | 第 395 场周赛 | | 3133 | [数组最后一个元素的最小值](/solution/3100-3199/3133.Minimum%20Array%20End/README.md) | `位运算` | 中等 | 第 395 场周赛 | | 3134 | [找出唯一性数组的中位数](/solution/3100-3199/3134.Find%20the%20Median%20of%20the%20Uniqueness%20Array/README.md) | `数组`,`哈希表`,`二分查找`,`滑动窗口` | 困难 | 第 395 场周赛 | -| 3135 | [Equalize Strings by Adding or Removing Characters at Ends](/solution/3100-3199/3135.Equalize%20Strings%20by%20Adding%20or%20Removing%20Characters%20at%20Ends/README.md) | | 中等 | 🔒 | -| 3136 | [有效单词](/solution/3100-3199/3136.Valid%20Word/README.md) | | 简单 | 第 396 场周赛 | -| 3137 | [K 周期字符串需要的最少操作次数](/solution/3100-3199/3137.Minimum%20Number%20of%20Operations%20to%20Make%20Word%20K-Periodic/README.md) | | 中等 | 第 396 场周赛 | -| 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) | | 中等 | 🔒 | +| 3135 | [Equalize Strings by Adding or Removing Characters at Ends](/solution/3100-3199/3135.Equalize%20Strings%20by%20Adding%20or%20Removing%20Characters%20at%20Ends/README.md) | `字符串`,`二分查找`,`动态规划`,`滑动窗口`,`哈希函数` | 中等 | 🔒 | +| 3136 | [有效单词](/solution/3100-3199/3136.Valid%20Word/README.md) | `字符串` | 简单 | 第 396 场周赛 | +| 3137 | [K 周期字符串需要的最少操作次数](/solution/3100-3199/3137.Minimum%20Number%20of%20Operations%20to%20Make%20Word%20K-Periodic/README.md) | `哈希表`,`字符串`,`计数` | 中等 | 第 396 场周赛 | +| 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) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 7df10135bd88a..c0128d10d54c3 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3143,12 +3143,13 @@ Press Control + F(or Command + F on | 3132 | [Find the Integer Added to Array II](/solution/3100-3199/3132.Find%20the%20Integer%20Added%20to%20Array%20II/README_EN.md) | `Array`,`Two Pointers`,`Enumeration`,`Sorting` | Medium | Weekly Contest 395 | | 3133 | [Minimum Array End](/solution/3100-3199/3133.Minimum%20Array%20End/README_EN.md) | `Bit Manipulation` | Medium | Weekly Contest 395 | | 3134 | [Find the Median of the Uniqueness Array](/solution/3100-3199/3134.Find%20the%20Median%20of%20the%20Uniqueness%20Array/README_EN.md) | `Array`,`Hash Table`,`Binary Search`,`Sliding Window` | Hard | Weekly Contest 395 | -| 3135 | [Equalize Strings by Adding or Removing Characters at Ends](/solution/3100-3199/3135.Equalize%20Strings%20by%20Adding%20or%20Removing%20Characters%20at%20Ends/README_EN.md) | | Medium | 🔒 | -| 3136 | [Valid Word](/solution/3100-3199/3136.Valid%20Word/README_EN.md) | | Easy | Weekly Contest 396 | -| 3137 | [Minimum Number of Operations to Make Word K-Periodic](/solution/3100-3199/3137.Minimum%20Number%20of%20Operations%20to%20Make%20Word%20K-Periodic/README_EN.md) | | Medium | Weekly Contest 396 | -| 3138 | [Minimum Length of Anagram Concatenation](/solution/3100-3199/3138.Minimum%20Length%20of%20Anagram%20Concatenation/README_EN.md) | | Medium | Weekly Contest 396 | -| 3139 | [Minimum Cost to Equalize Array](/solution/3100-3199/3139.Minimum%20Cost%20to%20Equalize%20Array/README_EN.md) | | Hard | Weekly Contest 396 | -| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | | Medium | 🔒 | +| 3135 | [Equalize Strings by Adding or Removing Characters at Ends](/solution/3100-3199/3135.Equalize%20Strings%20by%20Adding%20or%20Removing%20Characters%20at%20Ends/README_EN.md) | `String`,`Binary Search`,`Dynamic Programming`,`Sliding Window`,`Hash Function` | Medium | 🔒 | +| 3136 | [Valid Word](/solution/3100-3199/3136.Valid%20Word/README_EN.md) | `String` | Easy | Weekly Contest 396 | +| 3137 | [Minimum Number of Operations to Make Word K-Periodic](/solution/3100-3199/3137.Minimum%20Number%20of%20Operations%20to%20Make%20Word%20K-Periodic/README_EN.md) | `Hash Table`,`String`,`Counting` | Medium | Weekly Contest 396 | +| 3138 | [Minimum Length of Anagram Concatenation](/solution/3100-3199/3138.Minimum%20Length%20of%20Anagram%20Concatenation/README_EN.md) | `Hash Table`,`String`,`Counting` | Medium | Weekly Contest 396 | +| 3139 | [Minimum Cost to Equalize Array](/solution/3100-3199/3139.Minimum%20Cost%20to%20Equalize%20Array/README_EN.md) | `Greedy`,`Array`,`Enumeration` | Hard | Weekly Contest 396 | +| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | `Database` | Medium | 🔒 | +| 3141 | [Maximum Hamming Distances](/solution/3100-3199/3141.Maximum%20Hamming%20Distances/README_EN.md) | | Hard | 🔒 | ## Copyright