Giter Club home page Giter Club logo

combinationsum's Introduction

CombinationSum

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 to T.

The same 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 target 7, A solution set is: [ [7], [2, 2, 3] ]

Solution:

  1. An integer array and target value is received as function parameter.

  2. Sort the array

  3. Create a function which will be recursive function. The function will have following parameters

    • sorted array
    • target value
    • a list of list which will hold the results
    • a new list which will hold result
    • start index

Calling the function will look like this: combinationSum(candidates, target, results, new ArrayList(), 0); Function declaration will look like this: combinationSum(int[] candidates, int target, List<List> results, List result, int start)

  1. In the recursive function it is checked if the target value is greater than 0, if yes then we loop starting from the start position which is passed in as function parameter. It is checked in the loop condition if we have reached the end of sorted array of values and the target is greater than or equal to the i'th position of sorted array, and i is incremented.

  2. In the loop we add the sorted array i'th value to the result and call the function making it a recursive call. In recursive call instead of passing target we will pass target - sortedArray[i].

  3. In the next step we remove the last value from result.

  4. Lastly if the target value is equal to 0 then we add the result in to list of lists.

This algorithm has time complexity

O((n+k)!) 
where n is the size of candidates, and k is the max repeated times for each candidates
and space complexity 
O(m) where m is the size of array for the solution.

combinationsum's People

Contributors

alihassan89 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.