Skip to the code
cf.
+25 XP

#665 Fence Around Map Markers

medium

A delivery-zone map draws the tightest convex outline around all store markers. points holds the distinct marker positions [x, y].

Write zoneCorners(points) that returns the corners of that outline: the convex hull. Points that lie in the middle of a straight edge of the outline are not corners and must be left out. The corners may be returned in any order.

If all points lie on one line, return the two end points. If there is one point, return it alone.

Example 1

Input: points = [[0,0],[4,0],[4,4],[0,4],[2,2],[2,1]]
Output: [[0,0],[4,0],[4,4],[0,4]]

The four corners of the square form the outline. [2, 2] and [2, 1] are inside.

Example 2

Input: points = [[0,0],[2,1],[4,2],[6,3]]
Output: [[0,0],[6,3]]

All points are on one line, so only the two ends are corners.

Constraints

  • 1 <= points.length <= 10^4
  • -10^4 <= x, y <= 10^4
  • All points are distinct.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).