prepbook
  • Introduction
  • Some common stuff
    • python __repr__
    • HackerRank input tips
  • Data Structures and Algorithms
    • Breadth first search
    • Depth First Search
    • Dijkstra
    • A* Search Algorithm
    • Binary Search
    • python counter
    • Sorting
      • Merge Sort
      • Quick Sort
    • Priority Queue
  • Multiprocessing vs Threading
  • Common Coding
    • Find loop in lin list
    • Maximum sum subarray
  • Coding
    • Valid palindrome
    • Palindrome number
    • Remove duplicates from sorted array
    • Island perimeter
    • Serialize and Deserialize Binary Tree
    • Valid Soduku
    • Word Pattern
    • Word Pattern II
    • Group Anagrams
    • Implement Trie
    • Deep copy list with random node
    • Palindrome Permutation
    • Combination Sum
    • Clone Graph
    • Generate parenthesis
    • Fibonacci Number
    • LRU Cache
    • Merge two sorted arrays in place
    • Hamming Distance
    • Merge K sorted arrays
    • Kth smalles element in BST
    • Kth largest element in an array
    • Remove duplicates from sorted list
    • Power of 2
    • Nested list weight sum
    • SIngle number in a list
    • Factor combinations
    • Delete node from BST
  • hacker Rank
    • Coding
      • print staircase
      • Drawing book
      • Challenge 0
      • Min-Max sum
  • WorkRelatedCoding
    • Rectangle Overlap
  • Python tips
Powered by GitBook
On this page

Was this helpful?

  1. Coding

Combination Sum

Given a set of candidate numbers (C)(without duplicates)and a target number (T), find all unique combinations in C where the candidate numbers sums toT.

Thes ame repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.

  • The solution set must not contain duplicate combinations.

For example, given candidate set[2, 3, 6, 7]and target7, A solution set is:

[
  [7],
  [2, 2, 3]
]
class Solution(object):
    def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """

        retList = []
        if not candidates:
            return []
        if not target:
            return []

        self.combinationSumHelper(candidates, target, retList, [], 0)
        return retList

    def combinationSumHelper(self, candidates, target, retList, curList, start):

        #res = 0
        #print 'curList: ', curList, ' target: ', target
        if target < 0:
            return

        if target == 0:
            # Note: remember to copy over the curList using the [:] slicing technique for lists
            retList += [curList[:]]
            #print 'retList: ', retList
            return

        for idx in xrange(start, len(candidates)):
            elem = candidates[idx]
            # Note: Two ways of doing, either make a new list or if the same list is used then do a pop()
            #       after it returns

            #newList = curList + [elem]
            #print 'newList: ', newList
            #self.combinationSumHelper(candidates, (target - elem), retList, newList, idx)

            curList += [elem]
            self.combinationSumHelper(candidates, (target - elem), retList, curList, idx)
            curList.pop()
PreviousPalindrome PermutationNextClone Graph

Last updated 5 years ago

Was this helpful?