3588. Find Maximum Area of a Triangle Medium

@problem@discussion
#Array#Hash Table#Math#Greedy#Geometry#Enumeration



1/**
2 * [3588] Find Maximum Area of a Triangle
3 *
4 * You are given a 2D array coords of size n x 2, representing the coordinates of n points in an infinite Cartesian plane.
5 * 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.
6 * If no such triangle exists, return -1.
7 * Note that a triangle cannot have zero area.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">coords = [[1,1],[1,2],[3,2],[3,3]]</span>
12 * Output: <span class="example-io">2</span>
13 * Explanation:
14 * <img src="https://assets.leetcode.com/uploads/2025/04/19/image-20250420010047-1.png" style="width: 300px; height: 289px;" />
15 * The triangle shown in the image has a base 1 and height 2. Hence its area is 1/2 * base * height = 1.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">coords = [[1,1],[2,2],[3,3]]</span>
20 * Output: <span class="example-io">-1</span>
21 * Explanation:
22 * 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.
23 * </div>
24 *  
25 * Constraints:
26 * 
27 * 	1 <= n == coords.length <= 10^5
28 * 	1 <= coords[i][0], coords[i][1] <= 10^6
29 * 	All coords[i] are unique.
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/find-maximum-area-of-a-triangle/
35// discuss: https://leetcode.com/problems/find-maximum-area-of-a-triangle/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40    pub fn max_area(coords: Vec<Vec<i32>>) -> i64 {
41        
42    }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_3588() {
53    }
54}
55

Back
© 2026 bowen.ge All Rights Reserved.