Leetcode 3588. Find Maximum Area of a Triangle
- Leetcode 3588. Find Maximum Area of a Triangle
- 1. 解题思路
- 2. 代码实现
- 题目链接:3588. Find Maximum Area of a Triangle
1. 解题思路
这一题我们只需要分别将同x坐标与同y坐标的点分堆记录下来,然后分别考察一下一条边平行于x轴以及一条边平行于y轴的两种情况即可。
在每一种情况下,我们都只需要在同一个坐标下(比如x坐标下)找到最远的两个点,然后考察x坐标下距离其最远的位置即可。
2. 代码实现
给出python代码实现瑞希啊:
class Solution:def maxArea(self, coords: List[List[int]]) -> int:xaxis, yaxis = defaultdict(list), defaultdict(list)for x, y in coords:xaxis[x].append(y)yaxis[y].append(x)xs, ys = sorted(xaxis.keys()), sorted(yaxis.keys())ans = -1if len(xs) > 1:for x in xs:if len(xaxis[x]) >= 2:d = max(xaxis[x]) - min(xaxis[x])h = max(x-xs[0], xs[-1]-x)ans = max(ans, d*h)if len(ys) > 1:for y in ys:if len(yaxis[y]) >= 2:d = max(yaxis[y]) - min(yaxis[y])h = max(y-ys[0], ys[-1]-y)ans = max(ans, d*h)return ans
提交代码评测得到:耗时618ms,占用内存74.12MB。