java - Find the maximum number of points that lie on the same straight line -


given n points on 2d plane, find maximum number of points lie on same straight line.

this programming puzzle taken here on leetcode

below attempt solve it.

   /**      * definition point.      * class point {      *     int x;      *     int y;      *     point() { x = 0; y = 0; }      *     point(int a, int b) { x = a; y = b; }      * }      */     public class solution {         public int maxpoints(point[] points) {             if (points.length==0) return 0;             map[] maps = new map[points.length];             (int i=0; i<points.length; i++) {                 map<double, integer> map = new hashmap<double, integer>();                 maps[i] = map;             }             //count points same coordinates             int [] identical = new int[points.length];              //count points form vertical line against x-axis             int [] vertical = new int[points.length];             (int i=0; i<points.length; i++) {                 (int j=i+1; j<points.length; j++) {                     point = points[i];                     point b = points[j];                     if (a.x != b.x) {                         double slope = (a.y-b.y)/(a.x-b.x);                         if (maps[i].containskey(slope)) {                             maps[i].put(slope, (int)maps[i].get(slope)+1);                         } else {                             maps[i].put(slope, 1);                         }                         if (maps[j].containskey(slope)) {                             maps[j].put(slope, (int)maps[j].get(slope)+1);                         } else {                             maps[j].put(slope, 1);                         }                     } else if (a.y == b.y) {                         identical[i]++;                         identical[j]++;                     } else {                         vertical[i]++;                         vertical[j]++;                     }                 }             }             int max = 0;             (int i=0; i<points.length; i++) {                 int maxforcurrentpoint = vertical[i];                 (object entry : maps[i].entryset())                 {                     int num = (int)((map.entry)entry).getvalue();                     if (num > maxforcurrentpoint) {                         maxforcurrentpoint = num;                     }                 }                 maxforcurrentpoint += identical[i]+1; //the 1 counts point                 if (maxforcurrentpoint > max)                 max = maxforcurrentpoint;             }             return max;         }     } 

however, not able pass test cases. testing results follows:

19 / 27 test cases passed.

input: [[84,250],[0,0],[1,0],[0,-70],[0,-70],[1,-1],[21,10],[42,90],[-42,-230]] output: 8 expected: 6

the logic of code seems okay me, perhaps i'm missing i'm not aware of? have doubt usage of hashtable here. please shed light on problem?

the line

double slope = (a.y-b.y)/(a.x-b.x); 

is wrong. int division, , converts result double. want

double slope = (a.y-b.y)/(double)(a.x-b.x); 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -