<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) {
        // Constants for large initial values
        constexpr int kMinValue = std::numeric_limits<int>::min() >> 1;
        constexpr int kMaxValue = std::numeric_limits<int>::max() >> 1;

        // Initialize the range with large values
        std::vector<int> result_range = {kMinValue, kMaxValue};
        std::map<int, std::vector<std::pair<int, int>>> index_map;
        const int num_lists = nums.size();

        // Populate the map with the first elements from each list
        for (int i = 0; i < num_lists; ++i) {
            index_map[nums[i][0]].emplace_back(i, 0);
        }

        // Continue until we can't process more elements
        while (true) {
            auto lo = index_map.begin();
            auto hi = index_map.rbegin();

            // Update the result range if a smaller one is found
            if ((hi->first - lo->first) < (result_range[1] - result_range[0])) {
                result_range = {lo->first, hi->first};
            }

            // Process the list corresponding to the smallest element
            for (const auto& [list_idx, element_idx] : lo->second) {
                int next_element_idx = element_idx + 1;
                
                // Skip duplicate consecutive elements
                while (next_element_idx < nums[list_idx].size() && 
                       nums[list_idx][element_idx] == nums[list_idx][next_element_idx]) {
                    ++next_element_idx;
                }
                
                // If we've reached the end of one list, return the result
                if (next_element_idx >= nums[list_idx].size()) {
                    return result_range;
                }

                // Add the next element from this list to the map
                index_map[nums[list_idx][next_element_idx]].emplace_back(list_idx, next_element_idx);
            }

            // Remove the processed smallest element
            index_map.erase(lo);
        }

        return result_range;  // In case of an edge case, though we should return earlier.
    }
};
23361