Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd939f6

Browse files
feat: add solutions to lc problem: No.3588 (doocs#4732)
No.3588.Find Maximum Area of a Triangle
1 parent 7e1a420 commit fd939f6

File tree

7 files changed

+535
-8
lines changed

7 files changed

+535
-8
lines changed

‎solution/3500-3599/3588.Find Maximum Area of a Triangle/README.md‎

Lines changed: 184 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,212 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:枚举 + 哈希表
79+
80+
题目要求三角形的两倍面积,因此我们可以直接计算三角形的底边和高的乘积。
81+
82+
又因为三角形至少有一条边与 $x$ 轴或 $y$ 轴平行,我们可以枚举与 $x$ 轴平行的边,计算所有可能的三角形的两倍面积,然后将 $\textit{coords}$ 横纵坐标交换后,重复上述过程,计算与 $y$ 轴平行的边的所有可能的三角形的两倍面积。
83+
84+
因此,我们设计一个函数 $\textit{calc}$ 来计算与 $y$轴平行的边的所有可能的三角形的两倍面积。
85+
86+
我们用两个哈希表 $\textit{f}$ 和 $\textit{g}$ 来记录每个横坐标对应的最小纵坐标和最大纵坐标。然后我们遍历 $\textit{coords},ドル更新哈希表 $\textit{f}$ 和 $\textit{g},ドル同时记录横坐标的最小值和最大值。最后,我们遍历哈希表 $\textit{f},ドル计算每个横坐标对应的三角形的两倍面积,并更新答案。
87+
88+
在主函数中,我们先调用 $\textit{calc}$ 函数计算与 $y$ 轴平行的边的所有可能的三角形的两倍面积,然后将 $\textit{coords}$ 横纵坐标交换后,重复上述过程,计算与 $x$ 轴平行的边的所有可能的三角形的两倍面积。最后返回答案,如果答案为 0,则返回 -1。
89+
90+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是 $\textit{coords}$ 的长度。
7991

8092
<!-- tabs:start -->
8193

8294
#### Python3
8395

8496
```python
85-
97+
class Solution:
98+
def maxArea(self, coords: List[List[int]]) -> int:
99+
def calc() -> int:
100+
mn, mx = inf, 0
101+
f = {}
102+
g = {}
103+
for x, y in coords:
104+
mn = min(mn, x)
105+
mx = max(mx, x)
106+
if x in f:
107+
f[x] = min(f[x], y)
108+
g[x] = max(g[x], y)
109+
else:
110+
f[x] = g[x] = y
111+
ans = 0
112+
for x, y in f.items():
113+
d = g[x] - y
114+
ans = max(ans, d * max(mx - x, x - mn))
115+
return ans
116+
117+
ans = calc()
118+
for c in coords:
119+
c[0], c[1] = c[1], c[0]
120+
ans = max(ans, calc())
121+
return ans if ans else -1
86122
```
87123

88124
#### Java
89125

90126
```java
91-
127+
class Solution {
128+
public long maxArea(int[][] coords) {
129+
long ans = calc(coords);
130+
for (int[] c : coords) {
131+
int tmp = c[0];
132+
c[0] = c[1];
133+
c[1] = tmp;
134+
}
135+
ans = Math.max(ans, calc(coords));
136+
return ans > 0 ? ans : -1;
137+
}
138+
139+
private long calc(int[][] coords) {
140+
int mn = Integer.MAX_VALUE, mx = 0;
141+
Map<Integer, Integer> f = new HashMap<>();
142+
Map<Integer, Integer> g = new HashMap<>();
143+
144+
for (int[] c : coords) {
145+
int x = c[0], y = c[1];
146+
mn = Math.min(mn, x);
147+
mx = Math.max(mx, x);
148+
if (f.containsKey(x)) {
149+
f.put(x, Math.min(f.get(x), y));
150+
g.put(x, Math.max(g.get(x), y));
151+
} else {
152+
f.put(x, y);
153+
g.put(x, y);
154+
}
155+
}
156+
157+
long ans = 0;
158+
for (var e : f.entrySet()) {
159+
int x = e.getKey();
160+
int y = e.getValue();
161+
int d = g.get(x) - y;
162+
ans = Math.max(ans, (long) d * Math.max(mx - x, x - mn));
163+
}
164+
return ans;
165+
}
166+
}
92167
```
93168

94169
#### C++
95170

96171
```cpp
97-
172+
class Solution {
173+
public:
174+
long long maxArea(vector<vector<int>>& coords) {
175+
auto calc = [&]() -> long long {
176+
int mn = INT_MAX, mx = 0;
177+
unordered_map<int, int> f, g;
178+
for (auto& c : coords) {
179+
int x = c[0], y = c[1];
180+
mn = min(mn, x);
181+
mx = max(mx, x);
182+
if (f.count(x)) {
183+
f[x] = min(f[x], y);
184+
g[x] = max(g[x], y);
185+
} else {
186+
f[x] = y;
187+
g[x] = y;
188+
}
189+
}
190+
long long ans = 0;
191+
for (auto& [x, y] : f) {
192+
int d = g[x] - y;
193+
ans = max(ans, 1LL * d * max(mx - x, x - mn));
194+
}
195+
return ans;
196+
};
197+
198+
long long ans = calc();
199+
for (auto& c : coords) {
200+
swap(c[0], c[1]);
201+
}
202+
ans = max(ans, calc());
203+
return ans > 0 ? ans : -1;
204+
}
205+
};
98206
```
99207

100208
#### Go
101209

102210
```go
211+
func maxArea(coords [][]int) int64 {
212+
calc := func() int64 {
213+
mn, mx := int(1e9), 0
214+
f := make(map[int]int)
215+
g := make(map[int]int)
216+
for _, c := range coords {
217+
x, y := c[0], c[1]
218+
mn = min(mn, x)
219+
mx = max(mx, x)
220+
if _, ok := f[x]; ok {
221+
f[x] = min(f[x], y)
222+
g[x] = max(g[x], y)
223+
} else {
224+
f[x] = y
225+
g[x] = y
226+
}
227+
}
228+
var ans int64
229+
for x, y := range f {
230+
d := g[x] - y
231+
ans = max(ans, int64(d)*int64(max(mx-x, x-mn)))
232+
}
233+
return ans
234+
}
235+
236+
ans := calc()
237+
for _, c := range coords {
238+
c[0], c[1] = c[1], c[0]
239+
}
240+
ans = max(ans, calc())
241+
if ans > 0 {
242+
return ans
243+
}
244+
return -1
245+
}
246+
```
103247

248+
#### TypeScript
249+
250+
```ts
251+
function maxArea(coords: number[][]): number {
252+
function calc(): number {
253+
let [mn, mx] = [Infinity, 0];
254+
const f = new Map<number, number>();
255+
const g = new Map<number, number>();
256+
257+
for (const [x, y] of coords) {
258+
mn = Math.min(mn, x);
259+
mx = Math.max(mx, x);
260+
if (f.has(x)) {
261+
f.set(x, Math.min(f.get(x)!, y));
262+
g.set(x, Math.max(g.get(x)!, y));
263+
} else {
264+
f.set(x, y);
265+
g.set(x, y);
266+
}
267+
}
268+
269+
let ans = 0;
270+
for (const [x, y] of f) {
271+
const d = g.get(x)! - y;
272+
ans = Math.max(ans, d * Math.max(mx - x, x - mn));
273+
}
274+
return ans;
275+
}
276+
277+
let ans = calc();
278+
for (const c of coords) {
279+
[c[0], c[1]] = [c[1], c[0]];
280+
}
281+
ans = Math.max(ans, calc());
282+
return ans > 0 ? ans : -1;
283+
}
104284
```
105285

106286
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /