AC JAVA solution


  • 0
    J
    public class Solution {
    public int maxPoints(Point[] points) {
    	if (points.length == 0) {
    		return 0;
    	}
    	if (points.length == 1) {
    		return 1;
    	}
    	HashMap<Double, Integer> slopemap = new HashMap<Double, Integer>();
    	int max = 1;
    	int result = 1;
    	double slope = 0;
    	int extra = 0;
    	for (int j = 0; j < points.length; j++) {
    		max = 1;
    		slope = 0;
    		extra = 0;
    		Point origin = points[j];
    		slopemap.clear();
    		for (int i = 0; i < points.length; i++) {
    			if (i != j) {
    				if (points[i].x == origin.x && points[i].y == origin.y) {
    					extra++;
    				} else {
    					if (origin.x == points[i].x) {
    						slope = 123.123123;
    					} else {
    						slope = (double) (origin.y - points[i].y) / (double) (origin.x - points[i].x);
    					}
    					if (slopemap.keySet().contains(slope)) {
    						int temp = slopemap.get(slope) + 1;
    						slopemap.put(slope, temp);
    						max = Math.max(max, temp);
    					} else {
    						slopemap.put(slope, 2);
    						max = Math.max(max, 2);
    					}
    				}
    			}
    		}
    		max += extra;
    		result = Math.max(result, max);
    	}
    
    	return result;
    }
    

    }


Log in to reply
 

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