Customer Support: firstname.lastname@example.org
We are listening to your every feedback,
and taking action to constantly improve your learning experience.
If you have any feedback, please use this form:
- If you are a student, email to email@example.com to get special student discount.
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
Input: candidates = [10,1,2,7,6,1,5], target = 8
[ [1,1,6], [1,2,5], [1,7], [2,6] ]
Input: candidates = [2,5,2,1,2], target = 5
[ [1,2,2],  ]
- NOTE: I highly recommend going through the Backtracking chapters in the order they are given in the Index page to get the most out of it and be able to build a rock-solid understanding.
Solution for this problem would be very similar to that of Combination Sum but there would be one big difference to ensure correct algorithm and bug free code. Here we are allowed to use an element only once. So when we d a backtracking on index and recursively call
backtrack( ... )method to see if the current partial solution leads to a complete solution, we should exclude the current index and move on the next index to make sure the element at current index is not included again.
This is a Premium content. Please subscribe to access the code.
After subscribing please come back and refresh this page.
Don't forget to take in-depth look at the other backtracking problems because that is what would make you comfortable with using the backtracking template and master the art of Backtracking:
- Letter Case Permutation
- Power Set
- All Paths Between Two Nodes
- Word Search
- Word Square
- Generate Parentheses
The above content is written by:
If you have any feedback, please use this form: https://thealgorists.com/Feedback.
Follow Us On LinkedIn