
2025-12-02:找到最大三角形面积。用go语言,给出一个大小为 n×2 的二维数组 coords,表示平面上 n 个点的坐标。任务是从这些点中任取三点构成三角形,并且要求该三角形至少有一条边与 x 轴或 y 轴平行。求所有满足条件的三角形中面积最大的那个的两倍(即 2A),若不存在符合条件且面积非零的三角形则返回 -1。
说明补充:
1 <= n == coords.length <= 100000。
1 <= coords[i][0], coords[i][1] <= 1000000。
所有 coords[i] 都是 唯一 的。
输入: coords = [[1,1],[1,2],[3,2],[3,3]]。
输出: 2。
解释:

在这里插入图片描述
图中的三角形的底边为 1,高为 2。因此,它的面积为 1/2 * 底边 * 高 = 1。
题目来自力扣3588。
calc(0) 处理与y轴平行的边,calc(1) 处理与x轴平行的边。calc(0))minY[x])和最大y(maxY[x]),同时记录全局最小x(minX)和最大x(maxX)。calc(1))calc(0) 和 calc(1) 的最大值作为候选结果。若结果为0(表示所有三角形退化为直线或不存在),返回-1;否则返回该值。calc 调用需遍历所有点一次(构建映射)和第二次遍历映射键(计算最大2A),映射键数最多为 (n)(点坐标唯一)。calc(分别对应j=0和j=1),总操作次数为 (2 \times O(n) = O(n))。minY 和 maxY 映射,最坏情况下(所有点x坐标互异)需 (O(n)) 空间。此方案在 (n \leq 100000) 时能高效运行,符合题目要求。
.
package main
import (
"fmt"
"math"
)
func maxArea(coords [][]int) int64 {
calc := func(j int) (res int) {
minX, maxX := math.MaxInt, 0
minY := map[int]int{}
maxY := map[int]int{}
for _, p := range coords {
x, y := p[j], p[1-j]
minX = min(minX, x)
maxX = max(maxX, x)
maxY[x] = max(maxY[x], y)
mn, ok := minY[x]
if !ok {
minY[x] = y
} else {
minY[x] = min(mn, y)
}
}
for x, y := range minY {
res = max(res, (maxY[x]-y)*max(maxX-x, x-minX))
}
return
}
ans := max(calc(0), calc(1))
if ans == 0 {
ans = -1
}
return int64(ans)
}
func main() {
coords := [][]int{{1, 1}, {1, 2}, {3, 2}, {3, 3}}
result := maxArea(coords)
fmt.Println(result)
}

.
# -*-coding:utf-8-*-
from typing import List
import math
def max_area(coords: List[List[int]]) -> int:
def calc(j: int) -> int:
min_x, max_x = math.inf, 0
min_y = {}
max_y = {}
for p in coords:
x, y = p[j], p[1 - j]
min_x = min(min_x, x)
max_x = max(max_x, x)
max_y[x] = max(max_y.get(x, y), y)
min_y[x] = min(min_y.get(x, y), y)
res = 0
for x, y in min_y.items():
width = max(max_x - x, x - min_x)
height = max_y[x] - y
res = max(res, height * width)
return res
ans = max(calc(0), calc(1))
return -1 if ans == 0 else ans
# 测试示例
coords = [[1, 1], [1, 2], [3, 2], [3, 3]]
result = max_area(coords)
print(result) # 输出结果
.
#include <iostream>
#include <vector>
#include <map>
#include <climits>
#include <algorithm>
using namespace std;
long long maxArea(vector<vector<int>>& coords) {
auto calc = [&](int j) -> long long {
int minX = INT_MAX, maxX = 0;
map<int, int> minY;
map<int, int> maxY;
for (auto& p : coords) {
int x = p[j];
int y = p[1 - j];
minX = min(minX, x);
maxX = max(maxX, x);
// 更新最大y值
if (maxY.find(x) == maxY.end()) {
maxY[x] = y;
} else {
maxY[x] = max(maxY[x], y);
}
// 更新最小y值
if (minY.find(x) == minY.end()) {
minY[x] = y;
} else {
minY[x] = min(minY[x], y);
}
}
long long res = 0;
for (auto& [x, y] : minY) {
int width = max(maxX - x, x - minX);
int height = maxY[x] - y;
res = max(res, (long long)width * height);
}
return res;
};
long long ans = max(calc(0), calc(1));
return (ans == 0) ? -1 : ans;
}
int main() {
vector<vector<int>> coords = {{1, 1}, {1, 2}, {3, 2}, {3, 3}};
long long result = maxArea(coords);
cout << result << endl; // 输出结果
return 0;
}

我们相信人工智能为普通人提供了一种“增强工具”,并致力于分享全方位的AI知识。在这里,您可以找到最新的AI科普文章、工具评测、提升效率的秘籍以及行业洞察。 欢迎关注“福大大架构师每日一题”,发消息可获得面试资料,让AI助力您的未来发展。