1. 程式人生 > >查詢表類演算法//直線上最多的點數

查詢表類演算法//直線上最多的點數

給定一個二維平面,平面上有 個點,求最多有多少個點在同一條直線上。

示例 1:

輸入: [[1,1],[2,2],[3,3]]
輸出: 3
解釋:
^
|
|        o
|     o
|  o  
+------------->
0  1  2  3  4

示例 2:

輸入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
輸出: 4
解釋:
^
|
|  o
|     o        o
|        o
|  o        o
+------------------->
0  1  2  3  4  5  6
/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
class Solution {
    public int maxPoints(Point[] points) {
		if(points.length == 0) {
			return 0;
		}
		int[] count = new int[points.length];
		for (int i = 0; i < points.length; i++) {
			count[i] = 1;
			int size = 1;
			int same = 0;
			HashMap<Integer[], Integer> hashMap = new HashMap<>();
			for (int j = 0; j < points.length; j++) {
				if(i != j) {
					if(points[i].x != points[j].x) {
						int dy = points[i].y - points[j].y;
						int dx = points[i].x - points[j].x; 
						int gcd = generateGCD(dy, dx);
						if(gcd != 0) {
							dy = dy / gcd;
							dx = dx / gcd;
						}
						Integer[] nums = new Integer[2];
						nums[0] = dy;
						nums[1] = dx;
						boolean flag = false;
						for (Integer[] array : hashMap.keySet()) {
							if(nums[0] == array[0] && nums[1] == array[1]) {
								flag = true;
								hashMap.put(array, hashMap.get(array) + 1);
							}
						}
						if(!flag) {
							hashMap.put(nums, 1);
						}
					}else {
						if(points[i].y == points[j].y) {
							same++;
						}
						size++;
					}
				}	
			}
			for (Integer[] array : hashMap.keySet()) {
				if(hashMap.get(array) + 1 > count[i]) {
					count[i] = hashMap.get(array) + 1;
				}
			}
			count[i] += same;
			count[i] = Math.max(count[i], size);
		}
		int maxIndex = 0;
		for (int i = 1; i < count.length; i++) {
			if(count[i] > count[maxIndex]) {
				maxIndex = i;
			}
		}
		return count[maxIndex];
	}
 
	// 歐幾里得演算法:計算最大公約數
	private int generateGCD(int x, int y) {
		if (y == 0)
			return x;
		return generateGCD(y, x % y);
	}

}
/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */


class Solution {
public:
    /*
     * @param points: an array of point
     * @return: An integer
     */
    int maxPoints(vector<Point> &points) {
        // write your code here
        int res = 0;
        
        for(int i=0; i<points.size(); i++){
            map<pair<int, int>, int> m;
            int common = 1;//記錄相同點的個數
            for(int j = i+1; j<points.size(); j++){
                //處理相同的點
                if(points[i].x==points[j].x && points[i].y==points[j].y){
                    common++;
                    continue;
                }
                //處理不同的點
                int distance_x = points[i].x - points[j].x;//斜率有正有負
                int distance_y = points[i].y - points[j].y;
                int d = gcd(distance_x, distance_y);
                m[{distance_x/d, distance_y/d}]++;
                
            }
            res = max(res, common);//處理相同的點的個數
            //處理不同點的個數
            map<pair<int, int> ,int>::iterator it;
            for(it = m.begin(); it != m.end(); it++){
                res = max(res, it->second+common);
            }
        }
        return res;
    }    
    
    int gcd(int a, int b){//a,b最大公約數
        return (b==0) ? a : gcd(b, a%b);
    }
};

 

class Solution {
public:
    int maxPoints(vector<Point>& points) {
        int count = 0;
        for(int i = 0; i < points.size(); i++){
            map<pair<int,int>,int> m;
            int cnt = 0;
            int samePointCnt = 0;
            for(int j = i+1; j < points.size(); j++){
                if(points[i].x == points[j].x&&points[i].y==points[j].y)
                    samePointCnt++;
                else{
                    int xDiff = points[i].x - points[j].x;
                    int yDiff = points[i].y - points[j].y;
                    int g = gcd(xDiff,yDiff);
                    xDiff/=g;
                    yDiff/=g;
                    cnt = max(cnt,++m[make_pair(xDiff,yDiff)]);
                }
            }
            count = max(count,cnt+samePointCnt+1);
        }
        return count;
    }
private:
    int gcd(int a,int b){
        if(b == 0)
            return a;
        else
            return gcd(b,a%b);
    }
};