Skip to content

Commit

Permalink
permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
elarabyelaidy19 committed May 15, 2022
1 parent 2a1543c commit 1de7fad
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
16 changes: 1 addition & 15 deletions Array/Arrays2.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,5 @@ public List<List<Integer>> threeSum(int[] nums) {
}


// // generate all "non-duplicates" subsets of a given array
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> output = new ArrayList<>();
output.add(new ArrayList<Integer>());

for (int num : nums) {
int size = output.size();
for (int i = 0; i < size; i++) {
List<Integer> subset = new ArrayList<>(output.get(i));
subset.add(num);
output.add(subset);
}
}
return output;
}

}
3 changes: 0 additions & 3 deletions Array/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,3 @@ public class Solution {

## [3sum](https://leetcode.com/problems/3sum/)
- sort, two pointers, if sum < 0, move left, if sum > 0, move right.

## [subsets](leetcode.com/problems/subsets)
-
23 changes: 23 additions & 0 deletions Backtracking/BackTracking.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,28 @@ public void backtrack(List<List<Integer>> outList, List<Integer> subList, int[]
subList.remove(subList.size() - 1);
}

}

// =========================================================================
// genertae all balnced parenthsis of length n
public List<String> generateParenthsis(int n) {
List<String> out = new ArrayList<>();
backtrack(out, "", 0, 0, n);
return out;
}

public void backtrack(List<String> out, String str, int open, int close, int max) {
// base case when open and close parenthes is complete add to out return
if(str.length() == 2 * max) {
out.add(str);
return;
}

if(open < max) {
backtrack(out, str+"(", open+1, close, max);
}
if(open > close) {
backtrack(out, str+")", open, close+1, max);
}
}
}
9 changes: 7 additions & 2 deletions Backtracking/backtrack.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
## [subsets](leetcode.com/problems/subsets)
-
## [subsets](https://leetcode.com/problems/subsets)
-

## [generateParenthesis] (https://leetcode.com/problems/generate-parentheses/)
- base case when str == max length * 2, open + close parenthsis
- num open parenthsis must equal num close parenthsis.
- n = 3 =>> “((()))”, “(()())”, “(())()”, “()(())”, “()()()”
20 changes: 20 additions & 0 deletions Subsets/Subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.ArrayList;
import java.util.List;

public class Subsets {
// // generate all "non-duplicates" subsets of a given array
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> output = new ArrayList<>();
output.add(new ArrayList<Integer>());

for (int num : nums) {
int size = output.size();
for (int i = 0; i < size; i++) {
List<Integer> subset = new ArrayList<>(output.get(i));
subset.add(num);
output.add(subset);
}
}
return output;
}
}
4 changes: 4 additions & 0 deletions Subsets/subsets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## [subsets](leetcode.com/problems/subsets)
- **O(N*2^N)**, **O(N*2^N)** N nums of elements, 2^N subsets.
- start with empty set, add each element to set, then add to result.
- [[], [1], [5], [1,5], [3], [1,3], [5,3], [1,5,3]]
3 changes: 2 additions & 1 deletion missing.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- revise two pointer interview school
- revise two pointer interview school
- return next to recursion DP, Backtracking

0 comments on commit 1de7fad

Please sign in to comment.