Share my java solution


  • 0
    F
    Map<Double, Integer> map = new HashMap<>();
    
    public boolean isSame(Point point1, Point point2) {
        return point1.x == point2.x && point1.y == point2.y;
    }
    
    public double slope(Point point1, Point point2) {
        if (point1.x == point2.x) {
            return Double.MAX_VALUE;
        } else {
            return 1.0 * (point1.y - point2.y) / (point1.x - point2.x);
        }
    }
    
    public int maxPoints(Point[] points) {
        int n = points.length;
        int result = 0;
        for (int i = 0; i < n; i++) {
            map.clear();
            int duplicate = 0;
            for (int j = 0; j < n; j++) {
                if (isSame(points[i], points[j])) {
                    duplicate++;
                } else {
                    double key = slope(points[i], points[j]);
                    map.put(key, map.getOrDefault(key, 0) + 1);
                }
            }
            int maxValue = map.values().size() == 0 ? 0 : Collections.max(map.values());
            result = Math.max(maxValue + duplicate, result);
        }
        return result;
    }

Log in to reply
 

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