-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from Amit-yadav099/master
Added solution for <Combination-SUM III> #174
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
combination Sum-III | ||
|
||
Link of the problem :-https://leetcode.com/problems/combination-sum-iii/description/ | ||
|
||
Find all valid combinations of k numbers that sum up to n such that the following conditions are true: | ||
|
||
Only numbers 1 through 9 are used. | ||
Each number is used at most once. | ||
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order. | ||
|
||
|
||
|
||
Intutuion: we have to select 'K' numbers from the given vector such that their sum is euqal to given number 'n'. | ||
we need to count such possible pair of numbers and return it. | ||
so we need to check for every possible pair individually ,to do so ,recursion is the first thing that come into the mind. | ||
However,there are also overlapping cases, so do this wouldn't enough to pass all the test cases so we need to improve it by memoization ,i.e, by the use of dynamic programming | ||
|
||
|
||
//conecpt used:- dynamic programming along with the backtracking to cover all the possible cases. | ||
|
||
Example 1: | ||
|
||
Input: k = 3, n = 7 | ||
Output: [[1,2,4]] | ||
Explanation: | ||
1 + 2 + 4 = 7 | ||
There are no other valid combinations. | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
|
||
//we make a 2D vector "ans" to to collect the all the right possible answer given the 1D vector temp | ||
vector<vector<int>>ans; | ||
|
||
//helper function to do the required | ||
void helper(int sum,int start,int k,int n,vector<int>&temp){ | ||
//base case | ||
//when size becomes k , we check whether our sum is equals to n or not | ||
// if it is equal to n, then we got the answer and push the vector temp into the 2D vector ans | ||
// if it is not equals to n, then we return , as this would not be in our answer | ||
if( temp.size()==k){ | ||
if(sum==n){ | ||
ans.push_back(temp);} | ||
|
||
return ; | ||
} | ||
|
||
//here we start the loop form 1 to 9 in order to enquire about the possible cases | ||
for(int i=start;i<=9;i++){ | ||
//option-01 | ||
temp.push_back(i); | ||
helper(sum+i,i+1,k, n,temp); | ||
//here after the work , if for the given indexwe got the answer then wew defintely get saved into 2D vector 'ans' | ||
//then we pop the value for the current index , so that for next iteration we make temp frm the that value | ||
temp.pop_back(); | ||
} | ||
} | ||
|
||
|
||
|
||
vector<vector<int>> combinationSum3(int k, int n) { | ||
vector<int>temp; | ||
int sum=0; | ||
helper(sum, 1,k, n,temp); | ||
return ans; | ||
} | ||
}; |