Skip to content

Commit 57f1e87

Browse files
committed
Day - 85 work
1 parent 28efbf1 commit 57f1e87

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 122 |
10-
| Current Streak | 84 days |
11-
| Longest Streak | 84 ( August 17, 2015 - November 08, 2015 ) |
9+
| Total Problems | 123 |
10+
| Current Streak | 85 days |
11+
| Longest Streak | 85 ( August 17, 2015 - November 09, 2015 ) |
1212

1313
</center>
1414

@@ -198,6 +198,7 @@ Include contains single header implementation of data structures and some algori
198198
| Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.| [maxProfitStock.cpp](leet_code_problems/maxProfitStock.cpp)|
199199
| Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.| [minPath.cpp](leet_code_problems/minPath.cpp)|
200200
| Count the number of prime numbers less than a non-negative number, n.| [countPrimes.cpp](leet_code_problems/countPrimes.cpp)|
201+
| Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example : for k = 3, n = 9 result would be [[1,2,6], [1,3,5], [2,3,4]], similarly for k = 3, n = 7, result would be [[1,2,4]]. | [combinationSum3.cpp](leet_code_problems/combinationSum3.cpp) |
201202

202203

203204

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used
3+
* and each combination should be a unique set of numbers.
4+
*
5+
* Ensure that numbers within the set are sorted in ascending order.
6+
* Example:
7+
* Example 1:
8+
*
9+
* Input: k = 3, n = 7
10+
*
11+
* Output:
12+
* [[1,2,4]]
13+
*
14+
* Input: k = 3, n = 9
15+
*
16+
* Output:
17+
* [[1,2,6], [1,3,5], [2,3,4]]
18+
*/
19+
20+
#include <iostream>
21+
#include <vector>
22+
23+
24+
void combination( std::vector<std::vector<int>> & results, std::vector<int> & curr, int k, int n, int idx ) {
25+
if ( k == 0 && n == 0 ) {
26+
results.push_back(curr);
27+
return;
28+
}
29+
30+
for ( int i = idx; i <= 10 - k && i <= n; ++i ) {
31+
curr.push_back(i);
32+
combination(results, curr, k - 1, n - i, i + 1 );
33+
curr.pop_back();
34+
}
35+
36+
}
37+
38+
39+
std::vector<std::vector<int>> combinationVec( int k, int n ) {
40+
std::vector<std::vector<int>> results;
41+
std::vector<int> curr;
42+
combination(results, curr, k, n, 1);
43+
return results;
44+
}
45+
46+
void printVecVec( std::vector<std::vector<int>> & vec ) {
47+
for ( auto v : vec ) {
48+
std::cout << "[ ";
49+
for ( auto e : v ) {
50+
std::cout << e << " ";
51+
}
52+
std::cout << "]" << std::endl;
53+
}
54+
}
55+
56+
57+
int main() {
58+
std::vector<std::vector<int>> results;
59+
std::cout << "Combinations for k = 3, n = 7\n";
60+
results = combinationVec(3,7);
61+
printVecVec(results);
62+
results.clear();
63+
std::cout << "\nCombinations for k = 3, n = 9\n";
64+
results = combinationVec(3,9);
65+
printVecVec(results);
66+
return 0;
67+
}
68+
69+

leet_code_problems/run

25.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)