<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:
    
    pair<int, int> reduce_frac(int num, int denom) {
        int reduction = __gcd(abs(num), abs(denom));
        int factor = 1;
        if ((num > 0 && denom < 0) || (num < 0 && denom > 0)) {
            factor *= -1;
        }
        num = abs(num) / reduction;
        denom = abs(denom) / reduction;
        num *= factor;
        return make_pair(num, denom);
    }
    
    pair<int, int> sum_fracs(pair<int,int> frac1, pair<int,int> frac2) {
        return reduce_frac(
            frac1.first * frac2.second + frac2.first * frac1.second,
            frac1.second * frac2.second
        );
    }
    array<int, 4> slope_and_intercept(vector<int> p1, vector<int> p2) {
        pair<int,int> slope = reduce_frac(p2[1] - p1[1], p2[0] - p1[0]);
        pair<int,int> intercept = sum_fracs(
            make_pair(-1 * slope.first * p1[0], slope.second),
            make_pair(p1[1], 1)
        );
        return {slope.first, slope.second, intercept.first, intercept.second};
        
    }
    int maxPoints(vector<vector<int>>& points) {
        map<array<int,4>, set<int>> yay;
        map<int, set<int>> horizontal;
        map<int, set<int>> vertical;
        if (points.size() == 1) {
            return 1;
        }
        for (int i = 0; i < points.size(); i++) {
            for(int j = i+1; j < points.size(); j++) {
                // account for flat and horizontal separately
                if (points[i][0] == points[j][0]) {
                    // vertical
                    vertical[points[i][0]].insert(i);
                    vertical[points[j][0]].insert(j);
                } else if (points[i][1] == points[j][1]) {
                    horizontal[points[i][1]].insert(i);
                    horizontal[points[j][1]].insert(j);
                } else {
                    yay[slope_and_intercept(points[i], points[j])].insert(i);
                    yay[slope_and_intercept(points[i], points[j])].insert(j);
                }
            }
        }
        int best = 0;
        for (auto& it : yay) {
            best = max((int) it.second.size(), best);
        }
        for (auto& it : vertical) {
            best = max((int) it.second.size(), best);
        }
        for (auto& it : horizontal) {
            best = max((int) it.second.size(), best);
        }
        return best;
        
    }
};
43846