Skip to content

Commit c8887a2

Browse files
committed
copy cpp files to fit directory structure
1 parent 39fe33d commit c8887a2

File tree

152 files changed

+6911
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+6911
-0
lines changed

cpp/1-Two-Sum.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Given int array & target, return indices of 2 nums that add to target
3+
Ex. nums = [2,7,11,15] & target = 9 -> [0,1], 2 + 7 = 9
4+
5+
At each num, calculate complement, if exists in hash map then return
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
class Solution {
12+
public:
13+
vector<int> twoSum(vector<int>& nums, int target) {
14+
unordered_map<int, int> m;
15+
vector<int> result;
16+
17+
for (int i = 0; i < nums.size(); i++) {
18+
int complement = target - nums[i];
19+
if (m.find(complement) != m.end()) {
20+
result.push_back(m[complement]);
21+
result.push_back(i);
22+
break;
23+
} else {
24+
m.insert({nums[i], i});
25+
}
26+
}
27+
28+
return result;
29+
}
30+
};
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Given string & pattern, implement RegEx matching
3+
'.' -> matches any single character
4+
'*' -> matches zero or more of the preceding element
5+
Matching should cover the entire input string (not partial)
6+
Ex. s = "aa", p = "a" -> false, "a" doesn't match entire string "aa"
7+
8+
DFS + memo, 2 choices at a *: either use it, or don't use it
9+
10+
Time: O(m x n)
11+
Space: O(m x n)
12+
*/
13+
14+
class Solution {
15+
public:
16+
bool isMatch(string s, string p) {
17+
return dfs(s, p, 0, 0);
18+
}
19+
private:
20+
map<pair<int, int>, bool> dp;
21+
22+
bool dfs(string& s, string& p, int i, int j) {
23+
if (dp.find({i, j}) != dp.end()) {
24+
return dp[{i, j}];
25+
}
26+
27+
if (i >= s.size() && j >= p.size()) {
28+
return true;
29+
}
30+
if (j >= p.size()) {
31+
return false;
32+
}
33+
34+
bool match = i < s.size() && (s[i] == p[j] || p[j] == '.');
35+
if (j + 1 < p.size() && p[j + 1] == '*') {
36+
// choices: either (1) don't use *, or (2) use *
37+
dp[{i, j}] = dfs(s, p, i, j + 2) || (match && dfs(s, p, i + 1, j));
38+
return dp[{i, j}];
39+
}
40+
41+
if (match) {
42+
dp[{i, j}] = dfs(s, p, i + 1, j + 1);
43+
return dp[{i, j}];
44+
}
45+
46+
dp[{i, j}] = false;
47+
return dp[{i, j}];
48+
}
49+
};

cpp/100-Same-Tree.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Given roots of 2 binary trees, check if they're the same or not (same structure & values)
3+
Ex. p = [1,2,3] q = [1,2,3] -> true, p = [1,2] q = [1,null,2] -> false
4+
5+
Check: (1) matching nulls, (2) non-matching nulls, (3) non-matching values
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
bool isSameTree(TreeNode* p, TreeNode* q) {
25+
if (p == NULL && q == NULL) {
26+
return true;
27+
}
28+
if (p == NULL || q == NULL) {
29+
return false;
30+
}
31+
if (p->val != q->val) {
32+
return false;
33+
}
34+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
35+
}
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Given root of binary tree, return level order traversal of its nodes (left to right)
3+
Ex. root = [3,9,20,null,null,15,7] -> [[3],[9,20],[15,7]]
4+
5+
Standard BFS traversal, at each level, push left & right nodes if they exist to queue
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
vector<vector<int>> levelOrder(TreeNode* root) {
25+
vector<vector<int>> result;
26+
27+
if (root == NULL) {
28+
return result;
29+
}
30+
31+
queue<TreeNode*> q;
32+
q.push(root);
33+
34+
while (!q.empty()) {
35+
int count = q.size();
36+
vector<int> curr;
37+
38+
for (int i = 0; i < count; i++) {
39+
TreeNode* node = q.front();
40+
q.pop();
41+
42+
curr.push_back(node->val);
43+
44+
if (node->left != NULL) {
45+
q.push(node->left);
46+
}
47+
if (node->right != NULL) {
48+
q.push(node->right);
49+
}
50+
}
51+
52+
result.push_back(curr);
53+
}
54+
55+
return result;
56+
}
57+
};
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Given root of binary tree, return max depth (# nodes along longest path from root to leaf)
3+
4+
At every node, max depth is the max depth between its left & right children + 1
5+
6+
Time: O(n)
7+
Space: O(n)
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* struct TreeNode {
13+
* int val;
14+
* TreeNode *left;
15+
* TreeNode *right;
16+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
int maxDepth(TreeNode* root) {
24+
if (root == NULL) {
25+
return 0;
26+
}
27+
return 1 + max(maxDepth(root->left), maxDepth(root->right));
28+
}
29+
};
30+
31+
// class Solution {
32+
// public:
33+
// int maxDepth(TreeNode* root) {
34+
// if (root == NULL) {
35+
// return 0;
36+
// }
37+
// queue<TreeNode*> q;
38+
// q.push(root);
39+
// int result = 0;
40+
// while (!q.empty()) {
41+
// int count = q.size();
42+
// for (int i = 0; i < count; i++) {
43+
// TreeNode* node = q.front();
44+
// q.pop();
45+
// if (node->left != NULL) {
46+
// q.push(node->left);
47+
// }
48+
// if (node->right != NULL) {
49+
// q.push(node->right);
50+
// }
51+
// }
52+
// result++;
53+
// }
54+
// return result;
55+
// }
56+
// };

cpp/1046-Last-Stone-Weight.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Given array of stones to smash, return smallest possible weight of last stone
3+
If x == y both stones destroyed, if x != y stone x destroyed, stone y = y - x
4+
Ex. stones = [2,7,4,1,8,1] -> 1, [2,4,1,1,1], [2,1,1,1], [1,1,1], [1]
5+
6+
Max heap, pop 2 biggest, push back difference until no more 2 elements left
7+
8+
Time: O(n log n)
9+
Space: O(n)
10+
*/
11+
12+
class Solution {
13+
public:
14+
int lastStoneWeight(vector<int>& stones) {
15+
priority_queue<int> pq;
16+
for (int i = 0; i < stones.size(); i++) {
17+
pq.push(stones[i]);
18+
}
19+
20+
while (pq.size() > 1) {
21+
int y = pq.top();
22+
pq.pop();
23+
int x = pq.top();
24+
pq.pop();
25+
if (y > x) {
26+
pq.push(y - x);
27+
}
28+
}
29+
30+
if (pq.empty()) {
31+
return 0;
32+
}
33+
return pq.top();
34+
}
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Given 2 integer arrays preorder & inorder, construct & return the binary tree
3+
Ex. preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] -> [3,9,20,null,null,15,7]
4+
5+
Preorder dictates nodes, inorder dictates subtrees (preorder values, inorder positions)
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
25+
int index = 0;
26+
return build(preorder, inorder, index, 0, inorder.size() - 1);
27+
}
28+
private:
29+
TreeNode* build(vector<int>& preorder, vector<int>& inorder, int& index, int i, int j) {
30+
if (i > j) {
31+
return NULL;
32+
}
33+
34+
TreeNode* root = new TreeNode(preorder[index]);
35+
36+
int split = 0;
37+
for (int i = 0; i < inorder.size(); i++) {
38+
if (preorder[index] == inorder[i]) {
39+
split = i;
40+
break;
41+
}
42+
}
43+
index++;
44+
45+
root->left = build(preorder, inorder, index, i, split - 1);
46+
root->right = build(preorder, inorder, index, split + 1, j);
47+
48+
return root;
49+
}
50+
};

cpp/11-Container-With-Most-Water.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Given array of heights, find max water container can store
3+
Ex. height = [1,8,6,2,5,4,8,3,7] -> 49, (8 - 1) x min(8, 7)
4+
5+
2 pointers outside in, greedily iterate pointer w/ lower height
6+
7+
Time: O(n)
8+
Space: O(1)
9+
*/
10+
11+
class Solution {
12+
public:
13+
int maxArea(vector<int>& height) {
14+
int i = 0;
15+
int j = height.size() - 1;
16+
17+
int curr = 0;
18+
int result = 0;
19+
20+
while (i < j) {
21+
curr = (j - i) * min(height[i], height[j]);
22+
result = max(result, curr);
23+
24+
if (height[i] <= height[j]) {
25+
i++;
26+
} else {
27+
j--;
28+
}
29+
}
30+
31+
return result;
32+
}
33+
};

0 commit comments

Comments
 (0)