GEOM-P3

The fence around the stations

Practice 3 of 4Computational geometry12 tests

The problem

Stations stand at whole-number points. A straight-sided fence must enclose them all and be as short as possible: it is the convex hull. Return its corners counterclockwise, starting from the point with the smallest y (the smallest x among those, if several). Points that lie on an edge between two corners are not corners. Repeated points count once.

hull([(0, 0), (2, 0), (2, 2), (0, 2), (1, 1)])
# [(0, 0), (2, 0), (2, 2), (0, 2)]
hull([(0, 0), (1, 0), (2, 0), (2, 2), (0, 2)])
# [(0, 0), (2, 0), (2, 2), (0, 2)]
hull([(2, 2), (0, 0), (5, 5)])   # [(0, 0), (5, 5)]

If all the points lie on one line, return its two ends (the lower one first); if there is only one point, return it alone.

Limits

  • Up to 200,000 points, with coordinates up to 10⁹. The big tests have 5 seconds each.
  • On one test every point is a corner, so gift wrapping (O(n · h)) is too slow.

Builds on GEOM-3 Convex hull.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

12 tests, not run yet
Test results
#TestResultReads · time
1A square and a point insideNot run
2Points on an edgeNot run
3Repeated pointsNot run
4The lowest, then the leftmostNot run
5All on one lineNot run
6One upright lineNot run
7One pointNot run
8Two pointsNot run
9Negative coordinatesNot run
10A triangle full of pointsNot run
11100,000 points, all cornersNot run
12200,000 random pointsNot run