<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> sorted in <strong>non-decreasing&nbsp;order</strong> and an integer <code>k</code>.</p>

<p>Define a pair <code>(u, v)</code> which consists of one element from the first array and one element from the second array.</p>

<p>Return <em>the</em> <code>k</code> <em>pairs</em> <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> <em>with the smallest sums</em>.</p>

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

<pre>
<strong>Input:</strong> nums1 = [1,7,11], nums2 = [2,4,6], k = 3
<strong>Output:</strong> [[1,2],[1,4],[1,6]]
<strong>Explanation:</strong> The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [1,1,2], nums2 = [1,2,3], k = 2
<strong>Output:</strong> [[1,1],[1,1]]
<strong>Explanation:</strong> The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
</pre>

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

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>nums1</code> and <code>nums2</code> both are sorted in <strong>non-decreasing order</strong>.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
	<li><code>k &lt;=&nbsp;nums1.length *&nbsp;nums2.length</code></li>
</ul>

class Solution {
#ifdef FIND_BY_BINARY_SEARCH
	int countLessOrEqual(vector<int>& nums1, vector<int>& nums2, long long mid) {
		int count = 0;

    	for (int i = 0; i < (int) nums1.size(); i++) {
    		int j = (int) nums2.size() - 1;
    		while (j >= 0 && nums1[i] + nums2[j] > mid) {
    			j--;
    		}
    		count += j + 1;
    	}

    	return count;
	}

public:
    vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
    	long long left = nums1[0] + nums2[0];
    	long long right = nums1.back() + nums2.back();
    	long long mid;
    	int count;
    	vector<vector<int>> ans1;
    	vector<vector<int>> ans2;

    	while (left < right) {
    		mid = left + (right - left) / 2;
    		count = countLessOrEqual(nums1, nums2, mid);
    		if (count < k) {
    			left = mid + 1;
    		} else { // count >= k
    			right = mid;
    		}
    	}
    	mid = left;

    	for (int i = 0; i < (int) nums1.size(); i++) {
    		int j = 0;
    		int size = (int) nums2.size();
    		while (j < size && nums1[i] + nums2[j] <= mid) {
    			if (nums1[i] + nums2[j] < mid)
    				ans1.push_back(vector<int>{ nums1[i], nums2[j] });
    			else
    				ans2.push_back(vector<int>{ nums1[i], nums2[j] });
    			j++;
    		}
    	}

    	int i = 0;
    	while ((int) ans1.size() < k) {
    		ans1.push_back(ans2[i]);
    		i++;
    	}

    	return ans1;
    }
#endif

	struct myCompare {
		bool operator()(const vector<int>& lhs, const vector<int>& rhs) {
			return lhs[0] > rhs[0]; // minHeap
		}
	};

	priority_queue<vector<int>,vector<vector<int>>, myCompare> pq;

	struct myHash {
		std::size_t operator()(const vector<int>& vec) const noexcept {
			std::hash<int> hasher;

			return hasher(vec[0]) ^ (hasher(vec[1]) << 1);
		}
	};

	unordered_set<vector<int>, myHash> set;

public:
    vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
    	vector<vector<int>> ans;
    	int col;
    	int row;

    	pq.push(vector<int>{ nums1[0] + nums2[0], 0, 0 });

    	while (pq.size() > 0 && (int) ans.size() < k) {
    		vector<int> top = pq.top();
    		pq.pop();

    		ans.push_back(vector<int>{ nums1[top[1]], nums2[top[2]] });

    		if (top[1] + 1 < (int) nums1.size()) {
    			col = top[1] + 1;
    			row = top[2];
    			if (set.count(vector<int> { col, row }) == 0) {
            		pq.push(vector<int>{ nums1[col] + nums2[row], col, row });
            		set.insert(vector<int> { col, row });
    			}
    		}
    		if (top[2] + 1 < (int) nums2.size()) {
    			col = top[1];
    			row = top[2] + 1;
    			if (set.count(vector<int> { col, row }) == 0) {
            		pq.push(vector<int>{ nums1[col] + nums2[row], col, row });
            		set.insert(vector<int> { col, row });
    			}
    		}
    	}

    	return ans;
    }
};
165166