Can someone help me to improve my solution, so that I can avoid timeout errors?


  • 0
    P
    /**
     * Definition for a point.
     * class Point {
     *     int x;
     *     int y;
     *     Point() { x = 0; y = 0; }
     *     Point(int a, int b) { x = a; y = b; }
     * }
     */
    import java.util.*;
    
    public class Solution {
        class Line{
        float a;
        float b;
        Line(){a=0.0f; b =0.0f;}
        Line(float a1,float b1){
        a=a1;
        b=b1;
        }
    }
        public int maxPoints(Point[] points) {
            Hashtable<Line,Integer>ht = new Hashtable<Line,Integer>();
            ArrayList test = null;
            int max =0 ;
            for(int i=0;i<points.length;i++){
                for(int j=i+1;j<points.length;j++){
                    int diff = points[j].x - points[i].x;
                    if(diff!=0)float m = (points[j].y - points[i].y)/diff;
                    float c = points[i].y - m*points[i].x;
                    Line l1 =new Line(m,c);
                    if(ht.get(l1)!=null){
                        int count = ht.get(l1);
                        count++;
                        ht.put(l1,count);
                        if(count>max) max = count;
                        //System.out.println("yo"+count);
                    }
                    else ht.put(l1,1);
                }
            }
            return max;
        }
    }

Log in to reply
 

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