Skip to main content

Posts

Leetcode 40: Combination Sum II

  1. Problem Statement (Simple Explanation): You are given: An array candidates (may contain  duplicates ). An integer target. You must return  all unique combinations  of numbers from candidates such that: The sum of the chosen numbers equals target. Each number can be used at most once  in each combination. The result must  not contain duplicate combinations . Return combinations in any order. 2. Examples: Example 1: Input: candidates = [10,1,2,7,6,1,5] target = 8 Possible unique combinations: [1,1,6] [1,2,5] [1,7] [2,6] Output: [   [1,1,6],   [1,2,5],   [1,7],   [2,6] ] Example 2: Input: candidates = [2,5,2,1,2] target = 5 Valid unique combinations: [1,2,2] [5] Output: [   [1,2,2],   [5] ] 3. Key Differences vs. Combination Sum (Problem 39): Here,  each candidate can be used at most once . candidates may contain  duplicates , but the result must have...
Recent posts

Leetcode 39: Combination Sum

  1. Problem Statement (Simple Explanation): You are given: An array of  distinct  positive integers candidates. A positive integer target. You must find  all unique combinations  of numbers from candidates such that: The sum of the chosen numbers equals target. You can use the  same number unlimited times . Two combinations are considered  different  if the count of at least one number differs. Return all combinations (order of combinations and order inside each combination does not matter). The number of unique combinations is guaranteed to be < 150. 2. Examples: Example 1: Input: candidates = [2,3,6,7], target = 7 Valid combinations: 2 + 2 + 3 = 7 → [2,2,3] 7 = 7 → [7] Output: [[2,2,3],[7]] Example 2: Input: candidates = [2,3,5], target = 8 Valid combinations: 2 + 2 + 2 + 2 = 8 → [2,2,2,2] 2 + 3 + 3 = 8 → [2,3,3] 3 + 5 = 8 → [3,5] Output: [[2,2,2,2],[2,3,3],[3,5]]...