<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:
#define unsigned long long ull
    int gcd(int a, int b) {
        int sign = 1;
        if (a < 0 || b < 0)
            sign = -1;
        a = abs(a), b = abs(b);
        if (a < b)
            swap(a, b);
        while (b) {
            // cout << a << " " << b << endl;
            a %= b;
            swap(a, b);
        }
        return sign * a;
    }
    int maxPoints(vector<vector<int>>& points) {
        unordered_map<uint64_t, unordered_map<int, int>> lines;
        unordered_map<int, int> verticals;
        unordered_map<int, int> horizontals;
        int n = points.size(), a, b, c, g, ans = 2;
        uint64_t key;
        auto cmp = [&](const vector<int>& a, const vector<int>& b) {
            return a[0] != b[0]? a[0] < b[0] : a[1] < b[1];
        };

        if (n == 1)
            return 1;
        sort(points.begin(), points.end(), cmp);
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                // vertical line
                if (points[i][0] == points[j][0]) {
                    if (verticals.find(points[i][0]) == verticals.end()) {
                        verticals[points[i][0]] = 2;
                    }
                    else
                        ++verticals[points[i][0]];
                    ans = max(ans, verticals[points[i][0]]);
                    continue;
                }
                // horizontal lines
                if (points[i][1] == points[j][1]) {
                    if (horizontals.find(points[i][1]) == horizontals.end())
                        horizontals[points[i][1]] = 2;
                    else
                        ++horizontals[points[i][1]];
                    ans = max(ans, horizontals[points[i][1]]);
                    continue;
                }
                // ax + by = c, a > 0
                a = points[i][1] - points[j][1];
                b = points[j][0] - points[i][0];
                if (a != 1 && b != 1) {
                    g = gcd(a, b);
                    // cout << a << " " << b << " " << g << endl;
                    a /= g, b /= g;
                }
                if (a < 0)
                    a = -a, b = -b;
                key = ((uint64_t)a << 32) | b;
                c = a * points[i][0] + b * points[i][1];
                // cout << "i: " << i << ", j: " << j << ", " << a << "x+" << b << "y=" << c << ", key: " << key << endl;
                if (lines.find(key) == lines.end())
                    lines[key] = unordered_map<int, int>();
                if (lines[key].find(c) == lines[key].end())
                    lines[key][c] = 2;
                else
                    ans = max(++lines[key][c], ans);
            }
            lines.clear();
            verticals.clear();
            horizontals.clear();
        }

        return ans;
    }
};
31278