From 93d742172bf0a56d3127fc19b0472216d24c53b3 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 25 Dec 2023 20:00:19 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2975 No.2975.Maximum Square Area by Removing Fences From a Field --- .../README.md | 129 +++++++++++++++++- .../README_EN.md | 129 +++++++++++++++++- .../Solution.cpp | 27 ++++ .../Solution.go | 25 ++++ .../Solution.java | 28 ++++ .../Solution.py | 14 ++ .../Solution.ts | 22 +++ 7 files changed, 368 insertions(+), 6 deletions(-) create mode 100644 solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.cpp create mode 100644 solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.go create mode 100644 solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.java create mode 100644 solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.py create mode 100644 solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.ts diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README.md b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README.md index e96298a7c7a41..061c759dad88b 100644 --- a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README.md +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README.md @@ -54,6 +54,12 @@ +**方法一:枚举** + +我们可以枚举 $hFences$ 中的任意两条水平栅栏 $a$ 和 $b$,计算 $a$ 和 $b$ 之间的距离 $d$,记录在哈希表 $hs$ 中,然后枚举 $vFences$ 中的任意两条垂直栅栏 $c$ 和 $d$,计算 $c$ 和 $d$ 之间的距离 $d$,记录在哈希表 $vs$ 中,最后遍历哈希表 $hs$,如果 $hs$ 中的某个距离 $d$ 在哈希表 $vs$ 中也存在,那么说明存在一个正方形田地,其边长为 $d$,面积为 $d^2$,我们只需要取最大的 $d$,求 $d^2 \bmod 10^9 + 7$ 即可。 + +时间复杂度 $O(h^2 + v^2)$,空间复杂度 $O(h^2 + v^2)$。其中 $h$ 和 $v$ 分别是 $hFences$ 和 $vFences$ 的长度。 + ### **Python3** @@ -61,7 +67,20 @@ ```python - +class Solution: + def maximizeSquareArea( + self, m: int, n: int, hFences: List[int], vFences: List[int] + ) -> int: + def f(nums: List[int], k: int) -> Set[int]: + nums.extend([1, k]) + nums.sort() + return {b - a for a, b in combinations(nums, 2)} + + mod = 10**9 + 7 + hs = f(hFences, m) + vs = f(vFences, n) + ans = max(hs & vs, default=0) + return ans**2 % mod if ans else -1 ``` ### **Java** @@ -69,19 +88,123 @@ ```java - +class Solution { + public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) { + Set hs = f(hFences, m); + Set vs = f(vFences, n); + hs.retainAll(vs); + int ans = -1; + final int mod = (int) 1e9 + 7; + for (int x : hs) { + ans = Math.max(ans, x); + } + return ans > 0 ? (int) (1L * ans * ans % mod) : -1; + } + + private Set f(int[] nums, int k) { + int n = nums.length; + nums = Arrays.copyOf(nums, n + 2); + nums[n] = 1; + nums[n + 1] = k; + Arrays.sort(nums); + Set s = new HashSet<>(); + for (int i = 0; i < nums.length; ++i) { + for (int j = 0; j < i; ++j) { + s.add(nums[i] - nums[j]); + } + } + return s; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int maximizeSquareArea(int m, int n, vector& hFences, vector& vFences) { + auto f = [](vector& nums, int k) { + nums.push_back(k); + nums.push_back(1); + sort(nums.begin(), nums.end()); + unordered_set s; + for (int i = 0; i < nums.size(); ++i) { + for (int j = 0; j < i; ++j) { + s.insert(nums[i] - nums[j]); + } + } + return s; + }; + auto hs = f(hFences, m); + auto vs = f(vFences, n); + int ans = 0; + for (int h : hs) { + if (vs.count(h)) { + ans = max(ans, h); + } + } + const int mod = 1e9 + 7; + return ans > 0 ? 1LL * ans * ans % mod : -1; + } +}; ``` ### **Go** ```go +func maximizeSquareArea(m int, n int, hFences []int, vFences []int) int { + f := func(nums []int, k int) map[int]bool { + nums = append(nums, 1, k) + sort.Ints(nums) + s := map[int]bool{} + for i := 0; i < len(nums); i++ { + for j := 0; j < i; j++ { + s[nums[i]-nums[j]] = true + } + } + return s + } + hs := f(hFences, m) + vs := f(vFences, n) + ans := 0 + for h := range hs { + if vs[h] { + ans = max(ans, h) + } + } + if ans > 0 { + return ans * ans % (1e9 + 7) + } + return -1 +} +``` +### **TypeScript** + +```ts +function maximizeSquareArea(m: number, n: number, hFences: number[], vFences: number[]): number { + const f = (nums: number[], k: number): Set => { + nums.push(1, k); + nums.sort((a, b) => a - b); + const s: Set = new Set(); + for (let i = 0; i < nums.length; ++i) { + for (let j = 0; j < i; ++j) { + s.add(nums[i] - nums[j]); + } + } + return s; + }; + const hs = f(hFences, m); + const vs = f(vFences, n); + let ans = 0; + for (const h of hs) { + if (vs.has(h)) { + ans = Math.max(ans, h); + } + } + return ans ? Number(BigInt(ans) ** 2n % 1000000007n) : -1; +} ``` ### **...** diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README_EN.md b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README_EN.md index e7b8403134b6c..559b6ad0ec2dd 100644 --- a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README_EN.md +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README_EN.md @@ -48,30 +48,153 @@ ## Solutions +**Solution 1: Enumeration** + +We can enumerate any two horizontal fences $a$ and $b$ in $hFences$, calculate the distance $d$ between $a$ and $b$, and record it in the hash table $hs$. Then, we enumerate any two vertical fences $c$ and $d$ in $vFences$, calculate the distance $d$ between $c$ and $d$, and record it in the hash table $vs$. Finally, we traverse the hash table $hs$. If a distance $d$ in $hs$ also exists in the hash table $vs$, it means that there exists a square field with a side length of $d$ and an area of $d^2$. We just need to take the largest $d$ and calculate $d^2 \bmod 10^9 + 7$. + +The time complexity is $O(h^2 + v^2)$, and the space complexity is $O(h^2 + v^2)$. Where $h$ and $v$ are the lengths of $hFences$ and $vFences$ respectively. + ### **Python3** ```python - +class Solution: + def maximizeSquareArea( + self, m: int, n: int, hFences: List[int], vFences: List[int] + ) -> int: + def f(nums: List[int], k: int) -> Set[int]: + nums.extend([1, k]) + nums.sort() + return {b - a for a, b in combinations(nums, 2)} + + mod = 10**9 + 7 + hs = f(hFences, m) + vs = f(vFences, n) + ans = max(hs & vs, default=0) + return ans**2 % mod if ans else -1 ``` ### **Java** ```java - +class Solution { + public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) { + Set hs = f(hFences, m); + Set vs = f(vFences, n); + hs.retainAll(vs); + int ans = -1; + final int mod = (int) 1e9 + 7; + for (int x : hs) { + ans = Math.max(ans, x); + } + return ans > 0 ? (int) (1L * ans * ans % mod) : -1; + } + + private Set f(int[] nums, int k) { + int n = nums.length; + nums = Arrays.copyOf(nums, n + 2); + nums[n] = 1; + nums[n + 1] = k; + Arrays.sort(nums); + Set s = new HashSet<>(); + for (int i = 0; i < nums.length; ++i) { + for (int j = 0; j < i; ++j) { + s.add(nums[i] - nums[j]); + } + } + return s; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int maximizeSquareArea(int m, int n, vector& hFences, vector& vFences) { + auto f = [](vector& nums, int k) { + nums.push_back(k); + nums.push_back(1); + sort(nums.begin(), nums.end()); + unordered_set s; + for (int i = 0; i < nums.size(); ++i) { + for (int j = 0; j < i; ++j) { + s.insert(nums[i] - nums[j]); + } + } + return s; + }; + auto hs = f(hFences, m); + auto vs = f(vFences, n); + int ans = 0; + for (int h : hs) { + if (vs.count(h)) { + ans = max(ans, h); + } + } + const int mod = 1e9 + 7; + return ans > 0 ? 1LL * ans * ans % mod : -1; + } +}; ``` ### **Go** ```go +func maximizeSquareArea(m int, n int, hFences []int, vFences []int) int { + f := func(nums []int, k int) map[int]bool { + nums = append(nums, 1, k) + sort.Ints(nums) + s := map[int]bool{} + for i := 0; i < len(nums); i++ { + for j := 0; j < i; j++ { + s[nums[i]-nums[j]] = true + } + } + return s + } + hs := f(hFences, m) + vs := f(vFences, n) + ans := 0 + for h := range hs { + if vs[h] { + ans = max(ans, h) + } + } + if ans > 0 { + return ans * ans % (1e9 + 7) + } + return -1 +} +``` +### **TypeScript** + +```ts +function maximizeSquareArea(m: number, n: number, hFences: number[], vFences: number[]): number { + const f = (nums: number[], k: number): Set => { + nums.push(1, k); + nums.sort((a, b) => a - b); + const s: Set = new Set(); + for (let i = 0; i < nums.length; ++i) { + for (let j = 0; j < i; ++j) { + s.add(nums[i] - nums[j]); + } + } + return s; + }; + const hs = f(hFences, m); + const vs = f(vFences, n); + let ans = 0; + for (const h of hs) { + if (vs.has(h)) { + ans = Math.max(ans, h); + } + } + return ans ? Number(BigInt(ans) ** 2n % 1000000007n) : -1; +} ``` ### **...** diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.cpp b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.cpp new file mode 100644 index 0000000000000..d017451070b01 --- /dev/null +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int maximizeSquareArea(int m, int n, vector& hFences, vector& vFences) { + auto f = [](vector& nums, int k) { + nums.push_back(k); + nums.push_back(1); + sort(nums.begin(), nums.end()); + unordered_set s; + for (int i = 0; i < nums.size(); ++i) { + for (int j = 0; j < i; ++j) { + s.insert(nums[i] - nums[j]); + } + } + return s; + }; + auto hs = f(hFences, m); + auto vs = f(vFences, n); + int ans = 0; + for (int h : hs) { + if (vs.count(h)) { + ans = max(ans, h); + } + } + const int mod = 1e9 + 7; + return ans > 0 ? 1LL * ans * ans % mod : -1; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.go b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.go new file mode 100644 index 0000000000000..e846acbca8034 --- /dev/null +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.go @@ -0,0 +1,25 @@ +func maximizeSquareArea(m int, n int, hFences []int, vFences []int) int { + f := func(nums []int, k int) map[int]bool { + nums = append(nums, 1, k) + sort.Ints(nums) + s := map[int]bool{} + for i := 0; i < len(nums); i++ { + for j := 0; j < i; j++ { + s[nums[i]-nums[j]] = true + } + } + return s + } + hs := f(hFences, m) + vs := f(vFences, n) + ans := 0 + for h := range hs { + if vs[h] { + ans = max(ans, h) + } + } + if ans > 0 { + return ans * ans % (1e9 + 7) + } + return -1 +} \ No newline at end of file diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.java b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.java new file mode 100644 index 0000000000000..e651a9dd354c5 --- /dev/null +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.java @@ -0,0 +1,28 @@ +class Solution { + public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) { + Set hs = f(hFences, m); + Set vs = f(vFences, n); + hs.retainAll(vs); + int ans = -1; + final int mod = (int) 1e9 + 7; + for (int x : hs) { + ans = Math.max(ans, x); + } + return ans > 0 ? (int) (1L * ans * ans % mod) : -1; + } + + private Set f(int[] nums, int k) { + int n = nums.length; + nums = Arrays.copyOf(nums, n + 2); + nums[n] = 1; + nums[n + 1] = k; + Arrays.sort(nums); + Set s = new HashSet<>(); + for (int i = 0; i < nums.length; ++i) { + for (int j = 0; j < i; ++j) { + s.add(nums[i] - nums[j]); + } + } + return s; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.py b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.py new file mode 100644 index 0000000000000..7d7a935c91681 --- /dev/null +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def maximizeSquareArea( + self, m: int, n: int, hFences: List[int], vFences: List[int] + ) -> int: + def f(nums: List[int], k: int) -> Set[int]: + nums.extend([1, k]) + nums.sort() + return {b - a for a, b in combinations(nums, 2)} + + mod = 10**9 + 7 + hs = f(hFences, m) + vs = f(vFences, n) + ans = max(hs & vs, default=0) + return ans**2 % mod if ans else -1 diff --git a/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.ts b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.ts new file mode 100644 index 0000000000000..85cc62bf43dbc --- /dev/null +++ b/solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/Solution.ts @@ -0,0 +1,22 @@ +function maximizeSquareArea(m: number, n: number, hFences: number[], vFences: number[]): number { + const f = (nums: number[], k: number): Set => { + nums.push(1, k); + nums.sort((a, b) => a - b); + const s: Set = new Set(); + for (let i = 0; i < nums.length; ++i) { + for (let j = 0; j < i; ++j) { + s.add(nums[i] - nums[j]); + } + } + return s; + }; + const hs = f(hFences, m); + const vs = f(vFences, n); + let ans = 0; + for (const h of hs) { + if (vs.has(h)) { + ans = Math.max(ans, h); + } + } + return ans ? Number(BigInt(ans) ** 2n % 1000000007n) : -1; +}