← Home

1610. Maximum Number of Visible Points

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
161

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1610. Maximum Number of Visible Points, the solution in this repository is mainly a greedy solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

The notes already sitting in the source point us in the right direction:

  • two pointer
  • WA
  • 16 / 120 test cases passed.
  • [[956,232],[438,752],[595,297],[508,143],[111,594],[645,824],[758,434],[447,423],[825,356],[807,377]]

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are visiblePoints, getAngle1, getAngle2.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//two pointer
02//WA
03//16 / 120 test cases passed.
04//[[956,232],[438,752],[595,297],[508,143],[111,594],[645,824],[758,434],[447,423],[825,356],[807,377]]
05//38
06//[74,581]
07class Solution {
08public:
09    int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
10        vector<double> angles;
11        
12        int must_visible = 0;
13        
14        for(const vector<int>& point : points){
15            int xdiff = point[0] - location[0];
16            int ydiff = point[1] - location[1];
17            
18            double a = -1;
19            
20            if(xdiff == 0){
21                if(ydiff == 0){
22                    ++must_visible;
23                }else if(ydiff > 0){
24                    a = 90;
25                }else if(ydiff < 0){
26                    a = 270;
27                }
28            }else if(ydiff == 0){
29                a = (xdiff > 0) ? 0 : 180;
30            }else{
31                a = atan(ydiff/xdiff);
32                a = a*180.0/M_PI;
33                
34                if(ydiff < 0){
35                    a += 180;
36                }
37            }
38            
39            // cout << xdiff << ", " << ydiff << ", " << a << endl;
40            
41            if(a != -1) angles.push_back(a);
42        }
43        
44        // cout << "angles: " << endl;
45        // for(const double& d : angles){
46        //     cout << d << " ";
47        // }
48        // cout << endl;
49        
50        sort(angles.begin(), angles.end());
51        
52        vector<double> angles_p360 = angles;
53        for(int i = 0; i < angles_p360.size(); ++i){
54            angles_p360[i] += 360;
55        }
56        for(int i = 0; i < angles_p360.size(); ++i){
57            angles.push_back(angles_p360[i]);
58        }
59        
60        int max_visible_by_rotate = 0;
61        for(int i = 0, j = 0; i < angles.size(); ++i){
62            while(j < angles.size() && 
63                  (angles[j] - angles[i] - angle - 1e-1
64                   /*numeric_limits<double>::epsilon() * max({abs(angles[j]), abs(angles[i]), abs((double)angle)})*/ <= 0)){
65                ++j;
66            }
67            max_visible_by_rotate = max(max_visible_by_rotate, j-i);
68        }
69        
70        // cout << "must_visible: " << must_visible << endl;
71        // cout << "max_visible_by_rotate: " << max_visible_by_rotate << endl;
72        
73        return must_visible + max_visible_by_rotate;
74    }
75};
76
77//two pointer, atan's range is [-M_PI/2, M_PI/2]!!
78//https://leetcode.com/problems/maximum-number-of-visible-points/discuss/877735/C%2B%2B-Clean-with-Explanation
79//Runtime: 800 ms, faster than 50.00% of C++ online submissions for Maximum Number of Visible Points.
80//Memory Usage: 135.4 MB, less than 50.00% of C++ online submissions for Maximum Number of Visible Points.
81class Solution {
82public:
83    double getAngle1(double xdiff, double ydiff){
84        //a in [-M_PI/2, M_PI/2]
85        double a = atan(ydiff/xdiff);
86        
87        //[-M_PI/2, M_PI/2] -> [0, 2*M_PI]
88        if(xdiff < 0 && ydiff > 0){
89            //2nd quadrant
90            a += M_PI;
91        }else if(xdiff < 0 && ydiff < 0){
92            //3rd quadrant
93            a += M_PI;
94        }else if(xdiff > 0 && ydiff < 0){
95            //4th quadrant
96            a += M_PI*2;
97        }
98        a = a/M_PI*180.0;
99        
100        return a;
101    }
102    
103    double getAngle2(double xdiff, double ydiff){
104        // atan2: [-M_PI, M_PI]
105        double a = atan2(ydiff, xdiff);
106        // [-M_PI, M_PI] -> [0, M_PI*2]
107        if(a < 0) a += M_PI*2;
108        a = a/M_PI*180.0;
109        
110        return a;
111    }
112    
113    int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
114        vector<double> angles;
115        
116        int must_visible = 0;
117        
118        for(const vector<int>& point : points){
119            int xdiff = point[0] - location[0];
120            int ydiff = point[1] - location[1];
121            
122            double a = -1;
123            
124            if(xdiff == 0 && ydiff == 0){
125                ++must_visible;
126            }else if(xdiff == 0){
127                a = (ydiff > 0) ? 90 : 270;
128            }else{
129                a = getAngle2(xdiff, ydiff);
130                // cout << getAngle1(xdiff, ydiff) << ", " << getAngle2(xdiff, ydiff) << endl;
131            }
132            
133            if(a != -1) angles.push_back(a);
134        }
135        
136        // cout << "angles: " << endl;
137        // for(const double& d : angles){
138        //     cout << d << " ";
139        // }
140        // cout << endl;
141        
142        sort(angles.begin(), angles.end());
143        
144        vector<double> angles_p360 = angles;
145        for(int i = 0; i < angles_p360.size(); ++i){
146            angles_p360[i] += 360;
147        }
148        for(int i = 0; i < angles_p360.size(); ++i){
149            angles.push_back(angles_p360[i]);
150        }
151        
152        int max_visible_by_rotate = 0;
153        
154        //actually 1e-9 is not needed here
155        for(int i = 0, j = 0; i < angles.size(); ++i){
156            while(j < angles.size() && angles[j] - angles[i] <= angle + 1e-9){
157                ++j;
158            }
159            max_visible_by_rotate = max(max_visible_by_rotate, j-i);
160        }
161        
162        //why add 1e-9 on RHS????
163        // for(int i = 0, j = 0; j < angles.size(); ++j){
164        //     while(i < angles.size() && angles[j] - angles[i] >= angle + 1e-9){
165        //         ++i;
166        //     }
167        //     max_visible_by_rotate = max(max_visible_by_rotate, j-i+1);
168        // }
169        
170        // cout << "must_visible: " << must_visible << endl;
171        // cout << "max_visible_by_rotate: " << max_visible_by_rotate << endl;
172        
173        return must_visible + max_visible_by_rotate;
174    }
175};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.