Skip to content

Commit 272703f

Browse files
committed
🚀 07-Sep-2020
1 parent 3d29948 commit 272703f

10 files changed

+984
-0
lines changed

competitive programming/leetcode/116. Populating Next Right Pointers in Each Node.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,45 @@ class Solution {
7979
return root;
8080
}
8181
};
82+
83+
84+
// Recursive
85+
86+
class Solution {
87+
public:
88+
Node* connect(Node* root) {
89+
if(!root) {
90+
return NULL;
91+
}
92+
if(root -> left) {
93+
root -> left -> next = root -> right;
94+
if (root -> next) {
95+
root -> right -> next = root -> next -> left;
96+
}
97+
connect(root -> left);
98+
connect(root -> right);
99+
}
100+
return root;
101+
}
102+
};
103+
104+
// Iterative O(1) space
105+
106+
class Solution {
107+
public:
108+
Node* connect(Node* root) {
109+
Node *pre = root, *cur;
110+
while (pre) {
111+
cur = pre;
112+
while (cur && cur -> left) {
113+
cur -> left -> next = cur -> right;
114+
if (cur -> next) {
115+
cur -> right -> next = cur -> next -> left;
116+
}
117+
cur = cur -> next;
118+
}
119+
pre = pre -> left;
120+
}
121+
return root;
122+
}
123+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
2+
determine if s can be segmented into a space-separated sequence of one or more dictionary words.
3+
4+
Note:
5+
6+
The same word in the dictionary may be reused multiple times in the segmentation.
7+
You may assume the dictionary does not contain duplicate words.
8+
Example 1:
9+
10+
Input: s = "leetcode", wordDict = ["leet", "code"]
11+
Output: true
12+
Explanation: Return true because "leetcode" can be segmented as "leet code".
13+
Example 2:
14+
15+
Input: s = "applepenapple", wordDict = ["apple", "pen"]
16+
Output: true
17+
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
18+
Note that you are allowed to reuse a dictionary word.
19+
Example 3:
20+
21+
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
22+
Output: false
23+
24+
25+
26+
// Trie (TLE)
27+
28+
class Solution {
29+
public:
30+
31+
struct TrieNode {
32+
bool ew; // end of word
33+
struct TrieNode* child[26];
34+
};
35+
36+
TrieNode* createNode(){
37+
TrieNode* newNode= new TrieNode;
38+
newNode->ew=false;
39+
for(int i=0;i<26;i++)
40+
newNode->child[i]=NULL;
41+
return newNode;
42+
}
43+
44+
45+
void insert(string word, TrieNode* root){
46+
TrieNode* curr=root;
47+
int wordLen=word.length();
48+
for(int i=0;i<wordLen;i++){
49+
int index=word[i]-'a';
50+
if(curr->child[index]==NULL)
51+
curr->child[index]=createNode();
52+
curr=curr->child[index];
53+
}
54+
curr->ew=true;
55+
}
56+
57+
bool search(string word, TrieNode* root){
58+
TrieNode* curr=root;
59+
int wordLen=word.length();
60+
for(int i=0;i<wordLen;i++){
61+
int index=word[i]-'a';
62+
if(curr->child[index]==NULL) return false;
63+
curr=curr->child[index];
64+
}
65+
return (curr!=NULL && curr->ew);
66+
67+
}
68+
69+
bool wordBreakUtil(string s, TrieNode* root){
70+
int size=s.length();
71+
if(size==0) return true;
72+
73+
for(int i=1;i<=size;i++){
74+
if( search(s.substr(0, i), root) && wordBreakUtil(s.substr(i, size-i), root ))
75+
return true;
76+
}
77+
return false;
78+
}
79+
80+
bool wordBreak(string s, vector<string>& wordDict) {
81+
int n=wordDict.size();
82+
TrieNode* root= createNode();
83+
for(int i=0;i<n;i++){
84+
insert(wordDict[i], root);
85+
}
86+
87+
return wordBreakUtil(s, root);
88+
}
89+
};
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
// Recursive (TLE)
106+
107+
class Solution {
108+
public:
109+
110+
bool searchDict(string word, vector<string> &wordDict){
111+
for(auto &x: wordDict)
112+
if(word==x)
113+
return true;
114+
return false;
115+
}
116+
117+
bool wordBreakUtil(string s, vector<string> &wordDict){
118+
int size=s.length();
119+
if(size==0) return true;
120+
for(int i=1;i<=size;i++){
121+
if(searchDict(s.substr(0, i), wordDict) && wordBreakUtil(s.substr(i, size-i), wordDict) )
122+
return true;
123+
}
124+
return false;
125+
}
126+
127+
bool wordBreak(string s, vector<string>& wordDict) {
128+
return wordBreakUtil(s, wordDict);
129+
}
130+
};
131+
132+
133+
134+
135+
136+
137+
// Recursive (memoized)
138+
139+
class Solution {
140+
public:
141+
142+
unordered_map<string, bool> memo;
143+
144+
bool searchDict(string word, vector<string> &wordDict){
145+
for(auto &x: wordDict)
146+
if(word==x)
147+
return true;
148+
return false;
149+
}
150+
151+
bool wordBreakUtil(string s, vector<string> &wordDict){
152+
int size=s.length();
153+
if(size==0) return true;
154+
if(memo.find(s)!=memo.end()) return memo[s];
155+
for(int i=1;i<=size;i++){
156+
if(searchDict(s.substr(0, i), wordDict) && wordBreakUtil(s.substr(i, size-i), wordDict) )
157+
return memo[s]=true;
158+
}
159+
return memo[s]=false;
160+
}
161+
162+
bool wordBreak(string s, vector<string>& wordDict) {
163+
return wordBreakUtil(s, wordDict);
164+
}
165+
};
166+
167+
168+
169+
170+
// Dynamic Programming
171+
172+
// dp[i] denotes that the s.substr(0,i) is available in our dictionary
173+
174+
class Solution {
175+
public:
176+
bool wordBreak(string s, vector<string>& wordDict) {
177+
set<string>dictSet;
178+
for(auto &x: wordDict)
179+
dictSet.insert(x);
180+
181+
int n=s.length();
182+
183+
vector<bool> dp(n+1, false);
184+
185+
dp[0]=true;
186+
187+
for(int len=1;len<=n;len++){
188+
for(int i=0;i<len;i++){
189+
if(dp[i] && dictSet.find(s.substr(i, len-i))!=dictSet.end()){
190+
dp[len]=true;
191+
break;
192+
}
193+
}
194+
}
195+
196+
return dp[n];
197+
}
198+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Implement an iterator over a binary search tree (BST).
2+
Your iterator will be initialized with the root node of a BST.
3+
4+
Calling next() will return the next smallest number in the BST.
5+
6+
7+
8+
Example:
9+
10+
11+
12+
BSTIterator iterator = new BSTIterator(root);
13+
iterator.next(); // return 3
14+
iterator.next(); // return 7
15+
iterator.hasNext(); // return true
16+
iterator.next(); // return 9
17+
iterator.hasNext(); // return true
18+
iterator.next(); // return 15
19+
iterator.hasNext(); // return true
20+
iterator.next(); // return 20
21+
iterator.hasNext(); // return false
22+
23+
24+
Note:
25+
26+
next() and hasNext() should run in average O(1) time and uses O(h) memory,
27+
where h is the height of the tree.
28+
You may assume that next() call will always be valid, that is,
29+
there will be at least a next smallest number in the BST when next() is called.
30+
31+
32+
33+
34+
35+
36+
37+
38+
/**
39+
* Definition for a binary tree node.
40+
* struct TreeNode {
41+
* int val;
42+
* TreeNode *left;
43+
* TreeNode *right;
44+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
45+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
46+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
47+
* };
48+
*/
49+
class BSTIterator {
50+
public:
51+
52+
stack<TreeNode*> s;
53+
54+
void leftmostInorder(TreeNode* root){
55+
while(root){
56+
s.push(root);
57+
root=root->left;
58+
}
59+
}
60+
61+
BSTIterator(TreeNode* root) {
62+
leftmostInorder(root);
63+
}
64+
65+
/** @return the next smallest number */
66+
int next() {
67+
TreeNode *topmostNode=s.top();
68+
s.pop();
69+
if(topmostNode->right!=NULL) leftmostInorder(topmostNode->right);
70+
71+
return topmostNode->val;
72+
}
73+
74+
/** @return whether we have a next smallest number */
75+
bool hasNext() {
76+
return s.size() > 0;
77+
}
78+
};
79+
80+
/**
81+
* Your BSTIterator object will be instantiated and called as such:
82+
* BSTIterator* obj = new BSTIterator(root);
83+
* int param_1 = obj->next();
84+
* bool param_2 = obj->hasNext();
85+
*/
86+
87+
88+
89+
// Approach 1: Flatten the list
90+
// Do inorder traversal and store the result in an array
91+
// The array will be sorted
92+
// Time complexity : O(N)
93+
// Space complexity : O(N)
94+
95+
// Approach 2: Controlled Recursion
96+
// Implemented above
97+
// // Time complexity : O(N)
98+
//Space complexity: The space complexity is O(h)

0 commit comments

Comments
 (0)