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.
- Trick: 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:
- def maxPoints(self, points: List[List[int]]) -> int:
- points=sorted(points)
- ans=0
- for i in range(len(points)):
- dict={}
- temp=0
- for j in range(i+1,len(points)):
- x1,y1=points[i][0],points[i][1]
- x2,y2=points[j][0],points[j][1]
- if x2-x1==0:
- temp+=1
- else:
- slope=(y2-y1)/(x2-x1)
- if slope in dict.keys():
- dict[slope]+=1
- else:
- dict[slope]=1
- ans=max(ans,dict[slope])
- ans=max(ans,temp)
- return max(ans,temp)+1;
- ;
Advertisement
Add Comment
Please, Sign In to add comment