File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 4242| 35 | [ Search Inserted Position] ( https://leetcode.com/problems/search-insert-position/ ) | [ ![ Java] ( assets/java.png )] ( src/SearchInsertPosition.java ) [ ![ Python] ( assets/python.png )] ( python/search_insert_position.py ) | |
4343| 36 | [ Valid Sudoku] ( https://leetcode.com/problems/valid-sudoku ) | [ ![ Java] ( assets/java.png )] ( src/ValidSudoku.java ) | |
4444| 38 | [ Count and Say] ( https://leetcode.com/problems/count-and-say ) | [ ![ Java] ( assets/java.png )] ( src/CountAndSay.java ) [ ![ Python] ( assets/python.png )] ( python/count_and_say.py ) | |
45+ | 39 | [ Combination Sum] ( https://leetcode.com/problems/combination-sum ) | [ ![ Java] ( assets/java.png )] ( src/CombinationSum.java ) | |
4546| 53 | [ Maximum SubArray] ( https://leetcode.com/problems/maximum-subarray ) | [ ![ Java] ( assets/java.png )] ( src/MaximumSubArray.java ) [ ![ Python] ( assets/python.png )] ( python/maximum_sum_subarray.py ) | |
4647| 58 | [ Length of Last Word] ( https://leetcode.com/problems/length-of-last-word ) | [ ![ Java] ( assets/java.png )] ( src/LengthOfLastWord.java ) [ ![ Python] ( assets/python.png )] ( python/length_of_last_word.py ) | |
4748| 66 | [ Plus One] ( https://leetcode.com/problems/plus-one ) | [ ![ Java] ( assets/java.png )] ( src/PlusOne.java ) [ ![ Python] ( assets/python.png )] ( python/plus_one.py ) | |
Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/combination-sum
2+ // T: O(|candidates| ^ target)
3+ // S: O(target + candidate ^ 2) // not sure tho --> if anyone has any idea please feel free to contact me
4+
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ public class CombinationSum {
9+ private final List <List <Integer >> result = new ArrayList <>();
10+ private int [] candidates ;
11+ private int target ;
12+
13+ public List <List <Integer >> combinationSum (int [] candidates , int target ) {
14+ this .candidates = candidates ;
15+ this .target = target ;
16+ combinationSum (new ArrayList <>(), 0 , 0 );
17+ return result ;
18+ }
19+
20+ public void combinationSum (List <Integer > current , int sum , int index ) {
21+ if (sum == target ) {
22+ result .add (current );
23+ return ;
24+ } else if (sum > target ) return ;
25+ for (int i = index ; i < candidates .length ; i ++) {
26+ List <Integer > newCandidate = new ArrayList <>(current );
27+ newCandidate .add (candidates [i ]);
28+ combinationSum (newCandidate , sum + candidates [i ], i );
29+ }
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments