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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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 }...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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;...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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 freque...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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()...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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 ...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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\toccurrenceCou...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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...
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>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2...
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)...
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 ...
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 w...
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 ...
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 ...
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 ...
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; // ...
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 ...
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...
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 ...
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);r...
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 ...
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 = ...
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 ...
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...
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 ...
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-...
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 ...
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 ...
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 ...
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;\...
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 ...
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();...
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 ...
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...
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 ...
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)));...
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 ...
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 ...
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 ...
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 ...
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 ...
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})...
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 ...
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 ...
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 ...
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 ...
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 ...
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<in...
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 ...
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<in...
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 ...
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 fo...
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 ...
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>> visi...
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 ...
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) {\...
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 ...
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.si...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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...
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 ...
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....
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 ...
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][...
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 ...
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 ...
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 ...
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 ...
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 ...
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...
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 ...
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 ...
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 co...
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 ...
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 ...
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 ...
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...
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 ...
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>...
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 ...
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;...
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 ...
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.be...
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 ...
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<in...
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 ...
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 ...
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 ...
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(mat...
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 ...
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 ...
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 ...
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<pipi...
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 ...
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<pipi...
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 ...
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; ...
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 ...
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 =...
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 ...
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...
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 ...
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 ...
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 ...
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 pr...
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 ...
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...
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 ...
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...
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 ...
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...
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 ...
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 co...
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 ...
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...
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 ...
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)...
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 ...
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<vec...
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 ...
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<in...
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 ...
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<...
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 ...
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 ...
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 ...
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++)\...
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 ...
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 unorde...
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 ...
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];...
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 ...
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...
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 ...
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....
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 *...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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:\...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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:\n...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 ListN...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 ListN...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 ListN...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 vecto...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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=r...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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:...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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:\...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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() ...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 ListN...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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 unord...
382
<p>Given a singly linked list, return a random node&#39;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-linke...
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...
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 us...
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 --letter...
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 us...
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 --letter...
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 us...
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]) re...
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 us...
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};", "memo...
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 us...
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']...
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 us...
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 = ...