1398. Pick Interior Points
A land registry stores a parcel as a simple polygon whose vertices are integer points, listed in order around the boundary (clockwise or counter-clockwise, and some listed vertices may sit in the middle of a straight side). An inspector wants to know how many integer grid points lie strictly inside the parcel, not on its fence.
Given polygon as a list of [x, y] vertices, return that count as a 64-bit integer. Use Pick's theorem: with A2 the absolute value of the shoelace sum (twice the area) and B the number of lattice points on the boundary, which equals the sum over all edges of gcd(|dx|, |dy|), the interior count is (A2 - B + 2) / 2. Edges include the closing edge from the last vertex back to the first. Coordinates reach 10^6, so the shoelace sum needs 64-bit arithmetic. The intended solution runs in O(n log C) time and O(1) extra space, where C is the coordinate range.
Example 1
- Input:
- polygon = [[0,0],[4,0],[4,4],[0,4]]
- Output:
- 9
- Explanation:
A 4 by 4 square has area 16 and 16 boundary points, so I = (32 - 16 + 2) / 2 = 9.
Example 2
- Input:
- polygon = [[0,0],[10,0],[0,10]]
- Output:
- 36
- Explanation:
Twice the area is 100 and B = 10 + 10 + 10 = 30, so I = (100 - 30 + 2) / 2 = 36.
Constraints
3 ≤ polygon.length ≤ 1000
-106 ≤ x, y ≤ 106
The polygon is simple: edges meet only at shared endpoints and it has non-zero area
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n log C)
- Space
- O(1)