Skip to content

Commit fb6346f

Browse files
committed
LC Update
1 parent 861b5fe commit fb6346f

7 files changed

+218
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int maxDepth(TreeNode* root) {
15+
if(root == nullptr) return 0;
16+
if(root -> left == nullptr && root -> right == nullptr) return 1;
17+
int left = numeric_limits<int>::min(), right = numeric_limits<int>::min();
18+
if(root -> left != nullptr) left = maxDepth(root -> left);
19+
if(root -> right != nullptr) right = maxDepth(root -> right);
20+
return 1 + max(left, right);
21+
}
22+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string removeDuplicates(string S) {
4+
stack <char> st;
5+
for(int i = 0 ; i < S.length() ; ++i){
6+
if(!st.empty() && S[i] == st.top()){
7+
while(!st.empty() && st.top() == S[i]) st.pop();
8+
} else{
9+
st.push(S[i]);
10+
}
11+
}
12+
string modified(st.size(), '0');
13+
int i = modified.length() - 1;
14+
while(!st.empty()){
15+
modified[i--] = st.top();
16+
st.pop();
17+
}
18+
return modified;
19+
}
20+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int minDepth(TreeNode* root) {
15+
if(root == nullptr) return 0;
16+
if(root -> left == nullptr && root -> right == nullptr) return 1;
17+
int left = numeric_limits<int>::max(), right = numeric_limits<int>::max();
18+
if(root -> left != nullptr) left = minDepth(root -> left);
19+
if(root -> right != nullptr) right = minDepth(root -> right);
20+
return 1 + min(left, right);
21+
}
22+
};
23+
24+
/*****************************************************************************************/
25+
26+
class Solution {
27+
public:
28+
void min_depth_util(TreeNode *root, int &min_depth, int depth = 1){
29+
if(root -> left == nullptr && root -> right == nullptr){
30+
min_depth = min(min_depth, depth);
31+
return;
32+
}
33+
if(depth >= min_depth) return;
34+
if(root -> left != nullptr) min_depth_util(root -> left, min_depth, depth + 1);
35+
if(root -> right != nullptr) min_depth_util(root -> right, min_depth, depth + 1);
36+
}
37+
int minDepth(TreeNode* root) {
38+
if(root == nullptr) return 0;
39+
int min_depth = numeric_limits<int>::max();
40+
min_depth_util(root, min_depth);
41+
return min_depth;
42+
}
43+
};
44+
45+
/*****************************************************************************************/
46+
47+
/**
48+
* Definition for a binary tree node.
49+
* struct TreeNode {
50+
* int val;
51+
* TreeNode *left;
52+
* TreeNode *right;
53+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
54+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
55+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
56+
* };
57+
*/
58+
class Solution {
59+
public:
60+
int minDepth(TreeNode* root) {
61+
if(root == nullptr) return 0;
62+
queue <TreeNode*> q;
63+
map <TreeNode*, int> depths;
64+
q.push(root);
65+
depths[root] = 1;
66+
int depth = -1;
67+
while(!q.empty()){
68+
TreeNode* current = q.front();
69+
q.pop();
70+
if(current -> left == nullptr && current -> right == nullptr){
71+
depth = depths[current];
72+
break;
73+
}
74+
if(current -> left != nullptr){
75+
q.push(current -> left);
76+
depths[current -> left] = depths[current] + 1;
77+
}
78+
if(current -> right != nullptr){
79+
q.push(current -> right);
80+
depths[current -> right] = depths[current] + 1;
81+
}
82+
}
83+
return depth;
84+
}
85+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<string> buildArray(vector<int>& target, int n) {
4+
int index = 0;
5+
vector <string> operations; operations.reserve(n);
6+
for(int i = 1 ; index < target.size() ; ++i){
7+
if(i == target[index]){
8+
operations.emplace_back("Push");
9+
index++;
10+
} else{
11+
operations.emplace_back("Push");
12+
operations.emplace_back("Pop");
13+
}
14+
}
15+
return operations;
16+
}
17+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
3+
select FirstName, LastName, City, State
4+
from Person left join Address on Person.PersonId = Address.PersonId;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
3+
4+
select MAX(salary) as SecondHighestSalary
5+
from Employee
6+
where salary < (select MAX(salary) from Employee);
7+
8+
-----------------------------------------------------------------------
9+
10+
# Write your MySQL query statement below
11+
12+
select MAX(e1.salary) as SecondHighestSalary
13+
from Employee e1 join Employee e2
14+
where e1.salary < e2.salary;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
// O(N) Time, O(N) Space
3+
class Solution {
4+
public:
5+
bool backspaceCompare(string S, string T) {
6+
stack <char> s1;
7+
for(int i = 0 ; i < S.length() ; ++i){
8+
if(S[i] == '#' && !s1.empty()){
9+
s1.pop();
10+
} else if(S[i] != '#') {
11+
s1.push(S[i]);
12+
}
13+
}
14+
stack <char> s2;
15+
for(int i = 0 ; i < T.length() ; ++i){
16+
if(T[i] == '#' && !s2.empty()){
17+
s2.pop();
18+
} else if(T[i] != '#'){
19+
s2.push(T[i]);
20+
}
21+
}
22+
return s1 == s2;
23+
}
24+
};
25+
26+
/*********************************************************************************************/
27+
28+
class Solution {
29+
public:
30+
bool backspaceCompare(string S, string T) {
31+
int sl = S.length();
32+
for(int i = 0 ; i < sl ; ++i){
33+
if(S[i] != '#'){
34+
S.push_back(S[i]);
35+
} else if(S.length() > sl) {
36+
S.pop_back();
37+
}
38+
}
39+
int tl = T.length();
40+
for(int i = 0 ; i < tl ; ++i){
41+
if(T[i] != '#'){
42+
T.push_back(T[i]);
43+
} else if(T.length() > tl) {
44+
T.pop_back();
45+
}
46+
}
47+
if(S.length() - sl != T.length() - tl) return false;
48+
for(int i = sl, j = tl ; i < S.length() ; ++i, ++j){
49+
if(S[i] != T[j]) return false;
50+
}
51+
return true;
52+
}
53+
};
54+
55+
// O(N) Time, O(1) Space
56+

0 commit comments

Comments
 (0)