<p>You have <code>k</code> lists of sorted integers in <strong>non-decreasing&nbsp;order</strong>. Find the <b>smallest</b> range that includes at least one number from each of the <code>k</code> lists.</p>

<p>We define the range <code>[a, b]</code> is smaller than range <code>[c, d]</code> if <code>b - a &lt; d - c</code> <strong>or</strong> <code>a &lt; c</code> if <code>b - a == d - c</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
<strong>Output:</strong> [20,24]
<strong>Explanation: </strong>
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [[1,2,3],[1,2,3],[1,2,3]]
<strong>Output:</strong> [1,1]
</pre>

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

<ul>
	<li><code>nums.length == k</code></li>
	<li><code>1 &lt;= k &lt;= 3500</code></li>
	<li><code>1 &lt;= nums[i].length &lt;= 50</code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i][j] &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code>&nbsp;is sorted in <strong>non-decreasing</strong> order.</li>
</ul>

class Solution {
public:
    vector<int> smallestRange(vector<vector<int>>& nums) {
        vector<pair<int, int>> merged_list = merge(nums);
        // printMerged(merged_list);
        int left = 0, right = 0;
        int n = merged_list.size();
        int num_lists = nums.size();
        int unique_count = 0;
        vector<int> answer = {merged_list[0].first, merged_list[n - 1].first};
        
        vector<int> hash_table(nums.size(), 0);
        while(right < n)
        {
            hash_table[merged_list[right].second]++;
            if (hash_table[merged_list[right].second] == 1)
                unique_count++;
            while (unique_count == num_lists && left < right)
            {
                compare(answer, {merged_list[left].first, merged_list[right].first});
                hash_table[merged_list[left].second]--;
                if (hash_table[merged_list[left].second] == 0)
                    unique_count--;
                left++;
            }
            right++;
        }
        return answer;
    }

    void printVector(vector<int>& v)
    {
        for(int i = 0; i < v.size(); i++)
            cout << v[i] << " ";
        cout << endl;
    }

    void printMerged(vector<pair<int, int>>& merged_list)
    {
        for(int i = 0; i < merged_list.size(); i++)
        {
            cout << "(" << merged_list[i].first << ", " << merged_list[i].second << ") "; 
        }
        cout << endl;
    }

    void compare(vector<int>& answer, vector<int> new_possibility)
    {
        // cout << "(" << answer[0] << ", " << answer[1] << ") " << endl; 
        // cout << "(" << new_possibility[0] << ", " << new_possibility[1] << ") " << endl; 
        if (new_possibility[1] - new_possibility[0] > answer[1] - answer[0])
            return;
        else if (new_possibility[1] - new_possibility[0] < answer[1] - answer[0])
        {
            answer = new_possibility;
        }
        else if (answer[0] > new_possibility[0])
        {
            answer = new_possibility;
        }
        return;
    }

    vector<pair<int, int>> merge(vector<vector<int>>& nums)
    {
        int n = nums.size();
        vector<pair<int, int>> merged_list;
        vector<int> indices(n, 0);
        priority_queue<pair<int,int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        for(int i = 0; i < n; i++)
            pq.push(make_pair(nums[i][0], i));
        
        int idx;
        while(!pq.empty())
        {
            merged_list.push_back(pq.top());
            idx = pq.top().second;
            pq.pop();
            
            indices[idx]++;
            if (indices[idx] < nums[idx].size())
            {
                pq.push(make_pair(nums[idx][indices[idx]], idx));
            }
        }
        return merged_list;
    }
};
25321