Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1051 <= coords[i][0], coords[i][1] <= 106coords[i] are unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math · Greedy
[[1,1],[1,2],[3,2],[3,3]]
[[1,1],[2,2],[3,3]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3588: Find Maximum Area of a Triangle
class Solution {
public long maxArea(int[][] coords) {
long ans = calc(coords);
for (int[] c : coords) {
int tmp = c[0];
c[0] = c[1];
c[1] = tmp;
}
ans = Math.max(ans, calc(coords));
return ans > 0 ? ans : -1;
}
private long calc(int[][] coords) {
int mn = Integer.MAX_VALUE, mx = 0;
Map<Integer, Integer> f = new HashMap<>();
Map<Integer, Integer> g = new HashMap<>();
for (int[] c : coords) {
int x = c[0], y = c[1];
mn = Math.min(mn, x);
mx = Math.max(mx, x);
if (f.containsKey(x)) {
f.put(x, Math.min(f.get(x), y));
g.put(x, Math.max(g.get(x), y));
} else {
f.put(x, y);
g.put(x, y);
}
}
long ans = 0;
for (var e : f.entrySet()) {
int x = e.getKey();
int y = e.getValue();
int d = g.get(x) - y;
ans = Math.max(ans, (long) d * Math.max(mx - x, x - mn));
}
return ans;
}
}
// Accepted solution for LeetCode #3588: Find Maximum Area of a Triangle
func maxArea(coords [][]int) int64 {
calc := func() int64 {
mn, mx := int(1e9), 0
f := make(map[int]int)
g := make(map[int]int)
for _, c := range coords {
x, y := c[0], c[1]
mn = min(mn, x)
mx = max(mx, x)
if _, ok := f[x]; ok {
f[x] = min(f[x], y)
g[x] = max(g[x], y)
} else {
f[x] = y
g[x] = y
}
}
var ans int64
for x, y := range f {
d := g[x] - y
ans = max(ans, int64(d)*int64(max(mx-x, x-mn)))
}
return ans
}
ans := calc()
for _, c := range coords {
c[0], c[1] = c[1], c[0]
}
ans = max(ans, calc())
if ans > 0 {
return ans
}
return -1
}
# Accepted solution for LeetCode #3588: Find Maximum Area of a Triangle
class Solution:
def maxArea(self, coords: List[List[int]]) -> int:
def calc() -> int:
mn, mx = inf, 0
f = {}
g = {}
for x, y in coords:
mn = min(mn, x)
mx = max(mx, x)
if x in f:
f[x] = min(f[x], y)
g[x] = max(g[x], y)
else:
f[x] = g[x] = y
ans = 0
for x, y in f.items():
d = g[x] - y
ans = max(ans, d * max(mx - x, x - mn))
return ans
ans = calc()
for c in coords:
c[0], c[1] = c[1], c[0]
ans = max(ans, calc())
return ans if ans else -1
// Accepted solution for LeetCode #3588: Find Maximum Area of a Triangle
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3588: Find Maximum Area of a Triangle
// class Solution {
// public long maxArea(int[][] coords) {
// long ans = calc(coords);
// for (int[] c : coords) {
// int tmp = c[0];
// c[0] = c[1];
// c[1] = tmp;
// }
// ans = Math.max(ans, calc(coords));
// return ans > 0 ? ans : -1;
// }
//
// private long calc(int[][] coords) {
// int mn = Integer.MAX_VALUE, mx = 0;
// Map<Integer, Integer> f = new HashMap<>();
// Map<Integer, Integer> g = new HashMap<>();
//
// for (int[] c : coords) {
// int x = c[0], y = c[1];
// mn = Math.min(mn, x);
// mx = Math.max(mx, x);
// if (f.containsKey(x)) {
// f.put(x, Math.min(f.get(x), y));
// g.put(x, Math.max(g.get(x), y));
// } else {
// f.put(x, y);
// g.put(x, y);
// }
// }
//
// long ans = 0;
// for (var e : f.entrySet()) {
// int x = e.getKey();
// int y = e.getValue();
// int d = g.get(x) - y;
// ans = Math.max(ans, (long) d * Math.max(mx - x, x - mn));
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3588: Find Maximum Area of a Triangle
function maxArea(coords: number[][]): number {
function calc(): number {
let [mn, mx] = [Infinity, 0];
const f = new Map<number, number>();
const g = new Map<number, number>();
for (const [x, y] of coords) {
mn = Math.min(mn, x);
mx = Math.max(mx, x);
if (f.has(x)) {
f.set(x, Math.min(f.get(x)!, y));
g.set(x, Math.max(g.get(x)!, y));
} else {
f.set(x, y);
g.set(x, y);
}
}
let ans = 0;
for (const [x, y] of f) {
const d = g.get(x)! - y;
ans = Math.max(ans, d * Math.max(mx - x, x - mn));
}
return ans;
}
let ans = calc();
for (const c of coords) {
[c[0], c[1]] = [c[1], c[0]];
}
ans = Math.max(ans, calc());
return ans > 0 ? ans : -1;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.