Max Points on a Line

2014.2.26 18:13

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Solution1:

  A simple solution to this problem is O(n^3). Scan every pair of points and see if others are on the line with them. Simple but not efficient.

  The second solution will be more efficient, but requires hashing. For each point p, if there're k other points that can form lines with the same slope with this point, they're all on the same line. When recording the slopes, you need hashing to assist you.

  The hashing could choose the slope rate y/x as the hash key, but you know it would involve float point calculation, which is liable to accuracy problem.

  I chose to record the slop with a pair (x, y), where y/x means the slope rate. (0, 1) means vertical line to the x-axis, while 1/0 is not a valid slope rate. This way of representation is more universal, but requires a little bit more coding.

  I had intended to use <unordered_map> as the hashing tool, but the code wouldn't compile and run, as I've never used any user-defined type as hash key before. So I switched for <map> to solve the problem. The code is relatively simple, only a few things to remind you here:

    1. There're duplicate points.

    2. There're vertical lines.

    3. All coordinates are integers and you don't have to worry about integer overflow.

  Time complexity is O(n^2 * log(n)), space complexity is O(n).

Accepted code:

  1 // 8CE, 1AC, solution with <map>, I'll try <unordered_map> later.
  2 #include <map>
  3 #include <vector>
  4 using namespace std;
  5 
  6 /*
  7 struct Point {
  8     int x;
  9     int y;
 10     Point() : x(0), y(0) {}
 11     Point(int a, int b) : x(a), y(b) {}
 12 };
 13 */
 14 
 15 struct st {
 16     int x;
 17     int y;
 18     st(int _x = 0, int _y = 0): x(_x), y(_y) {};
 19 
 20     bool operator == (const st &other) const {
 21         return x == other.x && y == other.y;
 22     }
 23 
 24     bool operator != (const st &other) const {
 25         return x != other.x || y != other.y;
 26     }
 27 
 28     bool operator < (const st &other) const {
 29         if (x == other.x) {
 30             return y < other.y;
 31         } else {
 32             return x < other.x;
 33         }
 34     }
 35 };
 36 
 37 class Solution {
 38 public:
 39     int maxPoints(vector<Point> &points) {
 40         int n = (int)points.size();
 41         
 42         if (n <= 2) {
 43             return n;
 44         }
 45         
 46         map<st, int> um;
 47         st tmp;
 48         // special tangent value for duplicate points
 49         st dup(0, 0);
 50         
 51         int i, j;
 52         map<st, int>::const_iterator umit;
 53         int dup_count;
 54         int max_count;
 55         int result = 0;
 56         for (i = 0; i < n; ++i) {
 57             for (j = i + 1; j < n; ++j) {
 58                 tmp.x = points[j].x - points[i].x;
 59                 tmp.y = points[j].y - points[i].y;
 60                 // make sure one tangent value has one representation only.
 61                 normalize(tmp);
 62                 if (um.find(tmp) != um.end()) {
 63                     ++um[tmp];
 64                 } else {
 65                     um[tmp] = 1;
 66                 }
 67             }
 68             max_count = dup_count = 0;
 69             for (umit = um.begin(); umit != um.end(); ++umit) {
 70                 if (umit->first != dup) {
 71                     max_count = (umit->second > max_count ? umit->second : max_count);
 72                 } else {
 73                     dup_count = umit->second;
 74                 }
 75             }
 76             max_count = max_count + dup_count + 1;
 77             result = (max_count > result ? max_count : result);
 78             um.clear();
 79         }
 80         
 81         return result;
 82     }
 83 private:
 84     void normalize(st &tmp) {
 85         if (tmp.x == 0 && tmp.y == 0) {
 86             // (0, 0)
 87             return;
 88         }
 89         if (tmp.x == 0) {
 90             // (0, 1)
 91             tmp.y = 1;
 92             return;
 93         }
 94         if (tmp.y == 0) {
 95             // (1, 0)
 96             tmp.x = 1;
 97             return;
 98         }
 99         if (tmp.x < 0) {
100             // (-2, 3) and (2, -3) => (2, -3)
101             tmp.x = -tmp.x;
102             tmp.y = -tmp.y;
103         }
104         
105         int gcd_value;
106         
107         gcd_value = (tmp.y > 0 ? gcd(tmp.x, tmp.y) : gcd(tmp.x, -tmp.y));
108         // (4, -6) and (-30, 45) => (2, -3)
109         // using a double precision risks in accuracy
110         // so I did it with a pair
111         tmp.x /= gcd_value;
112         tmp.y /= gcd_value;
113     }
114     
115     int gcd(int x, int y) {
116         // used for reduction of fraction
117         return y ? gcd(y, x % y) : x;
118     }
119 };

Solution2:

  This is the version using <unordered_map>.

  Time complexity is O(n^2), space complexity is O(n).

  If you're not familiar with the usage of functor, perhaps you might see the code below for reference.

Accepted code:

  1 // 1CE, 1AC, unordered_map with user-defined key.
  2 #include <unordered_map>
  3 #include <vector>
  4 using namespace std;
  5 /*
  6 struct Point {
  7     int x;
  8     int y;
  9     Point() : x(0), y(0) {}
 10     Point(int a, int b) : x(a), y(b) {}
 11 };
 12 */
 13 struct st {
 14     int x;
 15     int y;
 16     st(int _x = 0, int _y = 0): x(_x), y(_y) {};
 17 
 18     bool operator == (const st &other) const {
 19         return x == other.x && y == other.y;
 20     };
 21 
 22     bool operator != (const st &other) const {
 23         return x != other.x || y != other.y;
 24     };
 25 };
 26 
 27 struct hash_functor {
 28     size_t operator () (const st &a) const {
 29         return (a.x * 1009 + a.y);
 30     }
 31 };
 32 
 33 struct compare_functor {
 34     bool operator () (const st &a, const st &b) const {
 35         return (a.x == b.x && a.y == b.y);
 36     }
 37 };
 38 
 39 typedef unordered_map<st, int, hash_functor, compare_functor> st_map;
 40 
 41 class Solution {
 42 public:
 43     int maxPoints(vector<Point> &points) {
 44         int n = (int)points.size();
 45         
 46         if (n <= 2) {
 47             return n;
 48         }
 49         
 50         st_map um;
 51         st tmp;
 52         // special tangent value for duplicate points
 53         st dup(0, 0);
 54         
 55         int i, j;
 56         st_map::const_iterator umit;
 57         int dup_count;
 58         int max_count;
 59         int result = 0;
 60         for (i = 0; i < n; ++i) {
 61             for (j = i + 1; j < n; ++j) {
 62                 tmp.x = points[j].x - points[i].x;
 63                 tmp.y = points[j].y - points[i].y;
 64                 // make sure one tangent value has one representation only.
 65                 normalize(tmp);
 66                 if (um.find(tmp) != um.end()) {
 67                     ++um[tmp];
 68                 } else {
 69                     um[tmp] = 1;
 70                 }
 71             }
 72             max_count = dup_count = 0;
 73             for (umit = um.begin(); umit != um.end(); ++umit) {
 74                 if (umit->first != dup) {
 75                     max_count = (umit->second > max_count ? umit->second : max_count);
 76                 } else {
 77                     dup_count = umit->second;
 78                 }
 79             }
 80             max_count = max_count + dup_count + 1;
 81             result = (max_count > result ? max_count : result);
 82             um.clear();
 83         }
 84         
 85         return result;
 86     }
 87 private:
 88     void normalize(st &tmp) {
 89         if (tmp.x == 0 && tmp.y == 0) {
 90             // (0, 0)
 91             return;
 92         }
 93         if (tmp.x == 0) {
 94             // (0, 1)
 95             tmp.y = 1;
 96             return;
 97         }
 98         if (tmp.y == 0) {
 99             // (1, 0)
100             tmp.x = 1;
101             return;
102         }
103         if (tmp.x < 0) {
104             // (-2, 3) and (2, -3) => (2, -3)
105             tmp.x = -tmp.x;
106             tmp.y = -tmp.y;
107         }
108         
109         int gcd_value;
110         
111         gcd_value = (tmp.y > 0 ? gcd(tmp.x, tmp.y) : gcd(tmp.x, -tmp.y));
112         // (4, -6) and (-30, 45) => (2, -3)
113         // using a double precision risks in accuracy
114         // so I did it with a pair
115         tmp.x /= gcd_value;
116         tmp.y /= gcd_value;
117     }
118     
119     int gcd(int x, int y) {
120         // used for reduction of fraction
121         return y ? gcd(y, x % y) : x;
122     }
123 };

 

 posted on 2014-02-26 18:48  zhuli19901106  阅读(396)  评论(0编辑  收藏  举报