id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int,vector<int>,greater<int>>min_heap;\n\n for(int i=0; i<nums.size(); i++){\n min_heap.push(nums[i]);\n if(min_heap.size()>k){\n min_heap.pop();\n }\...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, greater<int>> minHeap;\n for (int num : nums) {\n minHeap.push(num);\n if (minHeap.size() > k) {\n minHeap.pop();\n }\n }\n return minHeap.top(); ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue <int, vector<int>, greater<int>> min_heap;\n\n for(int i =0;i<nums.size();i++){\n if(i<k) min_heap.push(nums[i]);\n else {\n if(nums[i]>min_heap.top()){\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int,vector<int>,greater<int>> q;\n for(int i=0;i<nums.size();i++){\n //indexing start from zero\n if(q.size()<k){\n q.push(nums[i]);\n }\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "class Solution {\npublic:\n \n void insertFromRootHeap(int current_parent, vector<int>& nums,int size){\n // cout << \"now here is \"<< nums[current_parent] <<endl;\n // if (nums[current_parent] == 3){\n // cout << \"left is\" << nums[current_parent*2 + 1] <<endl;\n // cou...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "#include<queue>\nclass Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, greater<int>> qt;\n\n for(int i = 0; i<k; i++){\n qt.push(nums[i]);\n } \n for(int i = k; i<nums.size(); i++){\n int element = ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int,vector<int>,greater<int>> pq;\n for(auto num:nums){\n pq.push(num);\n if(pq.size()>k) pq.pop();\n }\n // while(pq.size()>k) pq.pop();\n return pq.top()...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
1
{ "code": "#include <iostream>\n#include <vector>\n#include <queue> // For priority_queue\n\nclass Solution {\npublic:\n int findKthLargest(std::vector<int>& nums, int k) {\n // Min-heap using priority_queue\n std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;\n \n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, greater<int>> pq; // min heap\n for (int num : nums) {\n pq.push(num);\n if (pq.size() > k) {\n pq.pop();\n }\n }\n re...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, greater<int>> minHeap;\n for(int i=0; i<k; i++){\n minHeap.push(nums[i]);\n }\n for(int i=k; i<nums.size(); i++){\n if(nums[i]>minHeap.top()){\n...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n int n = nums.size();\n priority_queue<int, deque<int>, greater<int>> minh;\n\n for(int i=0; i<n; i++){\n minh.push(nums[i]);\n\n if(minh.size()>k) minh.pop();\n\n }\n \n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n vector<int> count(20001, 0);\n\n for(int i=0; i<nums.size(); i++)\n count[nums[i]+10000]++;\n\n for(int i=20000; i>=0; i--){\n k-= count[i];\n if(k<=0) return i-10000;\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n class MaxHeap\n {\n public:\n int heapsize;\n int* arr;\n int capacity;\n \n MaxHeap(int size)\n {\n capacity = size;\n heapsize = 0;\n arr = new int[capacity];\n }\n int paren...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "struct Heap\n{\n int* arr = new int[100000];\n int n = 0;\n Heap() {}\n void insert(int x)\n {\n arr[n] = x;\n int i = n;\n while (i > 0 && arr[(i-1)/2] < arr[i])\n {\n swap(arr[(i-1)/2], arr[i]);\n i = (i-1)/2;\n }\n ++n;\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n \n //TC -> O(N * logN)\n //SC -> O(N)\n\n /*int n = nums.size();\n priority_queue<int> pq;\n\n for(int i = 0; i < n; i++){\n pq.push(nums[i]);\n }\n\n for(int i...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int>q;\n int n=nums.size();\n k=n+1-k;\n for(int i=0;i<nums.size();i++){\n q.push(nums[i]);\n if(q.size()>k){\n q.pop();\n }\n }\...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int> pq;\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n pq.push(nums[i]);\n if(i>=n-k+1) pq.pop();\n } \n return pq.top();\n\n }\n};", "memory": "...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int> a;\n for (int i = 0; i < nums.size() - k+1; i++) {\n a.push(nums[i]);\n }\n for (int i = nums.size() - k+1; i < nums.size(); i++) {\n if (a.top() > nums[i]) ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& arr, int k) {\n int n=arr.size();\n priority_queue<int> pq;\n for(int i=0;i<(n-k)+1;i++){\n pq.push(arr[i]);\n }\n for(int i=n-k+1;i<n;i++){\n int x=pq.top();\n if(x > arr[i]){...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n#if 1 // max-heap\n int findKthLargest(vector<int>& nums, int k) {\n int n=nums.size(), i;\n priority_queue<int, vector<int>, less<int>> p_queue; // max-heap\n // pop if heap size > n-k+1\n for(i=0; i<n; i++)\n {\n p_queue.push(n...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n if (nums.size() < k) return -1; // can't have nums have less than k numbers\n\n // If k is small, use a min-heap to find the k-th largest element.\n if (k >= nums.size() / 2) {\n priority_queue<i...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\n int left(int i) {\n return (i+1)*2-1;\n }\n int right(int i) {\n return (i+1)*2;\n }\n int parent(int i) {\n return (i+1)/2-1;\n }\n void go_up(vector<int>& heap, int i) {\n if(parent(i) < 0)\n return;\n if(heap[i] > heap...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n class MaxHeap {\n private:\n vector<int>heap;\n\n public:\n MaxHeap() {\n heap = vector<int>();\n }\n\n void heapifyUp() {\n int index = heap.size() - 1, parent;\n\n while (...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n void pull_up(vector<int> &heap, int n){\n if(heap.size() == 1 || n==0) return;\n int parent = (n-1)/2;\n if(heap[n] > heap[parent]){\n swap(heap[parent], heap[n]);\n pull_up(heap, parent);\n }\n \n }\n \n void ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n class MaxHeap {\n private:\n vector<int>heap;\n\n public:\n MaxHeap() {\n heap = vector<int>();\n }\n\n void heapifyUp() {\n int index = heap.size() - 1, parent;\n\n while (...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n class MaxHeap {\n private:\n vector<int>heap;\n\n public:\n MaxHeap() {\n heap = vector<int>();\n }\n\n void heapifyUp() {\n int index = heap.size() - 1, parent;\n\n while (...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n class MaxHeap {\n private:\n vector<int>heap;\n\n public:\n MaxHeap() {\n heap = vector<int>();\n }\n\n void heapifyUp() {\n int index = heap.size() - 1, parent;\n\n while (...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Comparator{\npublic:\n bool operator()(const int & int1, const int & int2){\n return (int1 <= int2);\n }\n\n};\n\nclass Solution {\npublic:\n int findKthLargest(std::vector<int>& nums, int k) {\n if(k==50000) return 1;\n\n std::priority_queue<int, std::vector<int>, Compa...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n vector<int> vectie = nums; \n sort(vectie.rbegin(), vectie.rend());\n return vectie[k - 1];\n }\n};\n", "memory": "63700" }
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int solve1(vector<int>&nums, int k){\n priority_queue<int>pq;\n for(auto i:nums) pq.push(i);\n\n int i=1;\n while(i<k){\n pq.pop();\n i++;\n }\n return pq.top();\n }\n\n int solve2(vector<int>nums, int k){\...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n vector<int>ans;\n priority_queue<int, vector<int>, greater<int>>pq;\n for(int i=0; i<k; i++)\n {\n pq.push(nums[i]);\n }\n\n for(int i=k; i<nums.size(); i++)\n {\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n QuickSort(nums,0,nums.size()-1);\n return nums[k-1];\n\n }\n void QuickSort(vector<int> & nums,int low,int high){\n // low = 0,high = nums.size()-1;\n if(low < high){\n int position ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int, vector<int>, greater<int>> max_heap;\n for(int num : nums)\n {\n if(max_heap.size() < k)\n max_heap.push(num);\n else if(max_heap.top() < num)\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int> pq;\n int n = nums.size();\n for(int i=0;i<n;i++){\n pq.push(nums[i]);\n } \n int kth ;\n while(k--){\n kth = pq.top();\n pq.pop();\n \n\n }\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int> pq;\n for (auto i : nums) {\n pq.push(i);\n }\n while (--k) {\n pq.pop();\n }\n return pq.top();\n }\n};", "memory": "64200" }
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int>q;\n for(int i=0;i<nums.size();i++){\n q.push(nums[i]);\n }\n for(int i=0;i<k-1;i++){\n q.pop();\n }\n return q.top(); \n }\n};", "memory": ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n // sort(nums.begin(), nums.end());\n // int n = nums.size();\n // int ans = nums[n-k];\n // return ans;\n priority_queue<int> pq;\n for(int i = 0; i < nums.size(); i++) pq.push(nums[i])...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
2
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n // sort(nums.begin(),nums.end(),greater<int>());\n // return nums[k-1];\n priority_queue<int> pq;\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n }\n int x=-1;\n ...
215
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
3
{ "code": "class Solution {\npublic:\n int findKthLargest(vector<int>& nums, int k) {\n priority_queue<int>pq;\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n }\n int poper=0;\n while(poper!=k-1){\n pq.pop();\n poper++;\n }\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n const int total_combinations = 1 << 9;\n std::vector<std::vector<int>> all_combinations;\n for (int i = 0; i < total_combinations; ++i) {\n int sum = 0;\n int total_bits = 0;\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n \n void solve(vector<int> &nums, int k, int n, int start, vector<int> &combination, vector<vector<int>> &result){\n if(n < 0) return;\n if(combination.size() == k && n == 0){\n result.push_back(combination);\n return;\n }\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n\n void findComb(int index,vector<int>&nums,vector<vector<int>>&ans,vector<int>&temp,int k,int sum){\n if(sum==0 && temp.size()==k){\n ans.push_back(temp);\n return;\n }\n if(sum<0 || temp.size()>k){\n return;\n }\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n std::vector<std::vector<int>> combinationSum3(int k, int n) {\n std::vector<std::vector<int>> result;\n std::vector<int> combination;\n std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};\n backtrack(nums, k, n, 0, combination, result);\n r...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\nvoid func(vector<vector<int>> &ans,vector<int> &curr,vector<int> &arr,int k,int target,int index)\n{\n if(curr.size()==k && target==0)\n {\n ans.push_back(curr);\n return;\n }\n for(int i=index;i<arr.size();i++)\n {\n if(target<arr[i])continue;...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n\n vector<vector<int>>ans;\n\n void solve(vector<int>&v,int idx,int n,vector<int>&tmp,int k)\n {\n if(n<0) return;\n\n if(n==0)\n {\n if(tmp.size()==k)\n ans.push_back(tmp);\n return;\n }\n\n for(int j=i...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\n void solve(int k, int n, int idx, vector<int> &arr, vector<int> &temp, vector<vector<int>> &ans) {\n \n \n\n if (idx == arr.size()) {\n if (temp.size() == k && n == 0) {\n ans.push_back(temp);\n }\n return;\n }...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n std::vector<std::vector<int>> combinationSum3(int k, int n) {\n std::vector<std::vector<int>> result;\n std::vector<int> combination;\n std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};\n backtrack(nums, k, n, 0, combination, result);\n r...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> ans;\n void solve(int k, int n, vector<int> &v, vector<int> &a, int i, int c)\n {\n if(i > 9) return;\n \n if(i == 9) {\n \n int s = accumulate(v.begin(),v.end(),0);\n \n if(s == n && v.size() == k)\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n void solve(vector<vector<int>> &ans, vector<int> &s, int k, int n, int start)\n {\n // Base case: when the combination is of size `k` and the sum matches `n`\n if (s.size() == k && n == 0) {\n ans.push_back(s);\n return;\n }\n\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
0
{ "code": "class Solution {\npublic:\n void solve(int k, int n, vector<int> &v, vector<vector<int>> &ans) {\n if(k == 0) {\n if(n == 0) ans.push_back(v);\n return;\n }\n\n if(v.size() == 0) {\n for(int i = 1; i <= 9; i++) {\n if(n >= i) {\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\n void backtrack(int k, int n, vector<int> combination, vector<vector<int>>& results) {\n if (k < 0 || n < 0) return;\n if (k == 0 && n == 0) {\n results.push_back(combination);\n return;\n }\n\n for (int i = combination.empty()...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\nprivate:\n void solve(int ind , vector<int> & path , vector<vector<int>> & res , int target , int k) {\n if(path.size() == k) {\n if(target == 0) {\n res.push_back(path);\n }\n return;\n }\n if(target <= 0 || ind == 1...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\n void solve(int k,int n,vector<vector<int>>&ans,vector<int>temp){\n if(n==0 && temp.size()==k){\n ans.push_back(temp);\n }\n for(int i=1;i<=9;i++){\n if(temp.size() && temp.back()<=i)continue;\n if(i>n)break;\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\n void solve(int i,int k, int n,vector<vector<int>>&ans,vector<int>&ds){\n if(n==0 && k==0){\n ans.push_back(ds);\n return;\n }\n // if(k==0 && n!=0)return;\n if(i>9 || i>n)return;\n if(n>=i){\n ds.push_back(i)...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\n void backtrack(int k, int n, vector<int> combination, vector<vector<int>>& results) {\n // cout << endl << \"--- New stack frame ---\" << endl;\n // cout << \"Digits left: \" << k << endl;\n\n if (k < 0 || n < 0) return;\n if (k == 0 && n == 0) {\n...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\nvoid helper(int i,int k,int n, vector<vector<int>>&ans,vector<int>&temp){\n if(k==0 && n==0) {\n ans.push_back(temp);\n return;\n }\n if(i>9 || k<0 || n<0) return;\n temp.push_back(i);\n helper(i+1,k-1,n-i,ans,temp);\n temp.pop_back();\n helper(...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\n void backtrack(int k, int n, bool digitSeen[9], vector<int> combination, vector<vector<int>>& results) {\n cout << endl << \"--- New stack frame ---\" << endl;\n cout << \"Digits left: \" << k << endl;\n\n if (k < 0 || n < 0) return;\n if (k == 0 &...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
2
{ "code": "class Solution {\npublic:\n void backtrack(int k, int n, vector<int> combination, vector<vector<int>>& results) {\n if (k < 0 || n < 0) return;\n if (k == 0 && n == 0) {\n results.push_back(combination);\n return;\n }\n\n for (int i = combination.empty()...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void backtrack(int k, int n, vector<int> combination, vector<vector<int>>& results) {\n if (k < 0 || n < 0) return;\n if (k == 0 && n == 0) {\n results.push_back(combination);\n return;\n }\n\n for (int i = combination.empty()...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n\n void combination3SumUtil(int k, int n, int start, int currSum, vector<int> temp, vector<vector<int>> &res) {\n if(k==0) {\n if(currSum == n) {\n res.push_back(temp);\n }\n return;\n }\n\n for(int i=start; ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n vector<vector<int>> res;\n vector<int> temp;\n\n function<void(int, int)> dfs = [&](int start, int sum) {\n if(sum == 0 && temp.size() == k) {\n res.emplace_back(temp);\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n \n vector<vector<int>> res;\n vector<int> temp;\n\n function<void(int, int)> dfs = [&](int start, int sum) {\n if(sum =...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\nint sum1(vector<int>a){\n int sum=0;\n for(int aa:a){\n sum+=aa;\n }\n return sum;\n}\nvoid backtrack(int start, vector<int>& nums, vector<int>& path, vector<vector<int>>& result,int k, int n) {\n for (int i = start; i < nums.size(); ++i) {\n int sum=...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n\n void cSum(vector<vector<int>> &res, vector<int>&temp, int k, int n, int i){\n if(i>9){\n if(n==0 && temp.size()==k){\n res.push_back(temp);\n }\n return;\n }\n\n temp.push_back(i);\n\n cSum(res, tem...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n // Try to add numbers starting s down to 0 to the existent vector\n // with k and n parameters\n // Return the set of the successful child vectors\n vector<vector<int>> add_ok(const vector<int>& prev, int s) {\n vector<vector<int>> rval;\n // count the ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n vector<int> v;\n vector<vector<int>> ans;\n generate(1, 0, k, n, v, ans);\n return ans;\n }\nprivate:\n void generate(int i, int sum, int k, int n, vector<int> &v, vector<vector<int >> &an...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\nprivate:\n void recsol(int k, int target, vector<int> nums, vector<vector<int>>& result, vector<int> combination, int s,int digits, int index){\n if(target==0 && digits==k){\n result.push_back(combination);\n return;\n }\n else{\n f...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void findAllCombinations(vector<vector<int>> &ans, vector<int> &currCombo, vector<int> nums, vector<bool> &visited, int n, int k, int sum){\n //base case\n if(currCombo.size() == k){\n if(sum == n){\n ans.push_back(currCombo);\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\n pair<bool, vector<vector<int>>> backtrack(int k, int start_digit, int n) {\n if (k == 0) {\n if (n == 0)\n return {true, {{}}};\n else\n return {false, {{}}};\n }\n\n vector<vector<int>> ans;\n for (int d ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void backtrack(vector<int>comb, int sum, vector<vector<int>>& ans,int k, int n){\n if(comb.size()==k){\n if(sum==n){\n ans.push_back(comb);\n }\n }\n else{\n for(int i=comb[comb.size()-1]+1;i<10;i++){\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void combo(int index,int k,int n,vector<int> nums,vector<int> ans,vector<vector<int>> &res){\n if(n < 0 || ans.size() > k) return;\n if(n==0 && ans.size()==k){\n res.push_back(ans);\n return;\n }\n \n \n \n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n set<vector<int>> result;\n vector<int> temp;\n vector<vector<int>>f;\n for(auto i = 1; i < n; i++){\n dfs(k, n, result, temp, i, 0);\n }\n for(auto i : result){\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void func (int k, int n,vector<vector<int>> & ans,vector<int> temp,\n int sum,int i){\n if (k==0){\n if (sum==n) ans.push_back(temp);\n }\n for (int j=i;j<10;j++){\n temp.push_back(j);\n func (k-1,n,ans,temp,sum+...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void backtrack(vector<int>comb, int sum, vector<vector<int>>& ans,int k, int n){\n if(comb.size()==k){\n if(sum==n){\n ans.push_back(comb);\n }\n }\n else{\n for(int i=comb[comb.size()-1]+1;i<10;i++){\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\n void dfs(vector<int>nums,vector<vector<int>>&result,int index,int k,int n,vector<int>temp)\n {\n if(n<0)\n return;\n if(temp.size()==k && n==0)\n { \n result.push_back(temp);\n return;\n }\n for(int i=index;i<nums.siz...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\n void dfs(vector<int>nums,vector<vector<int>>&result,int index,int k,int n,vector<int>temp)\n {\n if(n<0)\n return;\n if(temp.size()==k && n==0)\n { \n result.push_back(temp);\n return;\n }\n for(int i=index;i<nums.siz...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\nint sum=0;\n void fun(int ind,int sum,int k,int n,vector<vector<int>> &ans,vector<int> &ds)\n {\n if(ds.size()==k )\n {\n if(sum==n)\n {\n ans.push_back(ds);\n }\n return ;\n }\n if(ds.size()>k) return;\n \n for(int ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void chek(vector<vector<int>> &c,vector<int> &a,vector<bool> x,int k,int s){\n if(k==0){\n if(s==0) c.push_back(a);\n return;\n }\n for(int i=1;i<10;i++){\n if(x[i]) continue;\n a.push_back(i);\n x[i]...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n bool check(int n, int k, map<int, bool> &mark, int i, vector<int> &tmp,int loc){\n if (mark[i] == true) return false;\n if (i > n) return false;\n if (loc ==0) return true;\n if (i < tmp[tmp.size() -1]) return false;\n return true;\n }\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> res;\n void dfs(vector<int>& path, int idx, int k, int n, int sum) {\n if(path.size()==k) {\n if(sum==n) {\n res.push_back(path);\n }\n \n return;\n }\n if(path.size()>k...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void f(int i,int t,int n,vector<int>candidates,vector<vector<int>>&ans,vector<int>&ans1,int k)\n {\n if(t==0)\n {\n if(ans1.size()==k)\n {\n ans.push_back(ans1);\n }\n return;\n }\n if(i...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int length, int target) {\n\n vector<vector<int>> results;\n\n std::queue<std::pair<std::vector<int>, int>> q;\n q.push(std::make_pair(std::vector<int>(), 1));\n\n while (!q.empty())\n {\n auto ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void func(vector<vector<int>>& l,int k,int n,vector<int> v,vector<bool> b,int ind,int sum){\n cout<<k<<\" \"<<n<<\" \"<<ind<<\" \"<<sum<<endl;\n if(ind==k){\n if(sum==n){\n l.push_back(v);\n }\n return;\n }\...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\nprivate:\n void combination_helper(vector<vector<int>> &res, vector<int> &cur_combination, int k, int target, vector<bool> used_num, int cur_index){\n if(k == 0) {\n //cout << \"k == 0\" << endl;\n return;\n }\n\n //vector<int> tmp_combination...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n set<int> nums{1,2,3,4,5,6,7,8,9};\n vector<int> combination;\n vector<vector<int>> combinations;\n backtrack(k, n, 0, 0, nums, combination, combinations);\n return combinations;\n }\np...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n map<vector<int>,int> mp;\n vector<vector<int>> ans;\n void fun(int st,int k,int n,map<int,int> temp,vector<int> t){\n if(n==0){\n if(k==0){\n if(mp.find(t)==mp.end()){\n ans.push_back(t);\n }\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n int findCombo(int kp, map <string, string> &m ,int k, int n, vector<vector<int>> &res, int sum, int start, int *idx, string s, int *len) {\n int ret = -1;\n if (s != \"\" && m[s].length() != 0) {\n start = (m[s][m[s].length() -1] - '0') + 1;\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "// class Solution {\n// public:\n// std::vector<std::vector<int>> combinationSum3(int k, int n) {\n// std::vector<std::vector<int>> result;\n// std::vector<int> combination;\n// std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};\n// backtrack(nums, k, n, 0, combination...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n int findCombo(int kp, map <string, string> &m ,int k, int n, vector<vector<int>> &res, int sum, int start, int *idx, string s, int *len) {\n int ret = -1;\n if (s != \"\" && m[s].length() != 0) {\n start = (m[s][m[s].length() -1] - '0') + 1;\n ...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> f(int i, vector<int> &nums, int k, int n) {\n // Base case: if we found a valid combination\n if (n == 0 && k == 0) {\n return {{}};\n }\n // If we run out of elements or need more numbers than available\n if (...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void backtrack(int cur,vector<vector<int>>&v1,vector<int>v2,vector<int>v){\n for(int i=cur;i<v.size();i++){\n v2.push_back(v[i]);\n v1.push_back(v2);\n // v1.push_back(v2);\n backtrack(i+1,v1,v2,v);\n v2.pop_back()...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n vector<vector<int>> v;\n for(int i=1;i<=9;i++){\n way(k,v,n,{},i);\n }\n \n return v;\n }\n map<vector<int>,int> mp;\n void way(int k ,vector<vector<int>>& v,int targe...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> combinationSum3(int k, int n) {\n vector<vector<int>> res;\n vector<bool> curr(10, false);\n\n combine(res, k, n, 0, 1, curr, {});\n return res;\n }\n\n void combine (vector<vector<int>>& res, int k, int sum, int curr_sum,...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> ans;\n void f(int idx,int prev,int n,set<int> s){\n if(idx==0){\n vector<int> curr;\n for(int i=1;i<=9;i++){\n if(s.find(i)==s.end()){\n curr.push_back(i);\n }\n }\...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n\n void helper(vector<vector<int>>& res,vector<int> nums, int count,int n) {\n if (count<=0||nums.empty()) {return;}\n if (nums.back()>n) {return;}\n // exclude the last value in nums\n int last = nums.back();\n nums.pop_back();\n if (...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void gen(int num, vector<int> &arr, int sum, int target, vector<vector<int>> &res, int k){\n if(num == 10){\n if(sum == target && arr.size() == k) res.push_back(arr);\n return;\n }\n if(arr.size() == k){\n if(sum == target...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void gen(int num, vector<int> &arr, int sum, int target, vector<vector<int>> &res, int k){\n if(num == 10){\n if(sum == target && arr.size() == k) res.push_back(arr);\n return;\n }\n if(arr.size() == k){\n if(sum == target...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void func(int index,int target,int size,vector<int> &nums,vector <int> temp,vector <vector<int>>&ans){\n if(target ==0 && size == 0){\n //temp contains the answer;\n ans.push_back(temp);\n temp.clear();\n return;\n }\n...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void gen(int num, vector<int> &arr, int sum, int target, vector<vector<int>> &res, int k){\n if(num == 10){\n if(sum == target && arr.size() == k) res.push_back(arr);\n return;\n }\n if(arr.size() == k){\n if(sum == target...
216
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
3
{ "code": "class Solution {\npublic:\n void rec(int k, int n, int i, vector<int> curr, vector<vector<int>>& res)\n {\n if (k == 0)\n {\n if (n == 0) res.push_back(curr);\n return;\n }\n if (n <= 0 || i >= 10) return;\n\n curr.push_back(i);\n rec(k ...