id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n map<int,int> mp;\n int f = 0;\n for(int i=0;i<arr.size();i++){\n if(mp.find(arr[i])!=mp.end()){\n mp[arr[i]]++;\n }\n else{\n mp[arr[i]] = 1;\n }\n }\n set<int> freq;\n for(auto it : mp){\n if(freq.find(it.second)!=freq.end()){\n return false;\n }\n freq.insert(it.second);\n }\n return true;\n }\n};",
"memory": "11300"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n map<int, int> mcount;\n set<int> scount;\n map<int, int>::iterator it = mcount.begin();\n for(int e : arr){\n if(mcount.find(e) == mcount.end()){\n mcount[e] = 1;\n }else{\n mcount[e]++;\n }\n }\n \n for(map<int, int>::iterator it = mcount.begin(); it != mcount.end(); it++){\n if(scount.find(it->second) == scount.end()){\n scount.insert(it->second);\n }else{\n return false;\n }\n }\n return true;\n }\n};",
"memory": "11400"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\tbool uniqueOccurrences(std::vector<int>& arr) {\n\t\tstd::map<int, int> dig_count;\n\t\tfor (auto num: arr)\n\t\t{\n\t\t\tdig_count[num]++;\n\t\t}\n\t\tstd::set<int> unic_count;\n\t\tfor (auto now: dig_count)\n\t\t{\n\t\t\tif(unic_count.count(now.second))\n\t\t\t\treturn false;\n\t\t\tunic_count.insert(now.second);\n\t\t}\n\t\treturn true;\n\t}\n};",
"memory": "11400"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n unordered_map<int, int> freqMap;\n unordered_set<int> freqSet;\n\n // Count the frequency of each element\n for (int num : arr) {\n freqMap[num]++;\n }\n\n // Check if the frequency counts are unique\n for (const auto& pair : freqMap) {\n if (freqSet.find(pair.second) != freqSet.end()) {\n return false; // Duplicate frequency found\n }\n freqSet.insert(pair.second);\n }\n\n return true;\n }\n};",
"memory": "11500"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n unordered_map<int,int> mp; \n for(int i:arr){\n mp[i]++;\n }\n vector<int>arr1;\n for(auto i:mp){\n arr1.push_back(i.second);\n }\n sort(arr1.begin(),arr1.end());\n for(int i=0; i < arr1.size()-1; i++){\n if(arr1[i]==arr1[i+1]){\n return false;\n }\n }\n return true;\n }\n};",
"memory": "11500"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n unordered_map<int,int>mp;\n for(auto it:arr)mp[it]++;\n unordered_set<int>st;\n for(auto &[num,feq]:mp){\n if(st.find(feq)!=st.end())return false;\n st.insert(feq);\n }\n return true;\n }\n};",
"memory": "11600"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n map<int,int>cnt;\n for(auto u:arr){\n cnt[u]++;\n }\n int i=0;\n int sz=cnt.size();\n multiset<int>s;\n for(auto u:cnt){\n s.insert(u.second);\n }\n int j=0;\n for(auto u:s){\n arr[j++]=u;\n }\n i=0;\n for(i=0;i<sz-1;i++){\n if(arr[i]==arr[i+1]){\n return false;\n }\n }\n return true;\n }\n};",
"memory": "11600"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n unordered_map<int,int>map;\n for(int i = 0; i <arr.size(); i++){\n map[arr[i]]++;\n } \n set<int>ans;\n for(auto it : map){\n if(ans.find(it.second)!=ans.end()){\n return false;\n }\n ans.insert(it.second);\n }\n return true;\n }\n};\n//Once we have the frequencies of all elements, the next task is to ensure that no two elements have the same frequency.\n//To do this, we can use a set (set<int>), which automatically keeps only unique values.\n//We iterate through the hash map (which contains the element frequencies) and check if the frequency is already present in the set:\n//If the frequency is already in the set, it means two elements share the same frequency, so we return false.\n//If not, we insert the frequency into the set and continue checking the rest of the frequencies.",
"memory": "11700"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n\tunordered_map<int, int> occurrenceCount;\n\tunordered_set<int> uniqueOccurrences;\n\tfor (int element: arr) {\n\t\tif (occurrenceCount.find(element)==occurrenceCount.end())\n\t\t\toccurrenceCount[element]=1;\nelse\n\toccurrenceCount[element]++;\n}\nfor(auto foundElement: occurrenceCount) {\n\tuniqueOccurrences.insert(foundElement.second);\n}\nreturn uniqueOccurrences.size()==occurrenceCount.size();\n }\n};",
"memory": "11700"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n unordered_map<int,int>mp;\n for(int i=0;i<arr.size();i++)\n {\n mp[arr[i]]++;\n }\n unordered_set<int> s;\n for(auto i:mp)\n {\n s.insert(i.second);\n }\n return s.size()==mp.size();\n\n }\n};",
"memory": "11800"
} |
1,319 | <p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2,2,1,1,3]
<strong>Output:</strong> true
<strong>Explanation:</strong> The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,2]
<strong>Output:</strong> false
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 1000</code></li>
<li><code>-1000 <= arr[i] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n\n unordered_map<int,int>m;\n unordered_set<int>us;\n \n for (int i = 0; i < arr.size(); i++) {\n m.insert({arr[i],count(arr.begin(), arr.end(), arr[i])});\n }\n\n for(auto key:m){\n int cou = key.second;\n\n if(us.count(cou)){\n return false;\n }\n else{\n us.insert(cou);\n }\n }\n return true;\n\n\n\n\n\n }\n};",
"memory": "11800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 0 | {
"code": "class Solution { // 20 ms, faster than 98.92%\npublic:\n int m, n;\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n m = matrix.size(), n = matrix[0].size(); // For general, the matrix need not be a square\n int left = matrix[0][0], right = matrix[m-1][n-1], ans = -1;\n while (left <= right) {\n int mid = (left + right) >> 1;\n if (countLessOrEqual(matrix, mid) >= k) {\n ans = mid;\n right = mid - 1; // try to looking for a smaller value in the left side\n } else left = mid + 1; // try to looking for a bigger value in the right side\n }\n return ans;\n }\n int countLessOrEqual(vector<vector<int>>& matrix, int x) {\n int cnt = 0, c = n - 1; // start with the rightmost column\n for (int r = 0; r < m; ++r) {\n while (c >= 0 && matrix[r][c] > x) --c; // decrease column until matrix[r][c] <= x\n cnt += (c + 1);\n }\n return cnt;\n }\n};",
"memory": "15900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n vector<int> ptrs(n, 0);\n\n //repeat this K times\n for (int x = 1; x <= k; ++x) {\n\n //index of row of smallest element\n int ind = 0;\n\n //\n int currMin = INT_MAX;\n for (int i = 0; i < n; ++i) {\n if (ptrs[i] < n && matrix[i][ptrs[i]] < currMin) {\n currMin = matrix[i][ptrs[i]];\n ind = i;\n }\n }\n \n ptrs[ind]++;\n \n if (x == k)\n return currMin;\n }\n return 0;\n }\n};",
"memory": "15900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countLessEqual(vector<vector<int>>& matrix, int mid, int n) {\n int count = 0;\n int row = n - 1; // Start from the bottom-left corner\n int col = 0;\n\n while (row >= 0 && col < n) {\n if (matrix[row][col] <= mid) {\n count += row + 1; // All elements in this column (up to this row) are <= mid\n col++;\n } else {\n row--; // Move up the row\n }\n }\n \n return count;\n}\n\nint kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int low = matrix[0][0];\n int high = matrix[n - 1][n - 1];\n\n while (low < high) {\n int mid = low + (high - low) / 2;\n int count = countLessEqual(matrix, mid, n);\n\n if (count < k) {\n low = mid + 1; // Increase the lower bound\n } else {\n high = mid; // Narrow the search range\n }\n }\n\n return low; // The kth smallest element\n}\n};",
"memory": "16000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 0 | {
"code": "// O(N * log(high) * log(N))\n// Binary search on answer\n// Count the number of elements lesser than mid\n\nclass Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n auto countOfNumsLesserThan = [&] (int mid) -> int {\n int res = 0;\n for(int i = 0; i < matrix.size(); i++) {\n int lb = lower_bound(matrix[i].begin(), matrix[i].end(), mid + 1) - matrix[i].begin();\n res += lb;\n }\n return res;\n };\n\n int low = matrix[0][0], high = matrix.back().back();\n while(low <= high) {\n int mid = low + (high - low) / 2;\n if(countOfNumsLesserThan(mid) >= k) {\n high = mid - 1;\n }\n else {\n low = mid + 1;\n }\n }\n return low;\n }\n};",
"memory": "16000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC target (\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx\")\n#pragma GCC optimize (\"-ffloat-store\")\n#pragma GCC optimize (\"O3\", \"unroll-loops\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\n\nclass Solution {\npublic:\n\n int count(vector<vector<int>>& matrix, int mid){\n int ans = 0;\n for (int i = 0; i < matrix.size(); i++){\n ans += upper_bound(matrix[i].begin(), matrix[i].end(), mid) - matrix[i].begin();\n }\n return ans;\n }\n\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int s = matrix[0][0];\n int e = matrix[n - 1][n - 1];\n\n while (s <= e){\n int mid = s + (e - s) / 2;\n if (count(matrix, mid) >= k){\n\n e = mid - 1;\n }\n else{\n s = mid + 1;\n }\n }\n return s;\n }\n};",
"memory": "16100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "#pragma GCC optimize(\"Ofast\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n struct Data {\n std::size_t row = 0;\n std::size_t col = 0;\n int value = 0;\n\n friend bool operator<(const Data& d1, const Data& d2) {\n return d1.value >= d2.value;\n }\n };\n\n int kthSmallest(std::vector<std::vector<int>>& matrix, int k) {\n const auto n = matrix.size();\n\n std::priority_queue<Data> pq;\n for (std::size_t r = 0; r < std::min<std::size_t>(n, k); ++r) {\n pq.push({ r, std::size_t(0), matrix[r][0] });\n }\n\n auto data = pq.top();\n while (k--) {\n data = pq.top();\n pq.pop();\n\n if (data.col != n - 1) {\n pq.push({ data.row, data.col + 1, matrix[data.row][data.col + 1] });\n }\n }\n\n return data.value;\n\n /*\n const auto n = matrix.size();\n\n auto compare = [&](const auto& a, const auto& b) {\n const auto v1 = matrix[a.first][a.second];\n const auto v2 = matrix[b.first][b.second];\n if (v1 == v2) {\n return a > b;\n }\n return v1 > v2;\n };\n\n using PairT = std::pair<std::size_t, std::size_t>;\n std::priority_queue<PairT, std::vector<PairT>, decltype(compare)> pq(compare);\n pq.push({0, 0});\n\n auto p = pq.top();\n while (k--) {\n p = pq.top();\n\n if (p.first != n - 1) {\n pq.push({p.first + 1, p.second});\n }\n\n if (p.second != n - 1) {\n pq.push({p.first, p.second + 1});\n }\n\n while (!pq.empty() && p == pq.top()) {\n pq.pop();\n }\n }\n\n return matrix[p.first][p.second];\n */\n }\n};\n\n/*\n\n[[1,8],[4,9]]\n1\n[[1,8],[4,9]]\n2\n[[1,8],[4,9]]\n3\n[[1,8],[4,9]]\n4\n[[-5]]\n1\n\n\n\n[[1,5,9],[10,11,13],[12,13,15]]\n8\n[[1,1,1],[2,2,2],[3,3,3]]\n8\n[[1,5,10],[4,8,20],[10,18,30]]\n7\n[[2,5,10],[2,6,11],[3,7,12]]\n7\n[[1,50,70],[2,60,80],[3,90,100]]\n5\n[[1,4,30],[2,20,40],[50,60,90]]\n6\n[[1,2,3],[4,5,6],[10,20,30]]\n7\n[[1,2,6],[4,5,8],[10,20,30]]\n7\n\n\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n1\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n2\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n4\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n6\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n8\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n10\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n12\n[[1,5,6,7],[2,8,11,12],[3,9,13,15],[4,10,14,16]]\n14\n\n\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n1\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n3\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n5\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n7\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n9\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n11\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n13\n[[1,2,4,6],[3,8,11,12],[5,9,13,15],[7,10,14,16]]\n15\n\n*/\n\n\n/*\n\n 1 5 6 7\n 2 8 11 12\n 3 9 13 15\n 4 10 14 16\n\n 1 4 5 6\n 2 8 11 12\n 3 9 13 15\n 7 10 14 16\n\n 1 2 5 6\n 3 8 11 12\n 4 9 13 15\n 7 10 14 16\n\n 1 2 4 6\n 3 8 11 12\n 5 9 13 15\n 7 10 14 16\n\n1 [2, 3]\n2 [3, 4, 8]\n3 [4, 5, 8]\n4 [5, 6, 8, 11]\n5 [6, 7, 8, 9, 11]\n6 [7, 8, 9, 11, 12]\n7 [8, 9, 10, 11, 12]\n8 [9, 10, 11, 12]\n9 [10, 11, 12, 13]\n10 [11, 12, 13, 14]\n11 [12, 13, 14]\n12 [13, 14, 15]\n\n\n\n 1 5 9 20\n 2 6 10 21\n 3 7 11 22\n 4 8 12 23\n\n 1 5 9\n10 11 13\n12 13 15\n\n 1 5 9\n 6 7 10\n12 13 15\n\n 1 5 10\n 4 8 20\n10 18 30\n\n\n 1 50 70\n 2 60 80\n 3 90 100\n\n 1 4 30\n 2 20 40\n 50 60 90\n\n\n 1 2 3\n 4 5 6\n10 20 30\n\n 1 2 6\n 4 5 8\n10 20 30\n\n 1 8 9\n 4 9 10\n10 20 30\n\n[[1,8,9],[4,9,10],[10,20,30]]\n\n*/",
"memory": "16200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\nvector<vector<int>>m1;\nint n,m,K;\nbool check(int val){\n int count=0;\n for(int i=0;i<n;i++){\n auto it=upper_bound(m1[i].begin(),m1[i].end(),val);\n if(it!=m1[i].begin()){\n it--;\n int ind=it-m1[i].begin();\n count+=(ind+1);\n }\n\n }\n return count>=K;\n}\n\n\n\n int kthSmallest(vector<vector<int>>& v1, int k) {\n n=v1.size();\n m=v1[0].size();\n K=k;\n m1.resize(n,vector<int>(m,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n m1[i][j]=v1[i][j];\n }\n }\n int l=v1[0][0]-1;\n int r=v1[n-1][m-1]+1;\n int ans=0;\n while(l+1<r){\n int mid=(l+r)/2;\n if(check(mid)){\n r=mid;\n\n }\n else{\n l=mid;\n }\n }\n return r;\n }\n};",
"memory": "16500"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int n, k;\n int countNums(int x, vector<vector<int>>& arr){\n int top=0, bottom=n-1,mid, count1=0;\n while(top<=bottom){\n mid=top+(bottom-top)/2;\n if(arr[mid][0]<=x) top=mid+1;\n else{\n if(mid>0 && arr[mid-1][0]<=x){\n top=mid;\n break;\n } \n bottom=mid-1;\n }\n }\n for(int i=0;i<top;i++){\n int left=0, right=n-1;\n while(left<=right){\n mid=left+(right-left)/2;\n if(arr[i][mid]<=x) left=mid+1;\n else{\n if(mid>0 && arr[i][mid-1]<=x){\n left=mid;\n break; \n } \n right=mid-1;\n }\n }\n count1+=left;\n }\n return count1;\n }\n\n int kthSmallest(vector<vector<int>>& matrix, int k1) {\n k=k1, n=matrix.size();\n vector<vector<int>> arr=matrix;\n int left=arr[0][0], right=arr[n-1][n-1], mid;\n while(left<=right){\n mid=left+(right-left)/2;\n if(countNums(mid, arr)<k) left=mid+1;\n else{\n if(countNums(mid-1, arr)<k) break;\n right=mid-1;\n }\n }\n return mid; \n }\n};",
"memory": "16700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int n, k;\n int countNums(int x, vector<vector<int>>& arr){\n int i=0, j=n-1, count1=0;\n while(i<n && j>=0){\n if(arr[i][j]>x) j--;\n else{\n if(i<n-1 && arr[i+1][j]>x){\n count1++;\n j--;\n }else{\n i++;\n count1+=(j+1);\n }\n }\n }\n // int top=0, bottom=n-1,mid, count1=0;\n // while(top<=bottom){\n // mid=top+(bottom-top)/2;\n // if(arr[mid][0]<=x) top=mid+1;\n // else{\n // if(mid>0 && arr[mid-1][0]<=x){\n // top=mid;\n // break;\n // } \n // bottom=mid-1;\n // }\n // }\n // for(int i=0;i<top;i++){\n // int left=0, right=n-1;\n // while(left<=right){\n // mid=left+(right-left)/2;\n // if(arr[i][mid]<=x) left=mid+1;\n // else{\n // if(mid>0 && arr[i][mid-1]<=x){\n // left=mid;\n // break; \n // } \n // right=mid-1;\n // }\n // }\n // count1+=left;\n // }\n return count1;\n }\n\n int kthSmallest(vector<vector<int>>& matrix, int k1) {\n k=k1, n=matrix.size();\n vector<vector<int>> arr=matrix;\n int left=arr[0][0], right=arr[n-1][n-1], mid;\n while(left<=right){\n mid=left+(right-left)/2;\n if(countNums(mid, arr)<k) left=mid+1;\n else{\n if(countNums(mid-1, arr)<k) break;\n right=mid-1;\n }\n }\n return mid; \n }\n};",
"memory": "16700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n pair<int,pair<int,int>> a;\n int n = matrix.size();\n int first = n>k?k:n;\n struct min{\n bool operator()(pair<int,pair<int,int>> a, pair<int,pair<int,int>> b){\n return a.first > b.first;\n }\n };\n\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,min> pq;\n for(int i=0;i<first;i++){ pq.push({matrix[i][0],{i,0}}); }\n \n for(int i=0;i<k-1;i++){\n a = pq.top();\n pq.pop();\n int p = a.second.first;\n int q = a.second.second;\n if(q+1<n){ pq.push({matrix[p][q+1],{p,q+1}});}\n }\n return pq.top().first;\n }\n};",
"memory": "16800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class MinHeap {\npublic:\n bool operator()(const tuple<int, int, int>& a,\n const tuple<int, int, int>& b) {\n return get<0>(a) > get<0>(b);\n }\n};\n\nclass Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int rowCount = matrix.size();\n priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,\n MinHeap>\n minHeap;\n\n for (int i = 0; i < min(rowCount, k); ++i) {\n minHeap.push(make_tuple(matrix[i][0], i, 0));\n }\n\n int numbersChecked = 0, smallestElement = 0;\n\n while (!minHeap.empty()) {\n auto [value, rowIndex, colIndex] = minHeap.top();\n minHeap.pop();\n smallestElement = value;\n numbersChecked++;\n\n if (numbersChecked == k)\n break;\n\n if (colIndex + 1 < matrix[rowIndex].size()) {\n minHeap.push(make_tuple(matrix[rowIndex][colIndex + 1],\n rowIndex, colIndex + 1));\n }\n }\n\n return smallestElement;\n }\n};",
"memory": "16900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\n int N, M;\n\n struct Node {\n int val;\n int x, y;\n\n Node(const int &_x, const int &_y, const int &_val) {\n x = _x;\n y = _y;\n val = _val;\n }\n };\n\n struct comp {\n bool operator()(const Node &A, const Node &B) {\n return A.val > B.val; // Min heap\n };\n };\n\n bool isValid(const int &x, const int &y) {\n return (x >= 0 && x < M) && (y >= 0 && y < N);\n }\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n N = matrix.size(), M = matrix[0].size();\n priority_queue<Node, vector<Node>, comp> pq;\n\n for (int i = 0 ; i < N ; i++) {\n pq.push(Node(i, 0, matrix[i][0]));\n }\n\n int tot = 0;\n while (!pq.empty()) {\n struct Node topNode = pq.top();\n pq.pop();\n \n tot++;\n\n if (tot == k) {\n return topNode.val;\n }\n\n // cout << topNode.x << \",\" << topNode.y << \": \" << topNode.val << \", \";\n\n int x1[] = {0, 1};\n int y1[] = {1, 0};\n\n for (int i = 0 ; i < 1 ; i++) {\n int nxtX = topNode.x+x1[i], nxtY = topNode.y+y1[i];\n if (isValid(nxtX, nxtY)) {\n pq.push(Node(nxtX, nxtY, matrix[nxtX][nxtY]));\n }\n }\n }\n\n return 0;\n }\n};",
"memory": "17000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;\n for(int i=0;i<matrix.size();i++){\n pq.push(make_pair(matrix[i][0],make_pair(i,0)));\n }\n for(int i=0;i<k-1;i++){\n auto p= pq.top();\n pq.pop();\n if(p.second.second+1 < matrix[0].size()){\n pq.push(make_pair(matrix[p.second.first][p.second.second+1],make_pair(p.second.first,p.second.second+1)));}\n }\n return pq.top().first;\n }\n};",
"memory": "17100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n pair<int,pair<int,int>> a;\n int n = matrix.size();\n struct min{\n bool operator()(pair<int,pair<int,int>> a, pair<int,pair<int,int>> b){\n return a.first > b.first;\n }\n };\n\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,min> pq;\n for(int i=0;i<n;i++){ pq.push({matrix[i][0],{i,0}}); }\n \n for(int i=0;i<k-1;i++){\n a = pq.top();\n pq.pop();\n if(a.second.second+1<n){ pq.push({matrix[a.second.first][a.second.second+1],{a.second.first,a.second.second+1}});}\n }\n return pq.top().first;\n }\n};",
"memory": "17100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;\n for(int i=0;i<matrix.size();i++){\n pq.push({matrix[i][0],{i,0}});\n }\n pair<int,pair<int,int>>ans;\n while(pq.size()>0 and k--){\n ans=pq.top();\n pq.pop();\n int val=ans.first;\n int row=ans.second.first;\n int col=ans.second.second;\n if(col+1<matrix[row].size()){\n pq.push({matrix[row][col+1],{row,col+1}});\n }\n }\n return ans.first; \n }\n};",
"memory": "17200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n vector<int> ptrs(n, 0);\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;\n for (int i = 0; i < n; ++i) {\n q.push({matrix[i][0], i});\n }\n\n // repeat this K times\n for (int x = 1; x <= k; ++x) {\n\n auto curr = q.top();\n q.pop();\n \n int currMin = curr.first;\n int currPtr = curr.second;\n\n ptrs[currPtr]++;\n if(ptrs[currPtr] < n){\n q.push({matrix[currPtr][ptrs[currPtr]], currPtr});\n }\n\n if (x == k)\n return currMin;\n \n }\n return 0;\n }\n};",
"memory": "17200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n void TDH(vector<int>& nums, int k){\n int c;\n while(2*k+2<nums.size()){\n if(nums[2*k+1]>nums[2*k+2])\n c = 2*k+2;\n else \n c = 2*k+1;\n if(nums[c]<nums[k]){\n int t = nums[c];\n nums[c] = nums[k];\n nums[k] = t;\n k = c;\n }\n else break; //k=nums.size(); // break;\n }\n if(2*k+1<nums.size()){\n if(nums[k]>nums[2*k+1]){\n int t = nums[k];\n nums[k] = nums[2*k+1];\n nums[2*k+1] = t; \n k = c;\n }\n }\n }\n void BuildHeap(vector<int>& nums, int n){\n int i = ((n-1)/2) ;\n while(i>=0){\n TDH(nums, i--);\n }\n }\n int Delete(vector<int>& nums){\n int n = nums.size();\n int maxx = nums[0];\n nums[0] = nums[n-1];\n TDH(nums, 0);\n nums.pop_back();\n return maxx;\n }\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n // create min heap of all these ele\n // delete from heap k times\n vector<int> v;\n for(int i=0; i<matrix.size(); i++){\n for(int j=0;j<matrix[0].size();j++){\n v.push_back(matrix[i][j]);\n }\n }\n BuildHeap(v, v.size());\n int ans;\n for(int i=0;i<k;i++){\n ans = Delete(v);\n }\n return ans;\n }\n};",
"memory": "17300"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int s=matrix.size()*matrix.size();\n int p=0;\n vector<int>temp(s);\n for(int i=0;i<matrix.size();i++)\n {\n for(int j=0;j<matrix.size();j++)\n {\n temp[p]=matrix[i][j];\n p++;\n }\n }\n sort(temp.begin(),temp.end());\n return temp[k-1];\n }\n};",
"memory": "17400"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n struct CustomComparator {\n bool operator()(tuple<int,int,int> left, tuple<int,int,int> right) {\n return get<2>(left) > get<2>(right); // Greater than comparison for min-heap\n }\n };\n\n priority_queue<tuple<int,int,int>,std::vector<tuple<int,int,int>>,CustomComparator> pq;\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n pq.push({0,0,matrix[0][0]});\n vector<vector<bool>> ch(matrix.size(),vector<bool>(matrix.size(),false)) ;\n tuple<int,int,int> t;\n while(k--){\n t=pq.top();\n //cout<<get<2>(t)<<endl;\n int r=get<0>(t),c=get<1>(t);\n if((r+1)<matrix.size() && ch[r+1][c]==false){\n pq.push({r+1,c,matrix[r+1][c]});\n ch[r+1][c] =true;\n }\n if((c+1)<matrix[0].size()&&ch[r][c+1]==false){\n pq.push({r,c+1,matrix[r][c+1]});\n ch[r][c+1]=true;\n }\n pq.pop();\n }\n return get<2>(t);\n }\n};",
"memory": "17400"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n struct CustomComparator {\n bool operator()(tuple<int,int,int> left, tuple<int,int,int> right) {\n return get<2>(left) > get<2>(right); // Greater than comparison for min-heap\n }\n };\n\n priority_queue<tuple<int,int,int>,std::vector<tuple<int,int,int>>,CustomComparator> pq;\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n pq.push({0,0,matrix[0][0]});\n vector<vector<bool>> ch(matrix.size(),vector<bool>(matrix.size(),false)) ;\n tuple<int,int,int> t;\n while(k--){\n t=pq.top();\n cout<<get<2>(t)<<endl;\n int r=get<0>(t),c=get<1>(t);\n if((r+1)<matrix.size() && ch[r+1][c]==false){\n pq.push({r+1,c,matrix[r+1][c]});\n ch[r+1][c] =true;\n }\n if((c+1)<matrix[0].size()&&ch[r][c+1]==false){\n pq.push({r,c+1,matrix[r][c+1]});\n ch[r][c+1]=true;\n }\n pq.pop();\n }\n return get<2>(t);\n }\n};",
"memory": "17500"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n int total_num = n*n;\n if( k > total_num / 2){\n priority_queue<int,vector<int>,greater<int>> pq;\n for(int i = n - 1; i >= 0; i--){\n for(int j = n - 1; j >= 0 ; j--){\n if(pq.size() < total_num - k + 1){\n pq.push(matrix[i][j]);\n }else{\n if(matrix[i][j] > pq.top()){\n pq.pop();\n pq.push(matrix[i][j]);\n }\n }\n }\n }\n return pq.top();\n }else{\n priority_queue<int,vector<int>,less<int>> pq;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n ; j++){\n if(pq.size() < k){\n pq.push(matrix[i][j]);\n }else{\n if(matrix[i][j] < pq.top()){\n pq.pop();\n pq.push(matrix[i][j]);\n }\n }\n }\n }\n return pq.top();\n }\n }\n};",
"memory": "17600"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,\n greater<tuple<int, int, int>>>\n pq;\n // set<tuple<int, int, int>> visited;\n // bool visited[n][n];\n\n // for (int i = 0; i < n; i++){\n // for (int j = 0; j < n; j++){\n // visited[i][j] = 0;\n // }\n // }\n\n vector<vector<int>> visited(n, vector<int>(n, 0));\n\n\n pq.push(make_tuple(matrix[0][0], 0, 0));\n // visited.insert(make_tuple(matrix[0][0], 0, 0));\n visited[0][0] = 1;\n int curr;\n\n for (int x = 0; x < k; x++){\n curr = get<0>(pq.top());\n int i = get<1>(pq.top());\n int j = get<2>(pq.top());\n\n // cout << curr << \" \";\n\n pq.pop();\n\n if (i+1 < n){\n tuple<int, int, int> bottom = make_tuple(matrix[i+1][j], i+1, j);\n if (!visited[i+1][j])\n pq.push(bottom);\n visited[i+1][j] = 1;\n }\n if (j+1 < n){\n tuple<int, int, int> right = make_tuple(matrix[i][j+1], i, j+1);\n if (!visited[i][j+1])\n pq.push(right);\n visited[i][j+1] = 1;\n }\n }\n return curr;\n }\n};",
"memory": "17700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>> &matrix, int k) {\n int m = matrix.size(), n = matrix[0].size(); // For general, the matrix need not be a square\n priority_queue<int> maxHeap;\n for (int r = 0; r < m; ++r) {\n for (int c = 0; c < n; ++c) {\n maxHeap.push(matrix[r][c]);\n if (maxHeap.size() > k) maxHeap.pop();\n }\n }\n return maxHeap.top();\n }\n};\n",
"memory": "17800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int> pq; \n int n = matrix.size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n pq.push(matrix[i][j]);\n if (pq.size() > k) {\n pq.pop();\n }\n }\n }\n return pq.top();\n }\n};\n",
"memory": "17900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int m = matrix.size();\n int n = matrix[0].size();\n\n priority_queue<int> maxHeap;\n for(int r = 0; r < m; r++)\n {\n for(int c = 0; c < n; c++)\n {\n maxHeap.push(matrix[r][c]);\n if(maxHeap.size() > k)\n maxHeap.pop();\n }\n }\n return maxHeap.top();\n }\n};",
"memory": "17900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue <int> maxH;\n\n int m = matrix.size(), n = matrix[0].size();\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n maxH.push(matrix[i][j]);\n if (maxH.size() > k) maxH.pop();\n }\n }\n return maxH.top();\n }\n};",
"memory": "18000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int> pq;\n \n for(int i=0;i<matrix.size();i++){\n for(int j=0;j<matrix[0].size();j++){\n pq.push(matrix[i][j]);\n\n if(pq.size()>k){\n pq.pop();\n }\n }\n }\n return pq.top();\n }\n};",
"memory": "18000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n priority_queue<int> q;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n if(q.size()>=k&&matrix[i][j]>q.top()){\n break;\n }\n q.push(matrix[i][j]);\n while(q.size()>k){\n q.pop();\n }\n }\n }\n return q.top();\n\n }\n};",
"memory": "18100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n priority_queue<int> pq;\n int n = matrix.size();\n for(int i=0;i<n;i++) {\n for(int j=0;j<n;j++) {\n if(pq.size() < k) {\n pq.push(matrix[i][j]);\n }\n else {\n if(pq.top() > matrix[i][j]) {\n pq.pop();\n pq.push(matrix[i][j]);\n }\n }\n }\n }\n return pq.top();\n }\n};",
"memory": "18100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size(),m=matrix[0].size();\n \n priority_queue<int> pq;\n for(int i =0;i<n;i++){\n\n for(int j =0;j<n;j++){\n\n pq.push(matrix[i][j]);\n if(pq.size()>k)pq.pop();\n }\n }\n return pq.top();\n }\n};",
"memory": "18200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n //int rows=matrix[0].size();\n int n = matrix.size();\n priority_queue<int> maxHeap; \n\n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < n; ++j) {\n maxHeap.push(matrix[i][j]); \n \n if (maxHeap.size() > k) {\n maxHeap.pop(); \n }\n }\n }\n\n return maxHeap.top();\n \n }\n};",
"memory": "18200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int> pq;\n for (auto it : matrix) {\n for (auto jt : it) {\n\n if (pq.size() < k)\n pq.push(jt);\n else if(jt<pq.top()) {\n pq.pop();\n pq.push(jt);\n }\n }\n }\n return pq.top();\n }\n};",
"memory": "18900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue <int> maxheap;\n for(auto r : matrix){\n for(auto element : r){\n if(maxheap.size() < k){\n maxheap.push(element);\n }else{\n if(element > maxheap.top()){\n continue;\n }else{\n maxheap.pop();\n maxheap.push(element);\n }\n }\n }\n }\n return maxheap.top();\n }\n};",
"memory": "18900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int> pq;\n int count = 0;\n for(auto i : matrix){\n for(auto j : i){\n pq.push(j);\n count++;\n }\n }\n for(int i =0;i<count-k;i++){\n pq.pop();\n }\n return pq.top();\n }\n};",
"memory": "19000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n vector<int>v;\n for (auto r : matrix) {\n\n v.insert(v.end(), r.begin(), r.end());\n\n }\n\n \n\n \n sort (v.begin(),v.end());\n return v[k-1];\n }\n};",
"memory": "19000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n // Second method: max-heap for optimization\n // Create a 1D vector from the 2D matrix\n vector<int> elements;\n int rows = matrix.size();\n if (rows == 0) return -1; \n\n int cols = matrix[0].size();\n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n elements.push_back(matrix[i][j]);\n }\n }\n priority_queue<int> p(elements.begin(), elements.begin() + k);\n for (int i = k; i < elements.size(); ++i) {\n if (elements[i] < p.top()) {\n p.pop();\n p.push(elements[i]);\n }\n }\n return p.top();\n }\n};",
"memory": "19100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<long> maxHeap;\n\n for(auto& row: matrix) {\n for(auto& it: row) {\n maxHeap.push(it);\n if(maxHeap.size() > k) maxHeap.pop();\n }\n }\n\n return maxHeap.top();\n }\n};",
"memory": "19200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int, vector<int>, greater<int>> minHeap;\n for(auto it:matrix){\n for(auto it2:it){\n minHeap.push(it2);\n }\n }\n for(int i=0;i<k-1;i++){\n minHeap.pop();\n }\n return minHeap.top();\n }\n};",
"memory": "19300"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n vector<int>nums;\n for(int i=0;i<matrix.size();i++){\n for(int j=0;j<matrix[0].size();j++){\n nums.push_back(matrix[i][j]);\n }\n }\n\n priority_queue<int> pq;\n\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n\n if(pq.size()>k){\n pq.pop();\n }\n }\n return pq.top();\n }\n};",
"memory": "19400"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n vector<int> v;\n for(int i=0; i<matrix.size(); i++){\n for(int j=0; j<matrix[i].size(); j++){\n v.push_back(matrix[i][j]);\n }\n }\n priority_queue<int> q;\n for(int i=0; i<k; i++){\n q.push(v[i]);\n }\n for(int i=k; i<v.size(); i++){\n if(v[i]<q.top()){\n q.pop();\n q.push(v[i]);\n }\n }\n return q.top();\n }\n};",
"memory": "19500"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n vector<long long>temp;\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n temp.push_back(matrix[i][j]);\n }\n }\n sort(temp.begin(),temp.end());\n \n if(k-1<n*n){\n return temp[k-1];\n }\n return 0;\n }\n\n};",
"memory": "19600"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n vector<int>ans;\n\n for(int i=0;i<matrix.size();i++){\n for(int j=0;j<matrix[0].size();j++){\n\n ans.push_back(matrix[i][j]);\n }\n }\n\n priority_queue<int>pq;\n\n for(int i=0;i<k;i++){\n int ele=ans[i];\n pq.push(ele);\n }\n for(int i=k;i<ans.size();i++){\n int ele=ans[i];\n if(ele<pq.top()){\n pq.pop();\n pq.push(ele);\n }\n }\n int an=pq.top();\n return an;\n \n }\n};",
"memory": "19700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size()*matrix.size();\n vector<int> arr;\n int p=0;\n for(auto row:matrix){\n for(auto i:row){\n arr.push_back(i);\n }\n }\n\n\n priority_queue<int,vector<int>,greater<int>> minheap(arr.begin(),arr.end());\n for(int i=1;i<k;i++){\n minheap.pop();\n }\n return minheap.top();\n\n }\n};",
"memory": "19800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int,vector<int>,greater<int>> pq;\n int n = matrix[0].size();\n vector<int>temp;\n for(int i =0;i<n;i++){\n for(int j =0;j<n;j++){\n temp.push_back(matrix[i][j]);\n }\n }\n for(int i =0;i<temp.size();i++){\n pq.push(temp[i]);\n }\n\n int ans =0;\n while(k){\n ans = pq.top();\n cout << ans << endl;\n pq.pop();\n k--;\n }\n return ans;\n }\n};",
"memory": "19900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class node {\n public:\n int data;\n int arrayno;\n int index;\n \n node(int data, int row, int col){\n this->data = data;\n arrayno = row;\n index = col;\n }\n };\n class compare{\n public:\n bool operator()(node* a, node* b){\n return a->data > b->data;\n }\n };\nclass Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<node*,vector<node*>,compare> minheap;\n\n // store first element of every array (k)\n \n for (int i=0; i<matrix.size(); i++) {\n node* temp = new node(matrix[i][0], i, 0); // first element\n minheap.push(temp);\n }\n \n // \n int count = 0;\n int result = 0;\n \n while (count<k){\n node* temp = minheap.top();\n minheap.pop();\n\n result = temp->data;\n count++;\n \n int arrayno = temp->arrayno;\n int index = temp->index;\n \n // check size for next element\n if (index+1 < matrix[arrayno].size()){\n node* next = new node(matrix[arrayno][index+1], arrayno, index+1);\n minheap.push(next);\n }\n \n }\n \n return result;\n }\n};",
"memory": "20000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n if (k == 1)\n return matrix[0][0];\n\n int n = matrix.size(), m = matrix[0].size();\n\n typedef pair<int, pair<int, int>> pipii;\n priority_queue<pipii, vector<pipii>, greater<pipii>> q;\n int count = 0;\n\n q.push({ matrix[0][0], {0, 0}});\n\n while (!q.empty()) {\n auto [val, loc] = q.top();\n q.pop();\n ++count;\n\n if (count == k)\n return val;\n\n if (loc.first != n - 1) {\n q.push({matrix[loc.first + 1][loc.second], {loc.first + 1, loc.second}});\n matrix[loc.first + 1][loc.second] = INT_MAX;\n }\n\n if (loc.second != m - 1) {\n q.push({matrix[loc.first][loc.second + 1], {loc.first, loc.second + 1}});\n matrix[loc.first][loc.second + 1] = INT_MAX;\n }\n }\n\n return -1;\n }\n};",
"memory": "20100"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n if (k == 1)\n return matrix[0][0];\n\n int n = matrix.size(), m = matrix[0].size();\n\n typedef pair<int, pair<int, int>> pipii;\n priority_queue<pipii, vector<pipii>, greater<pipii>> q;\n int count = 0;\n\n q.push({ matrix[0][0], {0, 0}});\n\n while (!q.empty()) {\n auto [val, loc] = q.top();\n q.pop();\n ++count;\n\n if (count == k)\n return val;\n\n if (loc.first != n - 1) {\n q.push({matrix[loc.first + 1][loc.second], {loc.first + 1, loc.second}});\n matrix[loc.first + 1][loc.second] = INT_MAX;\n }\n\n if (loc.second != m - 1) {\n q.push({matrix[loc.first][loc.second + 1], {loc.first, loc.second + 1}});\n matrix[loc.first][loc.second + 1] = INT_MAX;\n }\n }\n\n return -1;\n }\n};",
"memory": "20200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<int> pq;\n vector<int> temp;\n for(vector<int> it : matrix){\n for(auto i : it){\n temp.push_back(i);\n }\n }\n for(int i=0; i<k; i++){\n pq.push(temp[i]);\n }\n for(int i=k; i<temp.size(); i++){\n if(pq.top()>temp[i]){\n pq.push(temp[i]);\n pq.pop();\n }\n }\n return pq.top();\n }\n};",
"memory": "20300"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Compare {\n bool operator()(vector<int>& a, vector<int>& b) {\n return a[0] > b[0];\n }\n };\n\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<vector<int>, vector<vector<int>>, Compare> pq;\n int n = matrix.size();\n\n for (int i = 0; i < n; i++) {\n pq.push({matrix[i][0], i, 0});\n }\n\n int val;\n while (k > 0) {\n val = pq.top()[0];\n int i = pq.top()[1];\n int j = pq.top()[2];\n pq.pop();\n\n if (j + 1 < n) {\n pq.push({matrix[i][j + 1], i, j + 1});\n }\n\n k--;\n }\n\n return val;\n }\n};",
"memory": "20600"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;\n int i;\n int n = matrix.size();\n\n for(i=0;i<min(n,k); i++){\n pq.push({matrix[i][0], i, 0});\n }\n\n vector<int>x;\n while(k--){\n x = pq.top();\n pq.pop();\n\n int row = x[1];\n int col = x[2];\n\n if(col+1 < n){\n pq.push({matrix[row][col+1], row, col+1});\n }\n }\n\n //cout<<x[0];\n return x[0];\n\n\n }\n};",
"memory": "20700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n=matrix.size();\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>> pq;\n for(int i=0;i<n;i++){\n pq.push({matrix[i][0],i,0}); //{number,row,col};\n }\n int curr=0;\n while(!pq.empty()){\n curr++;\n int num=pq.top()[0];\n int row=pq.top()[1];\n int col=pq.top()[2];\n pq.pop();\n if(curr==k) return num;\n if(col+1<n) pq.push({matrix[row][col+1],row,col+1});\n }\n return -1;\n }\n};",
"memory": "20800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(const vector<vector<int>>& matrix, const int k) {\n const int m = matrix.size(); \n const int n = matrix[0].size();\n auto pq_comparator = [](const auto &a, const auto &b)\n {\n return a[0] > b[0];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(pq_comparator)> pq(pq_comparator);\n for (int i = 0; i < min(k, m); i++) {\n pq.push({matrix[i][0], i, 0});\n }\n int cnt = k;\n vector<int> next;\n while (cnt-- > 0) {\n next = pq.top();\n pq.pop();\n const int row = next[1]; \n const int col = next[2];\n if (col + 1 < n) {\n pq.push({matrix[row][col + 1], row, col + 1});\n }\n }\n return next[0];\n }\n};",
"memory": "20900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n vector <int> id(matrix.size(), 0);\n priority_queue <vector<int>, vector<vector<int> >, greater<vector<int > > >q;\n\n for(int i = 0; i < matrix.size(); i++) {\n q.push({matrix[i][0], i, 0});\n }\n\n int res = 0, ans = 0, x, y; \n while(res < k){\n ans = q.top()[0];\n res++;\n x = q.top()[1]; y = q.top()[2];\n q.pop();\n if(y + 1 < matrix[x].size()){\n q.push({matrix[x][y+1], x, y+1});\n }\n }\n return ans;\n }\n};",
"memory": "21000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n // 3:24 PM - 3:31 PM\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> minHeap;\n int r = 0, c = 0;\n int n = matrix.size();\n int ans = 0;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n minHeap.push({matrix[r][c], r, c});\n\n while (minHeap.size()){\n int curRow = minHeap.top()[1];\n int curCol = minHeap.top()[2];\n minHeap.pop();\n\n k--;\n if (k == 0) {\n ans = matrix[curRow][curCol];\n break;\n }\n\n if (curRow+1 < n && !visited[curRow+1][curCol]) {\n minHeap.push({matrix[curRow+1][curCol], curRow+1, curCol});\n visited[curRow+1][curCol] = true;\n }\n if (curCol+1 < n && !visited[curRow][curCol+1]) {\n minHeap.push({matrix[curRow][curCol+1], curRow, curCol+1});\n visited[curRow][curCol+1] = true;\n }\n }\n\n return ans;\n }\n};",
"memory": "21200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n // 3:24 PM - 3:31 PM\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> minHeap;\n int r = 0, c = 0;\n int n = matrix.size();\n int ans = 0;\n vector<vector<bool>> visited(n, vector<bool>(n, false));\n minHeap.push({matrix[r][c], r, c});\n\n while (minHeap.size()){\n int curRow = minHeap.top()[1];\n int curCol = minHeap.top()[2];\n minHeap.pop();\n\n k--;\n if (k == 0) {\n ans = matrix[curRow][curCol];\n break;\n }\n\n if (curRow+1 < n && !visited[curRow+1][curCol]) {\n minHeap.push({matrix[curRow+1][curCol], curRow+1, curCol});\n visited[curRow+1][curCol] = true;\n }\n if (curCol+1 < n && !visited[curRow][curCol+1]) {\n minHeap.push({matrix[curRow][curCol+1], curRow, curCol+1});\n visited[curRow][curCol+1] = true;\n }\n }\n\n return ans;\n }\n};",
"memory": "21200"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "//note\n//find K pairs with smallest sums\n//min heap, use greater(>) comparator\n//pop before push\nclass Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n/*\n1 5 9\n10 11 13\n12 13 15\n*/\n int h = matrix.size(), w = matrix[0].size();\n auto& m= matrix;\n auto comp = [&](const auto &lhs, const auto &rhs){\n return m[lhs[0]][lhs[1]] > m[rhs[0]][rhs[1]];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> q(comp);\n\n for(int i = 0; i < h; ++i){\n q.push(vector<int>{i,0});\n }\n\n int ret = 0;\n while(k>0){\n --k;\n const auto& v = q.top();\n int r=v[0];\n int c=v[1];\n ret = m[r][c];\n q.pop();\n if(c+1<w){\n q.push(vector<int>{r,c+1});\n }\n }\n return ret;\n }\n};",
"memory": "22000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size(),ans;\n vector<int> v;multiset<pair<int,pair<int,int>>>ms;\n for(int i=0;i<n;i++){\n ms.insert({matrix[i][0],{i,0}});\n }\n int i,j;\n while(k){\n auto p=ms.begin();\n ans=p->first;i=(p->second).first,j=(p->second).second;\n ;ms.erase(ms.begin());\n if(j+1<n)ms.insert({matrix[i][j+1],{i,j+1}});\n k--;\n }\n return ans;\n }\n};",
"memory": "22500"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n // min heap pq.\n // can i do the same using map.\n // \n set< pair<int,pair<int,int>> > st;\n int n = matrix.size();\n st.insert( { matrix[0][0] , {0,0} } );\n while(--k){\n auto it = st.begin();\n pair<int,pair<int,int>> pr = *it;\n st.erase(it);\n auto [x,y] = pr.second;\n if( x + 1 < n ){\n st.insert({ matrix[x+1][y] , {x+1,y} });\n }\n if( y + 1 < n ){\n st.insert({ matrix[x][y+1] , { x , y+1 } });\n }\n }\n auto it = st.begin();\n return (*it).first;\n }\n};",
"memory": "22600"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "#include <tuple>\nclass Solution {\npublic:\n bool is_valid(const vector<vector<int>> &matrix, int y, int x){\n if (y<0 || y >= matrix.size())\n return false;\n if (x<0 || x >= matrix.size())\n return false;\n return true;\n }\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n if( k == 1) return matrix[0][0];\n set<tuple<int,int,int>> grey; // min heap {value, y, x}\n int cur_rank = 2;\n grey.insert(make_tuple(matrix[0][1], 0,1));\n grey.insert(make_tuple(matrix[1][0], 1,0));\n while( !grey.empty() ){\n auto [value, y, x] = *grey.begin();\n grey.erase(grey.begin());\n if(cur_rank==k)\n return value;\n if( is_valid(matrix, y+1, x) && grey.find(make_tuple(matrix[y+1][x], y+1, x))==grey.end()){\n grey.insert(make_tuple(matrix[y+1][x], y+1, x));\n }\n if( is_valid(matrix, y, x+1) && grey.find(make_tuple(matrix[y][x+1], y, x+1))==grey.end()){\n grey.insert(make_tuple(matrix[y][x+1], y, x+1));\n }\n cur_rank += 1;\n }\n return -1; \n\n }\n};",
"memory": "22700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "#include <tuple>\nclass Solution {\npublic:\n bool is_valid(vector<vector<int>> &matrix, int y, int x){\n if (y<0 || y >= matrix.size())\n return false;\n if (x<0 || x >= matrix.size())\n return false;\n return true;\n }\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n if( k == 1) return matrix[0][0];\n set<tuple<int,int,int>> grey; // min heap {value, y, x}\n int cur_rank = 2;\n grey.insert(make_tuple(matrix[0][1], 0,1));\n grey.insert(make_tuple(matrix[1][0], 1,0));\n while( !grey.empty() ){\n auto [value, y, x] = *grey.begin();\n grey.erase(grey.begin());\n if(cur_rank==k)\n return value;\n if( is_valid(matrix, y+1, x) && grey.find(make_tuple(matrix[y+1][x], y+1, x))==grey.end()){\n grey.insert(make_tuple(matrix[y+1][x], y+1, x));\n }\n if( is_valid(matrix, y, x+1) && grey.find(make_tuple(matrix[y][x+1], y, x+1))==grey.end()){\n grey.insert(make_tuple(matrix[y][x+1], y, x+1));\n }\n cur_rank += 1;\n }\n return -1; \n\n }\n};",
"memory": "22800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n pair<int, pair<int,int>> elFromMatrix(vector<vector<int>>& matrix, int r, int c) {\n return make_pair(matrix[r][c], make_pair(r, c));\n }\n\n int getIdx(pair<int,int> rc) {\n return rc.first*10000 + rc.second;\n }\n\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<pair<int, pair<int,int>>> pq;\n unordered_set<int> visited;\n pq.push(make_pair(-1*matrix[0][0], make_pair(0,0)));\n --k;\n\n while(pq.size() > 0) {\n pair<int, pair<int,int>> el = pq.top();\n pq.pop();\n\n if(visited.find(getIdx(el.second)) != visited.end())\n continue;\n\n if(k == 0) {\n return -1*el.first;\n }\n\n if(el.second.first < matrix.size()-1) {\n auto ell = elFromMatrix(matrix, el.second.first+1, el.second.second);\n ell.first = -1*ell.first;\n\n //if(visited.find(getIdx(ell.second)) != visited.end())\n pq.push(ell);\n }\n\n if(el.second.second < matrix.size()-1) {\n auto ell = elFromMatrix(matrix, el.second.first, el.second.second+1);\n ell.first = -1*ell.first;\n\n //if(visited.find(getIdx(ell.second)) != visited.end())\n pq.push(ell);\n }\n\n visited.insert(getIdx(el.second));\n --k;\n } \n\n return 0; \n }\n};",
"memory": "22900"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n;\n int encode(int row, int col)\n {\n return row * n + col;\n }\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n // Create a min heap\n // Add the corner element to the heap. This is garunteed to be the smallest number.\n // For each iteration: Pop the smallest element from the heap. The next smallest element will be either the stuff currently in the heap or the elements to the left and right\n\n this->n = matrix.size();\n int numIterations = 0;\n priority_queue< pair<int,pair<int,int>>, \n vector<pair<int,pair<int,int>>>, \n greater<pair<int,pair<int,int>>> > minHeap; // {integer, {row, col}}\n unordered_set<int> visited;\n minHeap.push({matrix[0][0], {0, 0}});\n visited.insert(encode(0,0));\n int kthSmallestNum = matrix[0][0];\n while(!minHeap.empty())\n {\n auto topHeap = minHeap.top();\n minHeap.pop();\n kthSmallestNum = topHeap.first;\n int row = topHeap.second.first;\n int col = topHeap.second.second;\n numIterations++;\n if(numIterations == k)\n {\n return kthSmallestNum;\n }\n\n //Right element\n if(col + 1 < n && visited.find(encode(row, col+1)) == visited.end())\n {\n minHeap.push({matrix[row][col+1], {row, col+1}});\n visited.insert(encode(row, col+1));\n }\n \n //Bottom element\n if(row + 1 < n && visited.find(encode(row+1, col)) == visited.end())\n {\n minHeap.push({matrix[row+1][col], {row + 1, col}});\n visited.insert(encode(row+1, col));\n }\n \n }\n\n return kthSmallestNum;\n }\n};",
"memory": "23000"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n\n int n = matrix.size();\n int start = matrix[0][0];\n int end = matrix[n-1][n-1];\n\n unordered_map<int, int>mp;\n\n for(int i = 0; i < n; i++)\n for(int j = 0; j < n; j++)\n mp[matrix[i][j]]++;\n \n for(int i = start; i <= end; i++){\n if(mp.find(i) != mp.end()){\n for(int j = 0; j < mp[i]; j++){\n k--;\n if(k == 0) return i;\n }\n }\n }\n return -1;\n }\n};",
"memory": "23500"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n typedef tuple<int, int, int> tp;\n\n priority_queue<tp, vector<tp>, greater<tp>> pq;\n\n if(!matrix.size()) {\n return 0;\n }\n\n pq.push({matrix[0][0],0,0});\n unordered_map<int, unordered_set<int>> visited {{0,{0}}};\n while(pq.size()) {\n auto [n, i, j] = pq.top();\n pq.pop();\n if(!--k) {\n return n;\n }\n if(i + 1 < matrix.size() && !visited[i+1].count(j)) {\n pq.push({matrix[i+1][j], i+1, j});\n visited[i+1].insert(j);\n }\n if(j + 1 < matrix[0].size() && !visited[i].count(j+1)) {\n pq.push({matrix[i][j+1], i, j+1});\n visited[i].insert(j+1);\n }\n }\n\n return 0;\n }\n};",
"memory": "23600"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int get_value(vector<vector<int>>& matrix, int row, int col) {\n if (row < 0 || col < 0) {\n return INT_MAX;\n } \n if (row >= matrix.size() || col >= matrix[0].size()) {\n return INT_MAX;\n }\n return matrix[row][col];\n }\n\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n priority_queue<tuple<int, int, int>> queue;\n set<pair<int, int>> visited;\n int num_passed = 0;\n queue.push(make_tuple(-matrix[0][0], 0, 0));\n while (num_passed != k) {\n auto my_tuple = queue.top();\n queue.pop();\n auto value = get<0>(my_tuple);\n auto row = get<1>(my_tuple);\n \n // cout << num_passed << endl;\n // cout << value << endl;\n auto col = get<2>(my_tuple);\n if (visited.find(make_pair(row, col)) != visited.end()) {\n continue;\n }\n visited.insert(make_pair(row, col));\n num_passed += 1;\n if (num_passed == k) {\n return -value;\n }\n auto v1 = get_value(matrix, row+1, col);\n if (v1 != INT_MAX) {\n queue.push(make_tuple(-v1, row+1, col));\n }\n auto v2 = get_value(matrix, row, col+1);\n if (v2 != INT_MAX) {\n queue.push(make_tuple(-v2, row, col+1));\n }\n }\n return matrix[matrix.size()-1][matrix[0].size() - 1];\n }\n};",
"memory": "23700"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int kthSmallest(vector<vector<int>>& matrix, int k) {\n int n = matrix.size();\n priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,\n greater<tuple<int, int, int>>>\n pq;\n set<tuple<int, int, int>> visited;\n\n pq.push(make_tuple(matrix[0][0], 0, 0));\n visited.insert(make_tuple(matrix[0][0], 0, 0));\n int curr;\n\n for (int x = 0; x < k; x++){\n curr = get<0>(pq.top());\n int i = get<1>(pq.top());\n int j = get<2>(pq.top());\n\n cout << curr << \" \";\n\n pq.pop();\n\n if (i+1 < n){\n tuple<int, int, int> bottom = make_tuple(matrix[i+1][j], i+1, j);\n if (visited.find(bottom) == visited.end())\n pq.push(bottom);\n visited.insert(bottom);\n }\n if (j+1 < n){\n tuple<int, int, int> right = make_tuple(matrix[i][j+1], i, j+1);\n if (visited.find(right) == visited.end())\n pq.push(right);\n visited.insert(right);\n }\n }\n return curr;\n }\n};",
"memory": "23800"
} |
378 | <p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p>
<p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not the <code>k<sup>th</sup></code> <strong>distinct</strong> element.</p>
<p>You must find a solution with a memory complexity better than <code>O(n<sup>2</sup>)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>Output:</strong> 13
<strong>Explanation:</strong> The elements in the matrix are [1,5,9,10,11,12,13,<u><strong>13</strong></u>,15], and the 8<sup>th</sup> smallest number is 13
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> matrix = [[-5]], k = 1
<strong>Output:</strong> -5
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 300</code></li>
<li><code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code></li>
<li>All the rows and columns of <code>matrix</code> are <strong>guaranteed</strong> to be sorted in <strong>non-decreasing order</strong>.</li>
<li><code>1 <= k <= n<sup>2</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>Could you solve the problem with a constant memory (i.e., <code>O(1)</code> memory complexity)?</li>
<li>Could you solve the problem in <code>O(n)</code> time complexity? The solution may be too advanced for an interview but you may find reading <a href="http://www.cse.yorku.ca/~andy/pubs/X+Y.pdf" target="_blank">this paper</a> fun.</li>
</ul>
| 3 | {
"code": "class Solution {\n \n class comp\n {\n public:\n bool operator()(const auto & v1, const auto & v2)\n {\n return v1[0] > v2[0];\n }\n };\n \npublic:\n \n //hg\n int kthSmallest(vector<vector<int>>& matrix, int k) \n {\n int n = matrix.size();\n priority_queue<vector<int> ,vector<vector<int>>, comp> pq;\n int res = 0;\n \n for(int i=0;i<n;i++)\n {\n pq.push({matrix[i][0], i, 0});\n }\n \n while(!pq.empty())\n {\n auto vec = pq.top();\n \n if(k == 1)\n return vec[0];\n \n pq.pop();\n \n if(vec[2] +1 < n)\n {\n pq.push({matrix[vec[1]][vec[2] + 1], vec[1] , vec[2]+1});\n }\n \n k--;\n }\n \n return -1;\n }\n};",
"memory": "24400"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 0 | {
"code": "#include <cstdlib> // For rand()\n#include <ctime> // For time()\n\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n Solution(ListNode* head) {\n this->head = head;\n // Initialize random seed\n srand(time(0));\n }\n \n int getRandom() {\n ListNode* current = head;\n ListNode* reservoir = NULL;\n int n = 0;\n \n while (current) {\n n++;\n // Reservoir sampling: with probability 1/n, replace the reservoir\n if (rand() % n == 0) {\n reservoir = current;\n }\n current = current->next;\n }\n \n return reservoir ? reservoir->val : -1; // Return -1 if the list is empty\n }\n\nprivate:\n ListNode* head;\n};\n",
"memory": "21300"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n ListNode* head;\n int len = 0;\n\n int findLen(ListNode* head)\n {\n int len = 0;\n while(head)\n {\n len++;\n head = head->next;\n }\n return len;\n }\npublic:\n Solution(ListNode* head) {\n this->head = head;\n len = findLen(head);\n }\n \n int getRandom() {\n int random = rand()%len;\n ListNode* temp = head;\n while(temp && random)\n {\n temp = temp->next;\n random--;\n }\n\n return temp->val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21400"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nint len = 0;\nListNode* headnode;\n Solution(ListNode* head) {\n\n headnode = head;\n ListNode* temp = head;\n while(temp != NULL)\n {\n len++;\n temp = temp -> next;\n }\n }\n \n int getRandom() {\n int index = rand()%len;\n ListNode* temp = headnode;\n for(int i=0;i<index;i++)\n {\n temp = temp -> next;\n }\n return temp -> val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21400"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode *root,*curr;\npublic:\n Solution(ListNode* head) {\n root = head;\n curr = head;\n srand(time(NULL));\n }\n \n int getRandom() {\n int n = rand()/(RAND_MAX*1.0) * 1000;\n for(int i=0;i<n;i++){\n if(curr->next) curr = curr->next;\n else curr = root;\n }\n return curr->val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21500"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode *root,*curr;\npublic:\n Solution(ListNode* head) {\n root = head;\n curr = head;\n srand(time(NULL));\n }\n \n int getRandom() {\n int n = rand()/(RAND_MAX*1.0) * 500;\n for(int i=0;i<n;i++){\n if(curr->next) curr = curr->next;\n else curr = root;\n }\n return curr->val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21500"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* Head;\n Solution(ListNode* head) {\n Head = head;\n }\n \n int getRandom() {\n int count = 1;\n int result = 0;\n ListNode* temp = Head;\n while(temp){\n if((rand() % count) < 1.0/count){\n result = temp -> val;\n }\n count++;\n temp = temp -> next;\n }\n return result;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */\n\n// class Solution {\n// public:\n// vector<int> arr;\n// Solution(ListNode* head) {\n// ListNode* temp = head;\n// while(temp){\n// arr.push_back(temp -> val);\n// temp = temp -> next;\n// }\n// }\n \n// int getRandom() {\n// int n = arr.size();\n// int random_index = rand() % n;\n// return arr[random_index];\n// }\n// };",
"memory": "21600"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode* root;\n int len=0;\npublic:\n Solution(ListNode* head) {\n root=head;\n while(head){\n len++;\n head=head->next;\n }\n }\n \n int getRandom() {\n int it=rand()%len;\n ListNode* tmp=root;\n while(it--){\n tmp=tmp->next;\n }\n return tmp->val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21600"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n vector<int> a;\npublic:\n Solution(ListNode* root) {\n while(root!=NULL)\n {\n a.push_back(root->val);\n root=root->next;\n }\n }\n int getRandom() {\n return a[rand() % a.size()];\n }\n};\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21700"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n vector<int>vec;\n int n;\n Solution(ListNode* head) {\n ListNode* node= head;\n while(node){\n vec.push_back(node->val);\n node=node->next;\n }\n n = vec.size();\n }\n \n int getRandom() {\n return vec[rand()%n]; \n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21700"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<ListNode*> nodes;\n Solution(ListNode* head) {\n while(head!=NULL)\n {\n nodes.push_back(head);\n head=head->next;\n }\n }\n \n int getRandom() {\n\n int n=nodes.size();\n int radn=rand()%n;\n return nodes[radn]->val; \n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21800"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution\n{\nprivate:\n vector<ListNode*> nodes;\npublic:\n Solution(ListNode* head)\n {\n while (head != nullptr)\n {\n nodes.push_back(head);\n head = head->next;\n }\n }\n \n int getRandom()\n {\n return nodes[rand() % nodes.size()]->val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21800"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n mt19937 gen;\n uniform_int_distribution<> distrib;\n vector<int> nums;\n \n \npublic:\n Solution(ListNode* head) { \n while(head != nullptr) {\n nums.push_back(head->val);\n head = head->next;\n }\n random_device rd;\n gen = mt19937(rd());\n distrib = uniform_int_distribution<>(0, nums.size() - 1);\n }\n \n int getRandom() {\n int i = distrib(gen);\n return nums[i];\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "21900"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> store;\n int cap = 0;\n int index = 0;\n\n Solution(ListNode* head) {\n store.resize(10000); \n while(head){\n store[cap] = head->val; \n head = head->next;\n cap++;\n }\n }\n\n int getRandom() {\n if (cap == 0) return -1; \n int randomIndex = rand() % cap;\n return store[randomIndex];\n }\n};\n",
"memory": "22000"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode *sol;\n vector<ListNode*>solArr;\npublic:\n Solution(ListNode* head) {\n sol=head;\n }\n \n int getRandom() {\n while(sol!=NULL){\n ListNode* newEle = new ListNode(sol->val);\n solArr.push_back(newEle);\n sol=sol->next;\n }\n int size = solArr.size();\n cout<<size;\n int randomIndex = rand()%size;\n return solArr[randomIndex]->val;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "22100"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n unordered_map<int,int> mp;\n int sz;\npublic:\n Solution(ListNode* head) {\n int idx = 0;\n while(head){\n mp[idx] = head->val;\n head = head->next;\n idx++;\n }\n sz = idx;\n }\n \n int getRandom() {\n return mp[rand()%sz];\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "22200"
} |
382 | <p>Given a singly linked list, return a random node's value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p>
<p>Implement the <code>Solution</code> class:</p>
<ul>
<li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linked list <code>head</code>.</li>
<li><code>int getRandom()</code> Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/16/getrand-linked-list.jpg" style="width: 302px; height: 62px;" />
<pre>
<strong>Input</strong>
["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
[[[1, 2, 3]], [], [], [], [], []]
<strong>Output</strong>
[null, 1, 3, 2, 2, 3]
<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3]);
solution.getRandom(); // return 1
solution.getRandom(); // return 3
solution.getRandom(); // return 2
solution.getRandom(); // return 2
solution.getRandom(); // return 3
// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the linked list will be in the range <code>[1, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>getRandom</code>.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<ul>
<li>What if the linked list is extremely large and its length is unknown to you?</li>
<li>Could you solve this efficiently without using extra space?</li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n multiset<int>s;\n Solution(ListNode* head) {\n while(head){\n s.insert(head->val);\n head=head->next;\n }\n }\n \n int getRandom() {\n int c=rand()%s.size();\n auto it=s.begin();\n advance(it,c);\n return *it;\n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(head);\n * int param_1 = obj->getRandom();\n */",
"memory": "22300"
} |
383 | <p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code></li>
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool canConstruct(const string& ransomNote, const string& magazine) {\n static const int ALPH = 26;\n int letters[ALPH] = {0};\n for (char c : magazine) {\n ++letters[c-'a'];\n }\n for (char c : ransomNote) {\n --letters[c-'a'];\n }\n bool cant = false;\n for (int i = 0; i < ALPH; ++i) {\n cant = cant || (letters[i] < 0);\n }\n return !cant;\n }\n};",
"memory": "9700"
} |
383 | <p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code></li>
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool canConstruct(const string& ransomNote, const string& magazine) {\n static const int ALPH = 26;\n int letters[ALPH] = {0};\n for (char c : magazine) {\n ++letters[c-'a'];\n }\n for (char c : ransomNote) {\n --letters[c-'a'];\n }\n bool cant = false;\n for (int i = 0; i < ALPH; ++i) {\n cant = cant || (letters[i] < 0);\n }\n return !cant;\n }\n};",
"memory": "9800"
} |
383 | <p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code></li>
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool canConstruct(string &ransomNote, string &magazine) {\n int note[26] = {}, mag[26] = {};\n for (char c : ransomNote) ++note[c - 'a'];\n for (char c : magazine) ++mag[c - 'a'];\n for (int i = 0; i < 26; i++)\n if (note[i] > mag[i]) return false;\n return true;\n }\n}; // 勒索信(贖金) Demande de rançon 身代金(みのしろきん)要求書 【身の代(みのしろ)】",
"memory": "9900"
} |
383 | <p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code></li>
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool canConstruct(const string& ransomNote, const string& magazine) {\n int count[256]{};\n for (char c : magazine) count[c]++;\n for (char c : ransomNote) {\n if (!count[c]--) return false;\n }\n return true;\n }\n};",
"memory": "10000"
} |
383 | <p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code></li>
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n vector<int> count(26);\n\n for (const char c : magazine)\n ++count[c - 'a'];\n\n for (const char c : ransomNote) {\n if (count[c - 'a'] == 0)\n return false;\n --count[c - 'a'];\n }\n\n return true;\n }\n};",
"memory": "10100"
} |
383 | <p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p>
<p>Each letter in <code>magazine</code> can only be used once in <code>ransomNote</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> ransomNote = "a", magazine = "b"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "ab"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> ransomNote = "aa", magazine = "aab"
<strong>Output:</strong> true
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code></li>
<li><code>ransomNote</code> and <code>magazine</code> consist of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool canConstruct(string ransomNote, string magazine) {\n sort(ransomNote.begin(), ransomNote.end());\n sort(magazine.begin(), magazine.end());\n\n int NoteLen = ransomNote.size(), MagLen=magazine.size();\n int flag=0, k=0;\n\n for (int i = 0; i<MagLen; i++){\n if (ransomNote[k] == magazine[i])\n { flag++; k++; }\n }\n return (flag==NoteLen) ? 1 : 0;\n }\n};",
"memory": "10100"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.