c++ easy-understanding using map


  • 0
    X
    class Solution {
    public:
        // using map
        int maxPoints(vector<Point>& points) {
            int size = points.size();
            if(size <= 2){
                return size;
            }
            int maxNum = 0;
            for(int i = 0; i < size; ++i){
                map<pair<int, int>, int> m;
                int samePoints = 0;
                int vertical = 0;
                int iMaxNum = 0;
                int x = points[i].x, y = points[i].y;
                for(int j = i + 1; j < size; ++j){
                    int a = points[j].x - x, b = points[j].y - y;
                    if(a == 0 && b == 0){
                        ++samePoints;
                    }else if(a == 0){
                        ++vertical;
                        iMaxNum = max(iMaxNum, vertical);
                    }else{
                        int k = gcd(a, b);
                        a /= k;
                        b /= k;
                        ++m[make_pair(a, b)];
                        iMaxNum = max(iMaxNum, m[make_pair(a, b)]);
                    }
                }
                maxNum = max(maxNum, iMaxNum + samePoints + 1);
            }
            return maxNum;
        }
    private:
        int gcd(int a, int b){
            if(b == 0){
                return a;
            }
            return gcd(b, a % b);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.