LeetCode-149. Max Points on a Line [C++][Java]

LeetCode-149. Max Points on a Lineicon-default.png?t=M3K6https://leetcode.com/problems/max-points-on-a-line/

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

Example 1:

LeetCode-149. Max Points on a Line [C++][Java]_第1张图片

Input: points = [[1,1],[2,2],[3,3]]
Output: 3

Example 2:

LeetCode-149. Max Points on a Line [C++][Java]_第2张图片

Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4

Constraints:

  • 1 <= points.length <= 300
  • points[i].length == 2
  • -10^4 <= xi, yi <= 10^4
  • All the points are unique.

【C++】

class Solution {
public:
    int maxPoints(vector>& points) {
        unordered_map hash; // <斜率, 点个数>
        int max_count = 0, same = 1, same_y = 1;
        for (int i = 0; i < points.size(); ++i) {
            same = 1, same_y = 1;
            for (int j = i + 1; j < points.size(); ++j) {
                if (points[i][1] == points[j][1]) {
                    ++same_y;
                    if (points[i][0] == points[j][0]) {++same;}
                } else {
                    double dx = points[i][0] - points[j][0], dy = points[i][1] - points[j][1];
                    ++hash[dx/dy];
                }
            }
            max_count = max(max_count, same_y);
            for (auto item : hash) {
                max_count = max(max_count, same + item.second);
            }
            hash.clear();
        }
        return max_count;
    }
};

【Java】

1. HashMap Method

class Solution {
    public int maxPoints(int[][] points) {
        Map hash = new HashMap<>(); // <斜率, 点个数>
        int max_count = 0, same = 1, same_y = 1;
        for (int i = 0; i < points.length; ++i) {
            same = 1; same_y = 1;
            for (int j = i + 1; j < points.length; ++j) {
                if (points[i][1] == points[j][1]) {
                    ++same_y;
                    if (points[i][0] == points[j][0]) {++same;}
                } else {
                    int dx = points[i][0] - points[j][0];
                    int dy = points[i][1] - points[j][1];
                    double k = 0;
                    if (dx != 0) {k = 1.0 * dx / dy;}
                    hash.put(k, hash.getOrDefault(k, 0) + 1);
                }
            }
            max_count = Math.max(max_count, same_y);
            for (Map.Entry item : hash.entrySet()) {
                max_count = Math.max(max_count, same + item.getValue());
            }
            hash.clear();
        }
        return max_count;
    }
}

2. GCD/HashMap Method

class Solution {
    public int maxPoints(int[][] points) {
        if (points == null) return 0;
        if (points.length <= 2) return points.length;
        Map> counts = new HashMap<>();
        int x1, x2, y1, y2;
        int max = 0;
        for (int i = 0; i < points.length; i++) {
            counts.clear();
            x1 = points[i][0];
            y1 = points[i][1];
            for (int j = i + 1; j < points.length; j++) {               
                x2 = points[j][0];
                y2 = points[j][1];
                int yDiff = y2 - y1;
                int xDiff = x2 - x1;
                int gcd = gcd(xDiff, yDiff);
                int dx = xDiff / gcd;
                int dy = yDiff / gcd;
                if(!counts.containsKey(dx)) {counts.put(dx, new HashMap<>());}
                counts.get(dx).put(dy, counts.get(dx).getOrDefault(dy, 0) + 1);
                max = Math.max(max, counts.get(dx).get(dy));
            }
        }
        return max + 1;
    }
    
    private int gcd(int a, int b) {
        if (b == 0) {return a;}
        return gcd(b, a%b);
    }
}

你可能感兴趣的:(LeetCode刷题怪,leetcode)