forked from bindubritto/tjip-leetcode-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Submission Week Ten #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nazmulwanted
wants to merge
7
commits into
master
Choose a base branch
from
week_ten
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6dddf1a
Problem 3: Subsets. Done.
nazmulwanted 7eccbd6
Problem 1: Verify preorder sequence in binary search tree. Done.
nazmulwanted 7674b84
Problem 4: Word search. Done.
nazmulwanted d760200
Problem 5: Generate parenthesis. Done.
nazmulwanted fdf8e6a
Problem 5: Generate parentheses updated. Done.
nazmulwanted e45f8b8
Updated the file name of problem 5.
nazmulwanted 50bfb0a
Problem 6: Letter combination of a phone number. Done.
nazmulwanted File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Time complexity: O(2 ^ (2 * n)) | ||
// Space complexity: O(2 ^ (2 * n)) | ||
|
||
void generateBrackets(string currStr, int open, int close, vector<string> &bracket_list){ | ||
if(open == close && open == 0){ | ||
bracket_list.push_back(currStr); | ||
return; | ||
} | ||
if(open > 0) generateBrackets(currStr + "(", open - 1, close, bracket_list); | ||
if(open < close) generateBrackets(currStr + ")", open, close - 1, bracket_list); | ||
} | ||
|
||
vector<string> generateParentheses(int n){ | ||
vector<string> bracket_list; | ||
generateBrackets("", n, n, bracket_list); | ||
return bracket_list; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Time complexity: O(N * 4 ^ N), N = Length of digits. | ||
// Space complexity: O(N * 4 ^ N), N = Length of digits. | ||
|
||
void genCombination(string currStr, int pos, string &digits, vector<string> &digitLetterList, vector<string> &combinationList){ | ||
if(pos == digits.size()){ | ||
combinationList.push_back(currStr); | ||
return; | ||
} | ||
int digit = digits[pos] - '0'; | ||
for(auto letter: digitLetterList){ | ||
genCombination(currStr + letter, pos + 1, digits, digitLetterList, combinationList); | ||
} | ||
} | ||
|
||
vector<string> letterCombinations(string digits){ | ||
if(digits.size() == 0) return {}; | ||
vector<string> combinationList; | ||
vector<string> digitLetterList = { | ||
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" | ||
}; | ||
string currStr; | ||
genCombination(currStr, 0, digits, digitLetterList, combinationList); | ||
return combinationList; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Time complexity: O(2^N) , N = Number of elements in the array. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Space complexity: O(2^N), N = Number of elements in the array. | ||
|
||
void gen_subsets(int curr, vector<int> subset, vector<vector<int>> &subset_list, vector<int> &nums){ | ||
if(curr == nums.size()){ | ||
subset_list.push_back(subset); | ||
return; | ||
} | ||
subset.push_back(nums[curr]); | ||
gen_subsets(curr + 1, subset, subset_list, nums); | ||
subset.pop_back(); | ||
gen_subsets(curr + 1, subset, subset_list, nums); | ||
} | ||
|
||
vector<vector<int>> subsets(vector<int> &nums){ | ||
vector<vector<int>> subset_list; | ||
gen_subsets(0, {}, subset_list, nums); | ||
return subset_list; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Time complexity: O(N), N = Number of elements in an array. | ||
// Space complexity: O(N) | ||
// | ||
|
||
void verify_bst(vector<int> &preorder, int &curr_index, int left, int right){ | ||
if(curr_index >= preorder.size()) return; | ||
int value = preorder[curr_index]; | ||
if(value < left || value > right) return; | ||
curr_index++; | ||
verify_bst(preorder, curr_index, left, value - 1); | ||
verify_bst(preorder, curr_index, value + 1, right); | ||
} | ||
|
||
bool verify_preorder(vector<int> &preorder){ | ||
int curr_index = 0; | ||
verify_bst(preorder, curr_index, INT_MIN, INT_MAX); | ||
return curr_index == preorder.size(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Time complexity: O(M * N * 3 ^ L), M = Width of the board, N = Height of the board, L = Word length. | ||
// Space complexity: O(L), L = Word length. | ||
|
||
int dx[4] = {0, 0, 1, -1}; | ||
int dy[4] = {-1, 1, 0, 0}; | ||
|
||
bool traverseBoard(vector<vector<char>> &board, string &word, int &m, int &n, int pos, int x, int y){ | ||
if(x < 0 || x >= m || y < 0 || y >= n || word[pos] != board[x][y]) return false; | ||
if(pos == word.size() - 1) return true; | ||
bool status = false; | ||
char ch = board[x][y]; | ||
board[x][y] = '#'; | ||
for(int i = 0; i < 4; i++){ | ||
status |= traverseBoard(board, word, m, n, pos+1, x + dx[i], y + dy[i]); | ||
} | ||
board[x][y] = ch; | ||
return status; | ||
} | ||
|
||
bool exist(vector<vector<char>> &board, string word){ | ||
if(word.size() == 0) return true; | ||
if(board.size() == 0 || board[0].size() == 0) return false; | ||
int m = board.size(); | ||
int n = board[0].size(); | ||
for(int i = 0; i < m; i++){ | ||
for(int j = 0; j < n; j++){ | ||
if(traverseBoard(board, word, m, n, 0, i, j)) return true; | ||
} | ||
} | ||
return false; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are wrong. The time and space complexity is O(2nCn / (n + 1)).