Might help you!


#1

int Solution::maxPoints(vector &A, vector &B) {
if(A.size() == 0)
return 0;
int numerator_of_slope = 0;
int denominator_of_slope = 0;
pair <int,int> hashKey;
int maxNoOfPoints = 1;
int gcdResult = 0;
for(int i = 0; i < A.size(); i++){
map < pair <int,int> , int > slopeMap;
for(int j = 0; j < A.size(); j++){
if(i == j)
continue;
numerator_of_slope = A[i] - A[j];
denominator_of_slope = B[i] - B[j];
gcdResult = (numerator_of_slope == 0 && denominator_of_slope == 0) ? 1 : __gcd(numerator_of_slope,denominator_of_slope);
numerator_of_slope /= gcdResult;
denominator_of_slope /= gcdResult;
hashKey = make_pair(numerator_of_slope,denominator_of_slope);
if(slopeMap.find(hashKey) == slopeMap.end())
slopeMap[hashKey] = 2;
else
slopeMap[hashKey]++;
maxNoOfPoints = max(maxNoOfPoints, slopeMap[hashKey]);
}
}
return maxNoOfPoints;
}