<p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= points.length &lt;= 300</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    struct Myhash {
        size_t operator()(const vector<double>& vec) const {
            size_t digest = 0;
            auto f = hash<double>();
            for (const auto& v : vec) {
                size_t v_hash = f(v);
                digest ^= v_hash + 0x9e3779b9 + (digest << 6) + (digest >> 2);
            }
            return digest;
        }
    };
    unordered_map<vector<double>, unordered_set<int>, Myhash> L;

    void line(vector<vector<int>>& points, int i, int j) {
        double x0 = points[i][0], y0 = points[i][1];
        double x1 = points[j][0], y1 = points[j][1];
        double dy = y1 - y0;
        double dx = x1 - x0;
        const double inf=1.0/0;//new version C++ spports
        double m, c;
        if (dx==0){//x=d
            m=inf;
            c=x0;
        }
        else{//dx!=0
            m = dy/dx;
            c = (x0 * dy - y0 * dx)/dx;
        }       
        
    //    cout<<"("<<x0<<","<<y0<<"),("<<x1<<","<<y1<<")\n";
        vector<double> lineKey={m, c};
    //    cout<<lineKey<<endl;
        L[lineKey].insert(i);
        L[lineKey].insert(j);
    //    cout<<L[lineKey].size()<<"\n----\n";
    }

    int maxPoints(vector<vector<int>>& points) {
        int n = points.size();
        if (n<=2) return n;// edge case;
        for (int i = 0; i < n-1; i++) {
            for (int j = i+1; j < n; j++) {
                line(points, i, j);
            }
        }

        int ans = 0;
        for (auto& [_, set] : L) {
            int v = set.size();
            ans = max(ans, v);
        }

        return ans;
    }
};
48416