Skip to content

Commit

Permalink
Day09 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
muditmahajan21 committed May 22, 2022
1 parent 454efc0 commit c7dd4e9
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Day09/Combination Sum II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
void solve(vector<vector<int>> &ans, vector<int> &res, vector<int> &nums, int target, int idx) {
if(!target) {
ans.push_back(res);
return;
}
for(int i = idx; i < nums.size(); i++) {
if(nums[i] > target) {
break;
}
if(i != idx and nums[i] == nums[i - 1]) {
continue;
}
res.push_back(nums[i]);
solve(ans, res, nums, target - nums[i], i + 1);
res.pop_back();
}
}

vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> res;
sort(candidates.begin(), candidates.end());
solve(ans, res, candidates, target, 0);
return ans;
}
23 changes: 23 additions & 0 deletions Day09/Combination Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
void solve(vector<vector<int>> &ans, vector<int> &res, vector<int> &arr, int target, int idx) {
if(!target) {
ans.push_back(res);
return;
}

if(idx == arr.size() || arr[idx] > target) {
return;
}

res.push_back(arr[idx]);
solve(ans, res, arr, target - arr[idx], idx);
res.pop_back();
solve(ans, res, arr, target, idx + 1);
}

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> res;
sort(candidates.begin(), candidates.end());
solve(ans, res, candidates, target, 0);
return ans;
}
28 changes: 28 additions & 0 deletions Day09/Palindrome Partitioning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
bool check_palin(string s, int i, int j) {
while(i <= j) {
if(s[i++] != s[j--]) return false;
}
return true;
}

void solve(vector<vector<string>> &ans, vector<string> &res, string &s, int idx) {
if(idx == s.length()) {
ans.push_back(res);
return;
}

for(int i = idx; i < s.length(); i++) {
if(check_palin(s, idx, i)) {
res.push_back(s.substr(idx, i - idx + 1));
solve(ans, res, s, i + 1);
res.pop_back();
}
}
}

vector<vector<string>> partition(string s) {
vector<vector<string>> ans;
vector<string> res;
solve(ans, res, s, 0);
return ans;
}
21 changes: 21 additions & 0 deletions Day09/Permutation Sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
string getPermutation(int n, int k) {
string ans = "";
string num = "";
vector<int> fact(n + 1);
fact[0] = 1;

for(int i = 0; i < n; i++) {
fact[i + 1] = (i + 1) * fact[i];
num += to_string(i + 1);
}

k--;

for(int i = n - 1 ; i >= 0; i--) {
int ind = k / fact[i];
k %= fact[i];
ans += num[ind];
num.erase(num.begin() + ind);
}
return ans;
}
101 changes: 101 additions & 0 deletions Day09/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Day9

## Subset Sums

Link: [https://practice.geeksforgeeks.org/problems/subset-sums]

- Create an answer vector and pass it to a helper function along with the given array, an index starting from 0 and a sum variable starting from 0 as well.
- In the helper function, if the current index is equal to the length of the givena array, then push the sum into the answer vector and return.
- Else, recurse once, adding the current index to the sum variable and once without adding it.
```
solve(idx + 1, sum + arr[idx], arr, ans);
solve(idx + 1, sum, arr, ans);
```

Time Complexity: O(2^n)

## Subsets II

Link: [https://leetcode.com/problems/subsets-ii/]

- Create an answer vector and a temp res vector and pass them on to a helper function with an index starting from zero as well.
- Push the temp res vector in the answer vector.
- Run a loop from the index to the length of the nums array.
- Check for duplicates by checking the consecutive elements.
- Add the current element to the temp res vector.
- Recurse inside of the loop by putting the (i + 1) as the index variable.
- Pop back the element pushed in the temp res vector for the next iteration.

Time Complexity: O(2^n)

## Combination Sum

Link: [https://leetcode.com/problems/combination-sum/]

- Create an answer vector along with a temp res vector and pass them on to a helper recursive function.
- Sort the given array before passing to the function.
- In the helper function, if target is zero, add the temp res vector to the answer vector and return.
- If the current index is equal to the size of the array or tbe current element is learger than the target, then return.
- Push tbe current element in the temp res array.
- Call the solve function with the initial values except subtract the current index element from the target.
- Pop the element pushed in the temp res vector.
- Again, call the solve function with the initial values, except increment the index variable.
- Return answer vector in the original function.

Time Complexity: O(2^target)

## Combination Sum II

Link: [https://leetcode.com/problems/combination-sum-ii/]

- Create an answer vector along with a temp res vector, sort the given array and pass them all to a helper recursive function.
- In the hlper function, if the target is zero, push the res vector into the answer vector and return.
- Else, run a loop from the index passed to the function to the length of the array.
- If the current element is greater than the target, then break out of the loop.
- Check for the duplicates with checking the neighbouring elements.
- Push the current element in the temp res vector.
- Call the solve function with the same values except, decrement the target with the current element and increment the i by 1 to take care of only having one element in a subset.
- Pop the last pushed value from the temp res array.
- Return th answer vector in the original array.

Time Complexity: O(2^n)

## Palindrome Partitioning

Link: [https://leetcode.com/problems/palindrome-partitioning/]

- Create an answer vector along with a temp res vector, pass them on to the helper function along with an index variable set to zero.
+ If the index is equal to length of the string, add it to the answer vector and return.
- Run a for loop from the index to the length of the string.
- Write a separate function to check if a substring is a plaindrome or not.
- If the substring from index to i is palindrome, then:
- Extract the substring using the substr function.
- Push it into the temp res vector call the solve function with index + 1.
- Pop the substring added to the temp res vector.
- Return the answer vector in the original function.

Time Complexity: O((2^n)* k *(n/2))

## Permutation Sequence

Link: [https://leetcode.com/problems/permutation-sequence/]

- Initialize two empty string, answer and number.
- Create a factorial vector.
- Run a loop from 0 to n, and add each (i + 1)th number to the numer string.
- Fill the factorial vector accordingly in the loop.
- Outside the loop, decremtent the k to take care of 0-indexing.
- Run a loop from the back of the string.
- Find the required index with the formula
```
idx = k / fact[i]
```
- Update the value of k by:
```
k %= fact[i]
```
- Add the character at the idx to the answer string.
- Erase the added character with the erase function.
- Return the answer string.

Time Complexity: O(n^2)
17 changes: 17 additions & 0 deletions Day09/Subset Sums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
void solve(int idx, int sum, vector<int> &ans, vector<int> &arr) {
if(idx == arr.size()) {
ans.push_back(sum);
return;
}

solve(idx + 1, sum + arr[idx], ans, arr);
solve(idx + 1, sum, ans, arr);
}

public:
vector<int> subsetSums(vector<int> arr, int N)
{
vector<int> ans;
solve(0, 0, ans, arr);
return ans;
}
18 changes: 18 additions & 0 deletions Day09/Subsets II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
void solve(int idx, vector<int> &nums, vector<vector<int>> &ans, vector<int> &res) {
ans.push_back(res);

for(int i = idx; i < nums.size(); i++) {
if(i != idx and nums[i] == nums[i - 1]) continue;
res.push_back(nums[i]);
solve(i + 1, nums, ans, res);
res.pop_back();
}
}

vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> res;
sort(nums.begin(), nums.end());
solve(0, nums, ans, res);
return ans;
}

0 comments on commit c7dd4e9

Please sign in to comment.