- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 37 lines of Java from the credited upstream file 1610.java.
- The implementation visibly relies on sequence storage.
- 4 loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {3 final int posX = location.get(0);4 final int posY = location.get(1);5 int maxVisible = 0;6 int same = 0;7 List<Double> pointAngles = new ArrayList<>();8 9 for (List<Integer> p : points) {10 final int x = p.get(0);11 final int y = p.get(1);12 if (x == posX && y == posY)13 ++same;14 else15 pointAngles.add(getAngle(y - posY, x - posX));16 }17 18 Collections.sort(pointAngles);19 20 final int n = pointAngles.size();21 for (int i = 0; i < n; ++i)22 pointAngles.add(pointAngles.get(i) + 360);23 24 for (int l = 0, r = 0; r < pointAngles.size(); ++r) {25 while (pointAngles.get(r) - pointAngles.get(l) > angle)26 ++l;27 maxVisible = Math.max(maxVisible, r - l + 1);28 }29 30 return maxVisible + same;31 }32 33 private double getAngle(int dy, int dx) {34 return Math.atan2(dy, dx) * 180 / Math.PI;35 }36}37