Programmer Interview Gold Code-Interview Question 16.14. Best Straight Line (Hash map + set)

1. Title

Given a two-dimensional plane and a list of N points on the plane, the coordinates of the i-th point are Points [i] = [Xi, Yi].
Please find a straight line that has the most points .

Set up to pass through the point of the straight line through all the points numbered in ascending sorted list is S, you only need to return [S [0], S [ 1]] as the answer
if a plurality of lines through the same number Point, select a straight line with a smaller S [0] value to return, and select a straight line with a smaller S [1] value to return if the S [0] is the same.

示例:
输入: [[0,0],[1,1],[1,0],[2,0]]
输出: [0,2]
解释: 所求直线穿过的3个点的编号为[0,2,3]

提示:
2 <= len(Points) <= 300
len(Points[i]) = 2

Source: LeetCode
Link: https://leetcode-cn.com/problems/best-line-lcci
Copyright belongs to Deduction Network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

2. Problem solving

  • Using nested hash map, the first layer key stores the slope, the second layer key stores the intercept, and the value is the set of points (storage subscript)
  • The slope does not exist. Open another hash table separately, the key is the intercept from the x axis, and the value is the point set
  • Traverse all collections to find the most
  • Sort the set of points of equal length and take out the smallest subscript required by the question
  • time complexity O ( n 2 ) O (n ^ 2)
class Solution {
public:
    vector<int> bestLine(vector<vector<int>>& points) {
    	int i, j, g, dx, dy, maxCount = 0, n = points.size();
    	double k, b;
    	unordered_map<double,unordered_map<double,set<int>>> m;//k,b,points
    	unordered_map<double,set<int>> v;//x轴截距,斜率不存在时的集合
        vector<set<int>> ans;
    	for(i = 0; i < n-1; ++i)
    	{
    		for(j = i+1; j < n; ++j)
    		{
    			dx = points[j][0]-points[i][0];
    			dy = points[j][1]-points[i][1];
    			if(dx==0)//斜率不存在
    			{
    				if(v[double(points[i][0])].empty())
    					v[double(points[i][0])].insert(i);
    				v[double(points[i][0])].insert(j);
    			}
    			else
    			{
    				k = double(dy)/dx;
    				b = double(points[i][1])-points[i][0]*k;
    				if(m[k][b].empty())
    					m[k][b].insert(i);
    				m[k][b].insert(j);
    			}
    		}
    	}
    	for(auto& mi : m)
    	{
    		for(auto& mii : mi.second)
    		{
                if(mii.second.size() > maxCount)
                {
                    maxCount = mii.second.size();
                    ans.clear();
                    ans.push_back(mii.second);
                }
                else if(mii.second.size() == maxCount)
                    ans.push_back(mii.second);
    		}
    	}
    	for(auto& vi : v)
    	{
    		if(vi.second.size() > maxCount)
			{
				maxCount = vi.second.size();
                ans.clear();
				ans.push_back(vi.second);
			}
            else if(vi.second.size() == maxCount)
                ans.push_back(vi.second);
    	}
        sort(ans.begin(),ans.end(),[&](auto a, auto b){
            auto it1 = a.begin(), it2 = b.begin();
            if(*it1 == *it2)
                return *(++it1) < *(++it2);
            return *it1 < *it2;
        });
        auto it = ans[0].begin();
    	return {*it,*(++it)};
    }
};

660 ms 117.7 MB

Published 845 original articles · Like 2187 · Visits 440,000+

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/105517540