From b5fb3fe8f520061ae5c4672c0af8f5304a4259cf Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 21 May 2024 17:49:06 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2831 No.2831.Find the Longest Equal Subarray --- .github/workflows/deploy.yml | 3 - .../README.md | 130 ++++++++++++++++++ .../README_EN.md | 130 ++++++++++++++++++ .../Solution2.cpp | 21 +++ .../Solution2.go | 16 +++ .../Solution2.java | 21 +++ .../Solution2.py | 13 ++ .../Solution2.ts | 18 +++ 8 files changed, 349 insertions(+), 3 deletions(-) create mode 100644 solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.cpp create mode 100644 solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.go create mode 100644 solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.java create mode 100644 solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.py create mode 100644 solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.ts diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 048f35f94c4d9..96213bc3f790d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -4,10 +4,7 @@ on: push: branches: - main - - docs paths: - - package.json - - requirements.txt - solution/** - lcs/** - lcp/** diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/README.md b/solution/2800-2899/2831.Find the Longest Equal Subarray/README.md index 5475d42d547ec..ce48046e0610f 100644 --- a/solution/2800-2899/2831.Find the Longest Equal Subarray/README.md +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/README.md @@ -179,4 +179,134 @@ function longestEqualSubarray(nums: number[], k: number): number { + + +### 方法二:哈希表 + 双指针(写法二) + +我们可以用一个哈希表 $g$ 维护每个元素的下标列表。 + +接下来,我们枚举每个元素作为等值元素,我们从哈希表 $g$ 中取出这个元素的下标列表 $ids$,然后我们定义两个指针 $l$ 和 $r$,用于维护一个窗口,使得窗口内的元素个数减去等值元素的个数,结果不超过 $k$。那么我们只需要求出最大的满足条件的窗口即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 + + + +#### Python3 + +```python +class Solution: + def longestEqualSubarray(self, nums: List[int], k: int) -> int: + g = defaultdict(list) + for i, x in enumerate(nums): + g[x].append(i) + ans = 0 + for ids in g.values(): + l = 0 + for r in range(len(ids)): + while ids[r] - ids[l] - (r - l) > k: + l += 1 + ans = max(ans, r - l + 1) + return ans +``` + +#### Java + +```java +class Solution { + public int longestEqualSubarray(List nums, int k) { + int n = nums.size(); + List[] g = new List[n + 1]; + Arrays.setAll(g, i -> new ArrayList<>()); + for (int i = 0; i < n; ++i) { + g[nums.get(i)].add(i); + } + int ans = 0; + for (List ids : g) { + int l = 0; + for (int r = 0; r < ids.size(); ++r) { + while (ids.get(r) - ids.get(l) - (r - l) > k) { + ++l; + } + ans = Math.max(ans, r - l + 1); + } + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int longestEqualSubarray(vector& nums, int k) { + int n = nums.size(); + vector g[n + 1]; + for (int i = 0; i < n; ++i) { + g[nums[i]].push_back(i); + } + int ans = 0; + for (const auto& ids : g) { + int l = 0; + for (int r = 0; r < ids.size(); ++r) { + while (ids[r] - ids[l] - (r - l) > k) { + ++l; + } + ans = max(ans, r - l + 1); + } + } + return ans; + } +}; +``` + +#### Go + +```go +func longestEqualSubarray(nums []int, k int) (ans int) { + g := make([][]int, len(nums)+1) + for i, x := range nums { + g[x] = append(g[x], i) + } + for _, ids := range g { + l := 0 + for r := range ids { + for ids[r]-ids[l]-(r-l) > k { + l++ + } + ans = max(ans, r-l+1) + } + } + return +} +``` + +#### TypeScript + +```ts +function longestEqualSubarray(nums: number[], k: number): number { + const n = nums.length; + const g: number[][] = Array.from({ length: n + 1 }, () => []); + for (let i = 0; i < n; ++i) { + g[nums[i]].push(i); + } + let ans = 0; + for (const ids of g) { + let l = 0; + for (let r = 0; r < ids.length; ++r) { + while (ids[r] - ids[l] - (r - l) > k) { + ++l; + } + ans = Math.max(ans, r - l + 1); + } + } + return ans; +} +``` + + + + + diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/README_EN.md b/solution/2800-2899/2831.Find the Longest Equal Subarray/README_EN.md index 63efd02282c4f..b2b7a4349b160 100644 --- a/solution/2800-2899/2831.Find the Longest Equal Subarray/README_EN.md +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/README_EN.md @@ -177,4 +177,134 @@ function longestEqualSubarray(nums: number[], k: number): number { + + +### Solution 2: Hash Table + Two Pointers (Method 2) + +We can use a hash table $g$ to maintain the index list of each element. + +Next, we enumerate each element as the equal value element. We take out the index list $ids$ of this element from the hash table $g$. Then we define two pointers $l$ and $r$ to maintain a window, so that the number of elements in the window minus the number of equal value elements does not exceed $k$. Therefore, we only need to find the largest window that meets the condition. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array. + + + +#### Python3 + +```python +class Solution: + def longestEqualSubarray(self, nums: List[int], k: int) -> int: + g = defaultdict(list) + for i, x in enumerate(nums): + g[x].append(i) + ans = 0 + for ids in g.values(): + l = 0 + for r in range(len(ids)): + while ids[r] - ids[l] - (r - l) > k: + l += 1 + ans = max(ans, r - l + 1) + return ans +``` + +#### Java + +```java +class Solution { + public int longestEqualSubarray(List nums, int k) { + int n = nums.size(); + List[] g = new List[n + 1]; + Arrays.setAll(g, i -> new ArrayList<>()); + for (int i = 0; i < n; ++i) { + g[nums.get(i)].add(i); + } + int ans = 0; + for (List ids : g) { + int l = 0; + for (int r = 0; r < ids.size(); ++r) { + while (ids.get(r) - ids.get(l) - (r - l) > k) { + ++l; + } + ans = Math.max(ans, r - l + 1); + } + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int longestEqualSubarray(vector& nums, int k) { + int n = nums.size(); + vector g[n + 1]; + for (int i = 0; i < n; ++i) { + g[nums[i]].push_back(i); + } + int ans = 0; + for (const auto& ids : g) { + int l = 0; + for (int r = 0; r < ids.size(); ++r) { + while (ids[r] - ids[l] - (r - l) > k) { + ++l; + } + ans = max(ans, r - l + 1); + } + } + return ans; + } +}; +``` + +#### Go + +```go +func longestEqualSubarray(nums []int, k int) (ans int) { + g := make([][]int, len(nums)+1) + for i, x := range nums { + g[x] = append(g[x], i) + } + for _, ids := range g { + l := 0 + for r := range ids { + for ids[r]-ids[l]-(r-l) > k { + l++ + } + ans = max(ans, r-l+1) + } + } + return +} +``` + +#### TypeScript + +```ts +function longestEqualSubarray(nums: number[], k: number): number { + const n = nums.length; + const g: number[][] = Array.from({ length: n + 1 }, () => []); + for (let i = 0; i < n; ++i) { + g[nums[i]].push(i); + } + let ans = 0; + for (const ids of g) { + let l = 0; + for (let r = 0; r < ids.length; ++r) { + while (ids[r] - ids[l] - (r - l) > k) { + ++l; + } + ans = Math.max(ans, r - l + 1); + } + } + return ans; +} +``` + + + + + diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.cpp b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.cpp new file mode 100644 index 0000000000000..cc6c2fefd2128 --- /dev/null +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestEqualSubarray(vector& nums, int k) { + int n = nums.size(); + vector g[n + 1]; + for (int i = 0; i < n; ++i) { + g[nums[i]].push_back(i); + } + int ans = 0; + for (const auto& ids : g) { + int l = 0; + for (int r = 0; r < ids.size(); ++r) { + while (ids[r] - ids[l] - (r - l) > k) { + ++l; + } + ans = max(ans, r - l + 1); + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.go b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.go new file mode 100644 index 0000000000000..24ae8cc49d3ac --- /dev/null +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.go @@ -0,0 +1,16 @@ +func longestEqualSubarray(nums []int, k int) (ans int) { + g := make([][]int, len(nums)+1) + for i, x := range nums { + g[x] = append(g[x], i) + } + for _, ids := range g { + l := 0 + for r := range ids { + for ids[r]-ids[l]-(r-l) > k { + l++ + } + ans = max(ans, r-l+1) + } + } + return +} \ No newline at end of file diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.java b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.java new file mode 100644 index 0000000000000..3e1109d62ccae --- /dev/null +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.java @@ -0,0 +1,21 @@ +class Solution { + public int longestEqualSubarray(List nums, int k) { + int n = nums.size(); + List[] g = new List[n + 1]; + Arrays.setAll(g, i -> new ArrayList<>()); + for (int i = 0; i < n; ++i) { + g[nums.get(i)].add(i); + } + int ans = 0; + for (List ids : g) { + int l = 0; + for (int r = 0; r < ids.size(); ++r) { + while (ids.get(r) - ids.get(l) - (r - l) > k) { + ++l; + } + ans = Math.max(ans, r - l + 1); + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.py b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.py new file mode 100644 index 0000000000000..1a53f9027e776 --- /dev/null +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.py @@ -0,0 +1,13 @@ +class Solution: + def longestEqualSubarray(self, nums: List[int], k: int) -> int: + g = defaultdict(list) + for i, x in enumerate(nums): + g[x].append(i) + ans = 0 + for ids in g.values(): + l = 0 + for r in range(len(ids)): + while ids[r] - ids[l] - (r - l) > k: + l += 1 + ans = max(ans, r - l + 1) + return ans diff --git a/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.ts b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.ts new file mode 100644 index 0000000000000..d62cdcef1a59c --- /dev/null +++ b/solution/2800-2899/2831.Find the Longest Equal Subarray/Solution2.ts @@ -0,0 +1,18 @@ +function longestEqualSubarray(nums: number[], k: number): number { + const n = nums.length; + const g: number[][] = Array.from({ length: n + 1 }, () => []); + for (let i = 0; i < n; ++i) { + g[nums[i]].push(i); + } + let ans = 0; + for (const ids of g) { + let l = 0; + for (let r = 0; r < ids.length; ++r) { + while (ids[r] - ids[l] - (r - l) > k) { + ++l; + } + ans = Math.max(ans, r - l + 1); + } + } + return ans; +}