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 points of size n x 2 representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi].
Count the number of pairs of points (A, B), where
A is on the upper left side of B, andA and B.Return the count.
Example 1:
Input: points = [[1,1],[2,2],[3,3]]
Output: 0
Explanation:
There is no way to choose A and B such that A is on the upper left side of B.
Example 2:
Input: points = [[6,2],[4,4],[2,6]]
Output: 2
Explanation:
(points[1], points[0]), where points[1] is on the upper left side of points[0] and the rectangle is empty.(points[2], points[1]), same as the left one it is a valid pair.(points[2], points[0]), where points[2] is on the upper left side of points[0], but points[1] is inside the rectangle so it's not a valid pair.Example 3:
Input: points = [[3,1],[1,3],[1,1]]
Output: 2
Explanation:
(points[2], points[0]), where points[2] is on the upper left side of points[0] and there are no other points on the line they form. Note that it is a valid state when the two points form a line.(points[1], points[2]), it is a valid pair same as the left one.(points[1], points[0]), it is not a valid pair as points[2] is on the border of the rectangle.Constraints:
2 <= n <= 50points[i].length == 20 <= points[i][0], points[i][1] <= 50points[i] are distinct.Problem summary: You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi]. Count the number of pairs of points (A, B), where A is on the upper left side of B, and there are no other points in the rectangle (or line) they make (including the border), except for the points A and B. Return the count.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,1],[2,2],[3,3]]
[[6,2],[4,4],[2,6]]
[[3,1],[1,3],[1,1]]
rectangle-area)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3025: Find the Number of Ways to Place People I
class Solution {
public int numberOfPairs(int[][] points) {
Arrays.sort(points, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0;
int n = points.length;
final int inf = 1 << 30;
for (int i = 0; i < n; ++i) {
int y1 = points[i][1];
int maxY = -inf;
for (int j = i + 1; j < n; ++j) {
int y2 = points[j][1];
if (maxY < y2 && y2 <= y1) {
maxY = y2;
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3025: Find the Number of Ways to Place People I
func numberOfPairs(points [][]int) (ans int) {
sort.Slice(points, func(i, j int) bool {
return points[i][0] < points[j][0] || points[i][0] == points[j][0] && points[j][1] < points[i][1]
})
for i, p1 := range points {
y1 := p1[1]
maxY := math.MinInt32
for _, p2 := range points[i+1:] {
y2 := p2[1]
if maxY < y2 && y2 <= y1 {
maxY = y2
ans++
}
}
}
return
}
# Accepted solution for LeetCode #3025: Find the Number of Ways to Place People I
class Solution:
def numberOfPairs(self, points: List[List[int]]) -> int:
points.sort(key=lambda x: (x[0], -x[1]))
ans = 0
for i, (_, y1) in enumerate(points):
max_y = -inf
for _, y2 in points[i + 1 :]:
if max_y < y2 <= y1:
max_y = y2
ans += 1
return ans
// Accepted solution for LeetCode #3025: Find the Number of Ways to Place People I
impl Solution {
pub fn number_of_pairs(mut points: Vec<Vec<i32>>) -> i32 {
points.sort_by(|a, b| {
if a[0] == b[0] {
b[1].cmp(&a[1])
} else {
a[0].cmp(&b[0])
}
});
let n = points.len();
let mut ans = 0;
for i in 0..n {
let y1 = points[i][1];
let mut max_y = i32::MIN;
for j in (i + 1)..n {
let y2 = points[j][1];
if max_y < y2 && y2 <= y1 {
max_y = y2;
ans += 1;
}
}
}
ans
}
}
// Accepted solution for LeetCode #3025: Find the Number of Ways to Place People I
function numberOfPairs(points: number[][]): number {
points.sort((a, b) => (a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]));
const n = points.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
const [_, y1] = points[i];
let maxY = -Infinity;
for (let j = i + 1; j < n; ++j) {
const [_, y2] = points[j];
if (maxY < y2 && y2 <= y1) {
maxY = y2;
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.