Skip to content

Commit 6e93db1

Browse files
authored
feat: add solutions to lc problem: No.2975 (doocs#2154)
No.2975.Maximum Square Area by Removing Fences From a Field
1 parent fccb469 commit 6e93db1

File tree

7 files changed

+368
-6
lines changed

7 files changed

+368
-6
lines changed

solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README.md

+126-3
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,157 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:枚举**
58+
59+
我们可以枚举 $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$ 即可。
60+
61+
时间复杂度 $O(h^2 + v^2)$,空间复杂度 $O(h^2 + v^2)$。其中 $h$ 和 $v$ 分别是 $hFences$ 和 $vFences$ 的长度。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
6066

6167
<!-- 这里可写当前语言的特殊实现逻辑 -->
6268

6369
```python
64-
70+
class Solution:
71+
def maximizeSquareArea(
72+
self, m: int, n: int, hFences: List[int], vFences: List[int]
73+
) -> int:
74+
def f(nums: List[int], k: int) -> Set[int]:
75+
nums.extend([1, k])
76+
nums.sort()
77+
return {b - a for a, b in combinations(nums, 2)}
78+
79+
mod = 10**9 + 7
80+
hs = f(hFences, m)
81+
vs = f(vFences, n)
82+
ans = max(hs & vs, default=0)
83+
return ans**2 % mod if ans else -1
6584
```
6685

6786
### **Java**
6887

6988
<!-- 这里可写当前语言的特殊实现逻辑 -->
7089

7190
```java
72-
91+
class Solution {
92+
public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) {
93+
Set<Integer> hs = f(hFences, m);
94+
Set<Integer> vs = f(vFences, n);
95+
hs.retainAll(vs);
96+
int ans = -1;
97+
final int mod = (int) 1e9 + 7;
98+
for (int x : hs) {
99+
ans = Math.max(ans, x);
100+
}
101+
return ans > 0 ? (int) (1L * ans * ans % mod) : -1;
102+
}
103+
104+
private Set<Integer> f(int[] nums, int k) {
105+
int n = nums.length;
106+
nums = Arrays.copyOf(nums, n + 2);
107+
nums[n] = 1;
108+
nums[n + 1] = k;
109+
Arrays.sort(nums);
110+
Set<Integer> s = new HashSet<>();
111+
for (int i = 0; i < nums.length; ++i) {
112+
for (int j = 0; j < i; ++j) {
113+
s.add(nums[i] - nums[j]);
114+
}
115+
}
116+
return s;
117+
}
118+
}
73119
```
74120

75121
### **C++**
76122

77123
```cpp
78-
124+
class Solution {
125+
public:
126+
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
127+
auto f = [](vector<int>& nums, int k) {
128+
nums.push_back(k);
129+
nums.push_back(1);
130+
sort(nums.begin(), nums.end());
131+
unordered_set<int> s;
132+
for (int i = 0; i < nums.size(); ++i) {
133+
for (int j = 0; j < i; ++j) {
134+
s.insert(nums[i] - nums[j]);
135+
}
136+
}
137+
return s;
138+
};
139+
auto hs = f(hFences, m);
140+
auto vs = f(vFences, n);
141+
int ans = 0;
142+
for (int h : hs) {
143+
if (vs.count(h)) {
144+
ans = max(ans, h);
145+
}
146+
}
147+
const int mod = 1e9 + 7;
148+
return ans > 0 ? 1LL * ans * ans % mod : -1;
149+
}
150+
};
79151
```
80152
81153
### **Go**
82154
83155
```go
156+
func maximizeSquareArea(m int, n int, hFences []int, vFences []int) int {
157+
f := func(nums []int, k int) map[int]bool {
158+
nums = append(nums, 1, k)
159+
sort.Ints(nums)
160+
s := map[int]bool{}
161+
for i := 0; i < len(nums); i++ {
162+
for j := 0; j < i; j++ {
163+
s[nums[i]-nums[j]] = true
164+
}
165+
}
166+
return s
167+
}
168+
hs := f(hFences, m)
169+
vs := f(vFences, n)
170+
ans := 0
171+
for h := range hs {
172+
if vs[h] {
173+
ans = max(ans, h)
174+
}
175+
}
176+
if ans > 0 {
177+
return ans * ans % (1e9 + 7)
178+
}
179+
return -1
180+
}
181+
```
84182

183+
### **TypeScript**
184+
185+
```ts
186+
function maximizeSquareArea(m: number, n: number, hFences: number[], vFences: number[]): number {
187+
const f = (nums: number[], k: number): Set<number> => {
188+
nums.push(1, k);
189+
nums.sort((a, b) => a - b);
190+
const s: Set<number> = new Set();
191+
for (let i = 0; i < nums.length; ++i) {
192+
for (let j = 0; j < i; ++j) {
193+
s.add(nums[i] - nums[j]);
194+
}
195+
}
196+
return s;
197+
};
198+
const hs = f(hFences, m);
199+
const vs = f(vFences, n);
200+
let ans = 0;
201+
for (const h of hs) {
202+
if (vs.has(h)) {
203+
ans = Math.max(ans, h);
204+
}
205+
}
206+
return ans ? Number(BigInt(ans) ** 2n % 1000000007n) : -1;
207+
}
85208
```
86209

87210
### **...**

solution/2900-2999/2975.Maximum Square Area by Removing Fences From a Field/README_EN.md

+126-3
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,153 @@
4848

4949
## Solutions
5050

51+
**Solution 1: Enumeration**
52+
53+
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$.
54+
55+
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.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

5561
```python
56-
62+
class Solution:
63+
def maximizeSquareArea(
64+
self, m: int, n: int, hFences: List[int], vFences: List[int]
65+
) -> int:
66+
def f(nums: List[int], k: int) -> Set[int]:
67+
nums.extend([1, k])
68+
nums.sort()
69+
return {b - a for a, b in combinations(nums, 2)}
70+
71+
mod = 10**9 + 7
72+
hs = f(hFences, m)
73+
vs = f(vFences, n)
74+
ans = max(hs & vs, default=0)
75+
return ans**2 % mod if ans else -1
5776
```
5877

5978
### **Java**
6079

6180
```java
62-
81+
class Solution {
82+
public int maximizeSquareArea(int m, int n, int[] hFences, int[] vFences) {
83+
Set<Integer> hs = f(hFences, m);
84+
Set<Integer> vs = f(vFences, n);
85+
hs.retainAll(vs);
86+
int ans = -1;
87+
final int mod = (int) 1e9 + 7;
88+
for (int x : hs) {
89+
ans = Math.max(ans, x);
90+
}
91+
return ans > 0 ? (int) (1L * ans * ans % mod) : -1;
92+
}
93+
94+
private Set<Integer> f(int[] nums, int k) {
95+
int n = nums.length;
96+
nums = Arrays.copyOf(nums, n + 2);
97+
nums[n] = 1;
98+
nums[n + 1] = k;
99+
Arrays.sort(nums);
100+
Set<Integer> s = new HashSet<>();
101+
for (int i = 0; i < nums.length; ++i) {
102+
for (int j = 0; j < i; ++j) {
103+
s.add(nums[i] - nums[j]);
104+
}
105+
}
106+
return s;
107+
}
108+
}
63109
```
64110

65111
### **C++**
66112

67113
```cpp
68-
114+
class Solution {
115+
public:
116+
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
117+
auto f = [](vector<int>& nums, int k) {
118+
nums.push_back(k);
119+
nums.push_back(1);
120+
sort(nums.begin(), nums.end());
121+
unordered_set<int> s;
122+
for (int i = 0; i < nums.size(); ++i) {
123+
for (int j = 0; j < i; ++j) {
124+
s.insert(nums[i] - nums[j]);
125+
}
126+
}
127+
return s;
128+
};
129+
auto hs = f(hFences, m);
130+
auto vs = f(vFences, n);
131+
int ans = 0;
132+
for (int h : hs) {
133+
if (vs.count(h)) {
134+
ans = max(ans, h);
135+
}
136+
}
137+
const int mod = 1e9 + 7;
138+
return ans > 0 ? 1LL * ans * ans % mod : -1;
139+
}
140+
};
69141
```
70142
71143
### **Go**
72144
73145
```go
146+
func maximizeSquareArea(m int, n int, hFences []int, vFences []int) int {
147+
f := func(nums []int, k int) map[int]bool {
148+
nums = append(nums, 1, k)
149+
sort.Ints(nums)
150+
s := map[int]bool{}
151+
for i := 0; i < len(nums); i++ {
152+
for j := 0; j < i; j++ {
153+
s[nums[i]-nums[j]] = true
154+
}
155+
}
156+
return s
157+
}
158+
hs := f(hFences, m)
159+
vs := f(vFences, n)
160+
ans := 0
161+
for h := range hs {
162+
if vs[h] {
163+
ans = max(ans, h)
164+
}
165+
}
166+
if ans > 0 {
167+
return ans * ans % (1e9 + 7)
168+
}
169+
return -1
170+
}
171+
```
74172

173+
### **TypeScript**
174+
175+
```ts
176+
function maximizeSquareArea(m: number, n: number, hFences: number[], vFences: number[]): number {
177+
const f = (nums: number[], k: number): Set<number> => {
178+
nums.push(1, k);
179+
nums.sort((a, b) => a - b);
180+
const s: Set<number> = new Set();
181+
for (let i = 0; i < nums.length; ++i) {
182+
for (let j = 0; j < i; ++j) {
183+
s.add(nums[i] - nums[j]);
184+
}
185+
}
186+
return s;
187+
};
188+
const hs = f(hFences, m);
189+
const vs = f(vFences, n);
190+
let ans = 0;
191+
for (const h of hs) {
192+
if (vs.has(h)) {
193+
ans = Math.max(ans, h);
194+
}
195+
}
196+
return ans ? Number(BigInt(ans) ** 2n % 1000000007n) : -1;
197+
}
75198
```
76199

77200
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int maximizeSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
4+
auto f = [](vector<int>& nums, int k) {
5+
nums.push_back(k);
6+
nums.push_back(1);
7+
sort(nums.begin(), nums.end());
8+
unordered_set<int> s;
9+
for (int i = 0; i < nums.size(); ++i) {
10+
for (int j = 0; j < i; ++j) {
11+
s.insert(nums[i] - nums[j]);
12+
}
13+
}
14+
return s;
15+
};
16+
auto hs = f(hFences, m);
17+
auto vs = f(vFences, n);
18+
int ans = 0;
19+
for (int h : hs) {
20+
if (vs.count(h)) {
21+
ans = max(ans, h);
22+
}
23+
}
24+
const int mod = 1e9 + 7;
25+
return ans > 0 ? 1LL * ans * ans % mod : -1;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func maximizeSquareArea(m int, n int, hFences []int, vFences []int) int {
2+
f := func(nums []int, k int) map[int]bool {
3+
nums = append(nums, 1, k)
4+
sort.Ints(nums)
5+
s := map[int]bool{}
6+
for i := 0; i < len(nums); i++ {
7+
for j := 0; j < i; j++ {
8+
s[nums[i]-nums[j]] = true
9+
}
10+
}
11+
return s
12+
}
13+
hs := f(hFences, m)
14+
vs := f(vFences, n)
15+
ans := 0
16+
for h := range hs {
17+
if vs[h] {
18+
ans = max(ans, h)
19+
}
20+
}
21+
if ans > 0 {
22+
return ans * ans % (1e9 + 7)
23+
}
24+
return -1
25+
}

0 commit comments

Comments
 (0)