<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:
    using Point = vector<int>;
    const int bitOffset = 32768;
    struct Direction 
    {
        int16_t X;
        int16_t Y;
        int Count = 2;  // Defaults to 2 because a line must have a minimum of 2 points to exist.
    };

    int maxPoints(vector<vector<int>>& points) {
        int numPoints = points.size();
        if (points.size() < 2) return numPoints;

        std::map<int, int> counts;
        int maxCount = 2;

        for (int i = 0; i < numPoints-1; i++)
        {
            vector<Direction> dirMap;
            Point& p1 = points[i];

            for (int j = i+1; j<numPoints; j++)
            {
                Point& p2 = points[j];
                Direction d;
                d.X = p2[0] - p1[0];
                d.Y = p2[1] - p1[1];
                
                int count = 2;  // Defaults to 2 because a line must have a minimum of 2 points to exist.
                for (Direction& existing : dirMap)
                {
                    int cross = (d.X*existing.Y) - (d.Y*existing.X);
                    
                    if (cross == 0)
                    {
                        // We have a match!
                        count = ++existing.Count;
                        if (count > maxCount) maxCount = count;
                        break;
                    }
                }

                if (count == 2)
                {
                    dirMap.push_back(d);
                }
            }

            if (numPoints - i - 1 <= maxCount) break;  // Avoid final iterations when we can't possibly find a larger set
        }

        return maxCount;
    }
};
11856