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

    #define endl "\n"
    
    typedef long long ll;
    typedef long double ld;
    typedef vector<int> vi;
    typedef pair<int, int> pi;
    typedef pair<ll, ll> pll;
    typedef vector<ll> vll;
    typedef vector<pi> vpi;
    typedef vector<pll> vpll;
    typedef vector<vll> vvll;
    typedef vector<vi> vvi;
    typedef vector<double> vd;
    typedef vector<vector<double>> vvd;
    typedef vector<ld> vld;
    typedef vector<vld> vvld;
    typedef vector<bool> vb;
    typedef vector<vb> vvb;
    typedef vector<vpi> vvpi;
    typedef vector<vvll> vvvll;
    typedef vector<string> vstr;
    typedef vector<vstr> vvstr;
    typedef vector<char> vch;
    typedef vector<vch> vvch;


    #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
    #define repi(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
    #define pb push_back
    #define fi first
    #define se second
    #define all(a) a.begin(), a.end()
    #define sz(a) (int)(a.size())

    
    string to_string(const string& s) {
        return '"' + s + '"';
    }
    
    string to_string(const char* s) {
        return to_string((string) s);
    }
    
    string to_string(const char s) {
        return string(1, s);
    }
    string to_string(bool b) {
        return (b ? "true" : "false");
    }
    
    string to_string(vector<bool> v) {
        bool first = true;
        string res = "{";
        for (int i = 0; i < static_cast<int>(v.size()); i++) {
            if (!first) {
            res += ", ";
            }
            first = false;
            res += to_string(v[i]);
        }
        res += "}";
        return res;
    }
    
    template <size_t N>
    string to_string(bitset<N> v) {
        string res = "";
        for (size_t i = 0; i < N; i++) {
            res += static_cast<char>('0' + v[i]);
        }
        return res;
    }
    
    template <typename A, typename B>
        string to_string(pair<A, B> p) {
        return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
    }
    
    template <typename A, typename B, typename C>
        string to_string(tuple<A, B, C> p) {
        return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
    }
    
    template <typename A, typename B, typename C, typename D>
        string to_string(tuple<A, B, C, D> p) {
        return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
    }

    template <typename A>
    string to_string(A v) {
        bool first = true;
        string res = "{";
        for (const auto &x : v) {
            if (!first) {
            res += ", ";
            }
            first = false;
            res += to_string(x);
        }
        res += "}";
        return res;
    }
    
    
    void debug_out() { cout << endl; }
    
    template <typename Head, typename... Tail>
    void debug_out(Head H, Tail... T) {
        cout << " " << to_string(H);
        debug_out(T...);
    }

    #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

    

class Solution {
public:
    vector<int> smallestRange(vector<vector<int>>& a) {
        // solution based on two pointers
        const int inf = 1e9;
        map<int, vi> mp;
        int n = a.size();
        rep(i, 0, n) {
            for (int x : a[i]) {
                mp[x].pb(i);
            }
        }

        int len = inf;
        int li, ri;
        map<int, int> freq;
        auto l = mp.begin(), r = mp.begin();
        for (int id : l->se) {
            freq[id]++;
        }

        while (true) {
            while (sz(freq) != n) {
                if (r != mp.end()) r++;
                else break;
                if (r == mp.end()) break;
                for (int id : r->se) {
                    freq[id]++;
                }
            }
            if (sz(freq) == n) {
                int curli = l->fi, curri = r->fi;
                int curlen = curri - curli + 1;
                if (curlen < len) {
                    len = curlen;
                    li = curli;
                    ri = curri;
                }
                for (int id : l->se) {
                    freq[id]--;
                    if (freq[id] == 0) {
                        freq.erase(id);
                    }
                }
                l++;
            } else {
                break;
            }
        }

        return vi({li, ri});
    }
};
24096