Skip to content

Commit

Permalink
Added solution for 39. Combination Sum: https://leetcode.com/problems…
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Nov 21, 2024
1 parent 02b57ec commit 670f858
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 19 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "460"
endingLineNumber = "460"
startingLineNumber = "462"
endingLineNumber = "462"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "426"
endingLineNumber = "426"
startingLineNumber = "428"
endingLineNumber = "428"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "351"
endingLineNumber = "351"
startingLineNumber = "353"
endingLineNumber = "353"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "319"
endingLineNumber = "319"
startingLineNumber = "321"
endingLineNumber = "321"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -110,8 +110,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "281"
endingLineNumber = "281"
startingLineNumber = "283"
endingLineNumber = "283"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "244"
endingLineNumber = "244"
startingLineNumber = "246"
endingLineNumber = "246"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "203"
endingLineNumber = "203"
startingLineNumber = "205"
endingLineNumber = "205"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -174,8 +174,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "192"
endingLineNumber = "192"
startingLineNumber = "194"
endingLineNumber = "194"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "178"
endingLineNumber = "178"
startingLineNumber = "180"
endingLineNumber = "180"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -388,5 +388,21 @@
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "FCF4AD65-9ED6-4AD6-9963-851A5D2EFAFD"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "algorithm/number.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "2016"
endingLineNumber = "2016"
landmarkName = "combination_sum(input, target, candidate, current_index, output)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
4 changes: 3 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "trie.hpp"

int main(int argc, const char* argv[]) {
invoke_generate_parentheses();
invoke_combination_sum();

// invoke_generate_parentheses();

// invoke_find_peak_element();

Expand Down
58 changes: 58 additions & 0 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1985,3 +1985,61 @@ void invoke_find_peak_element() {
std::vector<int> input = {1,2,3,1};
std::cout<<"The peak element is: "<<find_peak_element(input);
}

/*
Input: [2,3], Target: 6, Output: [[2,2,2],[3,3]]
Process:
sort the input
2 3
2(+2) 4+3!
6(+4)* 2+3
2+3+3!
3
3+3*
Input: [2,3], Target: 7, Output: [[2,2,2],[3,3]]
Process: sort the input
2 3
2+2 ->4+3* 3
2+4! 2+3 3+3
5+3! 6+3!
Input: [2,3,6,7], Target: 7, Output: [[2,2,3],[7]]
2 3 6 7
4 /6+3! 6 12!
6 /4+3 9!
8!
*/
void combination_sum(std::vector<int>& input, int target, std::vector<int> candidate,
int current_index, std::vector<std::vector<int>>& output) {
if(target == 0) {
output.push_back(candidate);
return;
}

for(int index = current_index; index < input.size(); ++index) {
int current_number = input[index];
if(current_number > target) { // 2>7, 2>5, 2>3, 2>1?true
return;
}

candidate.push_back(current_number); // 2, 2, 2
combination_sum(input, target - current_number, candidate, index, output); // target: 7-2=5, 5-2=3, 3-2=1
candidate.pop_back();
}
}
void invoke_combination_sum() {
std::vector<std::vector<int>> output;
std::vector<int> input = {2,3,6,7};
combination_sum(input, 7, {}, 0, output);
std::cout<<"Combination sum: "<<"[";
std::for_each(output.begin(), output.end(), [](std::vector<int> numbers) {
std::cout<<"[";
for(auto& number: numbers) {
std::cout<<number<<", ";
}
std::cout<<"]";
});
std::cout<<"]";
}
5 changes: 5 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,9 @@ void invoke_search_a_2d_matrix();
*/
void invoke_find_peak_element();

/*
39. Combination Sum: https://leetcode.com/problems/combination-sum
*/
void invoke_combination_sum();

#endif /* NUMBER_HPP */

0 comments on commit 670f858

Please sign in to comment.