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
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | A square and a point inside | Not run | |
| 2 | Points on an edge | Not run | |
| 3 | Repeated points | Not run | |
| 4 | The lowest, then the leftmost | Not run | |
| 5 | All on one line | Not run | |
| 6 | One upright line | Not run | |
| 7 | One point | Not run | |
| 8 | Two points | Not run | |
| 9 | Negative coordinates | Not run | |
| 10 | A triangle full of points | Not run | |
| 11 | 100,000 points, all corners | Not run | |
| 12 | 200,000 random points | Not run |