Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- We can find the slope of line from two points by slope=(y2-y1)/(x2-x1).
- We will iterate through the points and consider all the pairs of points.
- There is one temporary map which will store the slope count for every pairs
- for each ith iteration and finally pairs with maximum points of same slope
- will be the answer.
- There is one catch that when line will be vertical then (x2-x1) ie. denominator
- will be 0 so we are managing vertical lines using a temporary variable.
- */
- class Solution {
- public:
- int maxPoints(vector<vector<int>>& points) {
- int n=points.size();
- int ans=0;
- for(int i=0;i<n;i++){
- unordered_map<double,int> mp;
- int vertical=0;
- for(int j=i+1;j<n;j++){
- if(points[j][0]-points[i][0] ==0){vertical++;ans=max(ans,vertical);continue;}
- double slope= (double)(points[j][1]-points[i][1])/(points[j][0]-points[i][0]+1.0-1.0);
- mp[slope]++;
- ans=max(ans,mp[slope]);
- }
- mp.clear();
- }
- return ans+1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement