5-line Python solution, dp, beats 68.90%

  • 0
    def combinationSum2(self, candidates, target):
        dp = [set([()])] + [set([]) for i in xrange(target)]
        for c in sorted(candidates):
            for n in xrange(target, c - 1, -1):
                dp[n] |= {t + (c,) for t in dp[n - c]}
        return [list(t) for t in dp[target]]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.