Lv.4convex-hullgeometrysorting
Convex Hull Vertices
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Trees stand at points[i] = [x, y] (duplicates allowed). Return the number of vertices of their convex hull: points lying on a
straight edge do not count, all-identical points give 1, and collinear points give 2.
Examples
| points | result |
|---|---|
[[0, 0], [2, 0], [2, 2], [0, 2], [1, 1], [1, 0]] | 4 |
[[0, 0], [1, 1], [2, 2], [3, 3]] | 2 |
[[5, 5], [5, 5]] | 1 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(points: int[][]) → int
Constraints
-
points.length,
Expected complexity
- Time
- O(N log N)
- Space
- O(N)
Examples
| points | Expected |
|---|---|
[[0,0],[2,0],[2,2],[0,2],[1,1],[1,0]] | 4 |
[[0,0],[1,1],[2,2],[3,3]] | 2 |
[[5,5],[5,5]] | 1 |
Hint
Hint
Andrew's monotone chain: sort, dedupe, build lower and upper hulls popping while the cross product is <= 0 (so edge points are
dropped). Use 64-bit cross products (each product reaches ), never doubles. Gift wrapping is .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 234 ms | estimated |
| C++ (GCC, gnu++23) | 234 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 2058 ms | derived |
| Kotlin 2.4 (JVM) | 1468 ms | estimated |
| C# 14 (.NET 10) | 1468 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 9 | verdict only |
| performance | 60 | 4 | verdict only |