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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden.
Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if all the trees are enclosed.
Return the coordinates of trees that are exactly located on the fence perimeter. You may return the answer in any order.
Example 1:
Input: trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: [[1,1],[2,0],[4,2],[3,3],[2,4]] Explanation: All the trees will be on the perimeter of the fence except the tree at [2, 2], which will be inside the fence.
Example 2:
Input: trees = [[1,2],[2,2],[4,2]] Output: [[4,2],[2,2],[1,2]] Explanation: The fence forms a line that passes through all the trees.
Constraints:
1 <= trees.length <= 3000trees[i].length == 20 <= xi, yi <= 100Problem summary: You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden. Fence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if all the trees are enclosed. Return the coordinates of trees that are exactly located on the fence perimeter. You may return the answer in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
[[1,2],[2,2],[4,2]]
erect-the-fence-ii)sort-the-students-by-their-kth-score)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #587: Erect the Fence
class Solution {
public int[][] outerTrees(int[][] trees) {
int n = trees.length;
if (n < 4) {
return trees;
}
Arrays.sort(trees, (a, b) -> { return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]; });
boolean[] vis = new boolean[n];
int[] stk = new int[n + 10];
int cnt = 1;
for (int i = 1; i < n; ++i) {
while (cnt > 1 && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) {
vis[stk[--cnt]] = false;
}
vis[i] = true;
stk[cnt++] = i;
}
int m = cnt;
for (int i = n - 1; i >= 0; --i) {
if (vis[i]) {
continue;
}
while (cnt > m && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) {
--cnt;
}
stk[cnt++] = i;
}
int[][] ans = new int[cnt - 1][2];
for (int i = 0; i < ans.length; ++i) {
ans[i] = trees[stk[i]];
}
return ans;
}
private int cross(int[] a, int[] b, int[] c) {
return (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]);
}
}
// Accepted solution for LeetCode #587: Erect the Fence
func outerTrees(trees [][]int) [][]int {
n := len(trees)
if n < 4 {
return trees
}
sort.Slice(trees, func(i, j int) bool {
if trees[i][0] == trees[j][0] {
return trees[i][1] < trees[j][1]
}
return trees[i][0] < trees[j][0]
})
cross := func(i, j, k int) int {
a, b, c := trees[i], trees[j], trees[k]
return (b[0]-a[0])*(c[1]-b[1]) - (b[1]-a[1])*(c[0]-b[0])
}
vis := make([]bool, n)
stk := []int{0}
for i := 1; i < n; i++ {
for len(stk) > 1 && cross(stk[len(stk)-1], stk[len(stk)-2], i) < 0 {
vis[stk[len(stk)-1]] = false
stk = stk[:len(stk)-1]
}
vis[i] = true
stk = append(stk, i)
}
m := len(stk)
for i := n - 1; i >= 0; i-- {
if vis[i] {
continue
}
for len(stk) > m && cross(stk[len(stk)-1], stk[len(stk)-2], i) < 0 {
stk = stk[:len(stk)-1]
}
stk = append(stk, i)
}
var ans [][]int
for i := 0; i < len(stk)-1; i++ {
ans = append(ans, trees[stk[i]])
}
return ans
}
# Accepted solution for LeetCode #587: Erect the Fence
class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def cross(i, j, k):
a, b, c = trees[i], trees[j], trees[k]
return (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0])
n = len(trees)
if n < 4:
return trees
trees.sort()
vis = [False] * n
stk = [0]
for i in range(1, n):
while len(stk) > 1 and cross(stk[-2], stk[-1], i) < 0:
vis[stk.pop()] = False
vis[i] = True
stk.append(i)
m = len(stk)
for i in range(n - 2, -1, -1):
if vis[i]:
continue
while len(stk) > m and cross(stk[-2], stk[-1], i) < 0:
stk.pop()
stk.append(i)
stk.pop()
return [trees[i] for i in stk]
// Accepted solution for LeetCode #587: Erect the Fence
/**
* [0587] Erect the Fence
*
* You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden.
* You are asked to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed.
* Return the coordinates of trees that are exactly located on the fence perimeter.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/erect2-plane.jpg" style="width: 509px; height: 500px;" />
* Input: points = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
* Output: [[1,1],[2,0],[3,3],[2,4],[4,2]]
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/erect1-plane.jpg" style="width: 509px; height: 500px;" />
* Input: points = [[1,2],[2,2],[4,2]]
* Output: [[4,2],[2,2],[1,2]]
*
*
* Constraints:
*
* 1 <= points.length <= 3000
* points[i].length == 2
* 0 <= xi, yi <= 100
* All the given points are unique.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/erect-the-fence/
// discuss: https://leetcode.com/problems/erect-the-fence/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
// Credit: https://leetcode.com/problems/erect-the-fence/discuss/792325/Rust-translated-8ms-2.2M-100
pub fn outer_trees(trees: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
fn orientation(p: &[i32], q: &[i32], r: &[i32]) -> i32 {
(q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
}
let mut trees = trees;
if trees.len() <= 1 {
return trees;
}
trees.sort_by(|p, q| p[0].cmp(&q[0]).then(p[1].cmp(&q[1])));
let mut hull = Vec::<Vec<i32>>::new();
let n = trees.len();
for i in 0..n {
while hull.len() >= 2
&& orientation(
hull.get(hull.len() - 2).unwrap(),
hull.get(hull.len() - 1).unwrap(),
&trees[i],
) > 0
{
hull.pop();
}
hull.push(trees[i].clone());
}
for i in (0..n).rev() {
while hull.len() >= 2
&& orientation(
hull.get(hull.len() - 2).unwrap(),
hull.get(hull.len() - 1).unwrap(),
&trees[i],
) > 0
{
hull.pop();
}
hull.push(trees[i].clone());
}
let mut set = std::collections::HashSet::<Vec<i32>>::new();
for v in hull {
set.insert(v);
}
let mut result = vec![];
for v in set {
result.push(v);
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_0587_example_1() {
let trees = vec![
vec![1, 1],
vec![2, 2],
vec![2, 0],
vec![2, 4],
vec![3, 3],
vec![4, 2],
];
let result = vec![vec![1, 1], vec![2, 0], vec![3, 3], vec![2, 4], vec![4, 2]];
assert_eq_sorted!(Solution::outer_trees(trees), result);
}
#[test]
fn test_0587_example_2() {
let trees = vec![vec![1, 2], vec![2, 2], vec![4, 2]];
let result = vec![vec![4, 2], vec![2, 2], vec![1, 2]];
assert_eq_sorted!(Solution::outer_trees(trees), result);
}
}
// Accepted solution for LeetCode #587: Erect the Fence
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #587: Erect the Fence
// class Solution {
// public int[][] outerTrees(int[][] trees) {
// int n = trees.length;
// if (n < 4) {
// return trees;
// }
// Arrays.sort(trees, (a, b) -> { return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]; });
// boolean[] vis = new boolean[n];
// int[] stk = new int[n + 10];
// int cnt = 1;
// for (int i = 1; i < n; ++i) {
// while (cnt > 1 && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) {
// vis[stk[--cnt]] = false;
// }
// vis[i] = true;
// stk[cnt++] = i;
// }
// int m = cnt;
// for (int i = n - 1; i >= 0; --i) {
// if (vis[i]) {
// continue;
// }
// while (cnt > m && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) {
// --cnt;
// }
// stk[cnt++] = i;
// }
// int[][] ans = new int[cnt - 1][2];
// for (int i = 0; i < ans.length; ++i) {
// ans[i] = trees[stk[i]];
// }
// return ans;
// }
//
// private int cross(int[] a, int[] b, int[] c) {
// return (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]);
// }
// }
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.