<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>

#include <vector>
#include <unordered_map>
#include <memory>
#include <cmath>

//nije dobro ak je a==b (0) ili ak se dogodi slucaj hash(a,b) i hash(b,a) jer su isti
class XorPairHash {
public:
    template <class _T1, class _T2>
    std::size_t operator()(const std::pair<_T1, _T2>& p) const {
        return std::hash<float>()(p.first) ^ std::hash<float>()(p.second);
    }
};

class Solution {
private:
    //ax+b, bucketing
    std::unordered_map<std::pair<float, float>, int, XorPairHash> aib;

public:
    int maxPoints(std::vector<std::vector<int>>& points) {
        int max = 0;
        aib.reserve(points.size() * (points.size() - 1) / 2);
        for (std::size_t i = 0; i < points.size(); i++) {
            for (std::size_t j = i + 1; j < points.size(); j++) {
                auto ref = ++aib[calcAiB(points[i], points[j])];
                if (ref > max) {
                    max = ref;
                }
            }
        }

        //this is because we overcount, max is real choose 2 so this is solving for real, x^2 - x - 2 max = 0 and we know x is a natural number
        return (1 + static_cast<int>(std::sqrt(8 * max + 1))) / 2;
    }

    std::pair<float, float> calcAiB(const std::vector<int>& point1, const std::vector<int>& point2) const {
        float a = (static_cast<float>(point1[1]) - static_cast<float>(point2[1])) / (static_cast<float>(point1[0]) - static_cast<float>(point2[0]));
        [[unlikely]] if (std::isinf(a)) {
            return std::make_pair(static_cast<float>(1) / static_cast<float>(0), point1[0]);
        }
        auto temp = std::make_pair(a, round((static_cast<float>(point1[1]) - a * static_cast<float>(point1[0])) * 1000.0f));
        return temp;
    }
};
15283