3588. Find Maximum Area of a Triangle
Description
You are given a 2D array coords
of size n x 2
, representing the coordinates of n
points in an infinite Cartesian plane.
Find twice the maximum area of a triangle with its corners at any three elements from coords
, such that at least one side of this triangle is parallel to the x-axis or y-axis. Formally, if the maximum area of such a triangle is A
, return 2 * A
.
If no such triangle exists, return -1.
Note that a triangle cannot have zero area.
Example 1:
Input: coords = [[1,1],[1,2],[3,2],[3,3]]
Output: 2
Explanation:
The triangle shown in the image has a base 1 and height 2. Hence its area is 1/2 * base * height = 1
.
Example 2:
Input: coords = [[1,1],[2,2],[3,3]]
Output: -1
Explanation:
The only possible triangle has corners (1, 1)
, (2, 2)
, and (3, 3)
. None of its sides are parallel to the x-axis or the y-axis.
Constraints:
1 <= n == coords.length <= 105
1 <= coords[i][0], coords[i][1] <= 106
- All
coords[i]
are unique.
Solutions
Solution 1: Enumeration + Hash Map
The problem asks for twice the area of the triangle, so we can directly calculate the product of the base and height of the triangle.
Since the triangle must have at least one side parallel to the \(x\)-axis or \(y\)-axis, we can enumerate sides parallel to the \(x\)-axis and calculate the double area for all possible triangles, then swap the coordinates in \(\textit{coords}\) and repeat the process to calculate the double area for triangles with sides parallel to the \(y\)-axis.
Therefore, we design a function \(\textit{calc}\) to calculate the double area for all possible triangles with sides parallel to the \(y\)-axis.
We use two hash maps \(\textit{f}\) and \(\textit{g}\) to record the minimum and maximum \(y\)-coordinates for each \(x\)-coordinate. Then we iterate through \(\textit{coords}\), updating \(\textit{f}\) and \(\textit{g}\), and also record the minimum and maximum \(x\)-coordinates. Finally, we iterate through \(\textit{f}\), calculate the double area for each \(x\)-coordinate, and update the answer.
In the main function, we first call the \(\textit{calc}\) function to calculate the double area for triangles with sides parallel to the \(y\)-axis, then swap the coordinates in \(\textit{coords}\) and repeat the process to calculate the double area for triangles with sides parallel to the \(x\)-axis. Finally, we return the answer; if the answer is 0, we return -1.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the length of \(\textit{coords}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|