-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a4b28c
commit f043afc
Showing
30 changed files
with
1,058 additions
and
4 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
LeetCode/Algorithms/Easy/DesignAddAndSearchWordsDataStructure.cpp
This file contains 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,42 @@ | ||
class WordDictionary { | ||
vector<WordDictionary*> children; | ||
bool isEndOfWord; | ||
public: | ||
WordDictionary(): isEndOfWord(false){ | ||
children = vector<WordDictionary*>(26, nullptr); | ||
} | ||
|
||
void addWord(string word) { | ||
WordDictionary *curr = this; | ||
for(char c: word) { | ||
if(curr->children[c - 'a'] == NULL) { | ||
curr->children[c - 'a'] = new WordDictionary(); | ||
} | ||
curr = curr->children[c - 'a']; | ||
} | ||
curr->isEndOfWord = true; | ||
} | ||
|
||
bool search(string word) { | ||
WordDictionary *curr = this; | ||
for(int i=0; i<word.size(); i++) { | ||
char c = word[i]; | ||
if(c == '.') { | ||
for(auto ch: curr->children) { | ||
if(ch && ch->search(word.substr(i + 1))) return true; | ||
} | ||
return false; | ||
} | ||
if(curr->children[c - 'a'] == NULL) return false; | ||
curr = curr->children[c - 'a']; | ||
} | ||
return curr && curr->isEndOfWord; | ||
} | ||
}; | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* WordDictionary* obj = new WordDictionary(); | ||
* obj->addWord(word); | ||
* bool param_2 = obj->search(word); | ||
*/ |
30 changes: 30 additions & 0 deletions
30
LeetCode/Algorithms/Easy/DistributeMoneyToMaximumChildren.cpp
This file contains 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,30 @@ | ||
class Solution { | ||
public: | ||
int distMoney(int money, int children) { | ||
money = money - children; | ||
|
||
if(money < 0) { | ||
return -1; | ||
} | ||
|
||
if(money / 7 == children && money % 7 == 0) { | ||
return children | ||
} | ||
|
||
if(money / 7 == children - 1 && money % 7 == 3) { | ||
return children - 2; | ||
} | ||
|
||
return min(children - 1, money / 7); | ||
} | ||
}; | ||
|
||
// money = 20, children = 3 | ||
// Output: 1 | ||
|
||
// money = 16, children = 2 | ||
// Output: 2 | ||
|
||
// 20 - 3 = 17 | ||
// 17 / 7 = 2... | ||
// 17 % 7 == 3 |
43 changes: 43 additions & 0 deletions
43
LeetCode/Algorithms/Hard/FindTheLongestValidObstacleCourseAtEachPosition.cpp
This file contains 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,43 @@ | ||
class Solution { | ||
public: | ||
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) { | ||
vector<int> res, mono; | ||
|
||
for(int a: obstacles) { | ||
int left = 0, right = mono.size(); | ||
|
||
while(left < right) { | ||
int m = (left + right) / 2; | ||
|
||
if(mono[m] <= a) { | ||
left = m + 1; | ||
} else { | ||
right = m; | ||
} | ||
} | ||
|
||
res.push_back(left + 1); | ||
if(mono.size() == left) { | ||
mono.push_back(a); | ||
} | ||
mono[left] = a; | ||
} | ||
return res; | ||
} | ||
}; | ||
|
||
// Input: obstacles = [1,2,3,2] | ||
|
||
// left | 0 | 0 -> 1 | | ||
// right | 0 | 1 | | ||
// mono[left] | 1 | 2 | | ||
// a | 1 | 2 | 3 | ||
// res | 1 | 2 | | ||
|
||
|
||
// Output: [1,2,3,3] | ||
// Explanation: The longest valid obstacle course at each position is: | ||
// - i = 0: [1], [1] has length 1. | ||
// - i = 1: [1,2], [1,2] has length 2. | ||
// - i = 2: [1,2,3], [1,2,3] has length 3. | ||
// - i = 3: [1,2,3,2], [1,2,2] has length 3. |
32 changes: 32 additions & 0 deletions
32
LeetCode/Algorithms/Hard/LongestZigZagPathInABinaryTree.cpp
This file contains 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,32 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
int maxStep = 0; | ||
int longestZigZag(TreeNode* root) { | ||
dfs(root, true, 0); | ||
dfs(root, false, 0); | ||
return maxStep; | ||
} | ||
|
||
void dfs(TreeNode *root, bool isLeft, int step) { | ||
if(!root) return; | ||
maxStep = max(maxStep, step); // update max step sofar | ||
if(isLeft) { | ||
dfs(root->left, false, step + 1); // keep going from root to left | ||
dfs(root->right, true, 1); // restart going from root to right | ||
} else { | ||
dfs(root->right, true, step + 1); // keep going form root to right | ||
dfs(root->left, false, 1); // restart going | ||
} | ||
} | ||
}; |
This file contains 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,32 @@ | ||
class Solution { | ||
public: | ||
int minCost(int n, vector<int>& cuts) { | ||
map<pair<int, int>, int> m; | ||
|
||
return dfs(0, n); | ||
} | ||
|
||
dfs(int left, int right) { | ||
if(right - left == 1) { | ||
return 0; | ||
} | ||
if(dp[make_pair(left, right)]) { | ||
return dp[make_pair(left, right)]; | ||
} | ||
|
||
float res = INT_MAX; | ||
|
||
for(auto &c: cuts) { | ||
if(left < c < right) { | ||
res = min(res, (r - l) + dfs(l, c) + dfs(c, r)); | ||
} | ||
} | ||
|
||
if(res == INT_MAX) { | ||
res = 0; | ||
} | ||
|
||
dp[make_pair(left, right)] = res; | ||
return res; | ||
} | ||
}; |
23 changes: 23 additions & 0 deletions
23
LeetCode/Algorithms/Hard/MinimumInsertionStepsToMakeAStringPalindrome.cpp
This file contains 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,23 @@ | ||
class Solution { | ||
public: | ||
int minInsertions(string s) { | ||
int n = s.size(); | ||
vector<vector<int>> dp(n + 1, vector<int> (n + 1)); | ||
|
||
for(int i=0; i<n; i++) { | ||
for(int j=0; j<n; j++) { | ||
if(s[i] == s[n-1-j]) { | ||
dp[i + 1][j + 1] = dp[i][j] + 1; | ||
} else { | ||
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); | ||
} | ||
// dp[i + 1][j + 1] = s[i] == s[n - 1 - j] ? dp[i][j] + 1 : | ||
} | ||
} | ||
return n - dp[n][n]; | ||
} | ||
}; | ||
|
||
|
||
// Time Complexity - O(N * N) | ||
// Space Complexity - O(N * N) |
23 changes: 23 additions & 0 deletions
23
LeetCode/Algorithms/Hard/NumberOfWaysToFormATargetStringGivenADictionary.cpp
This file contains 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,23 @@ | ||
class Solution { | ||
public: | ||
int numWays(vector<string>& words, string target) { | ||
int n = target.size(); | ||
|
||
int mod = 1e9 + 7; | ||
vector<long> res(n + 1); | ||
res[0] = 1; | ||
for(int i=0; i<words[0].size(); i++) { | ||
vector<int> count(26); | ||
|
||
for(auto &w: words) { | ||
count[w[i] - 'a']++; | ||
} | ||
|
||
for(int j=n-1; j>=0; j--) { | ||
res[j+1] += res[j] * count[target[j] - 'a'] % mod; | ||
} | ||
} | ||
|
||
return res[n] % mod; | ||
} | ||
}; |
This file contains 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,20 @@ | ||
class Solution { | ||
public: | ||
int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit) { | ||
vector<vector<int>> dp(P + 1, vector<int>(G + 1, 0)); | ||
dp[0][0] = 1; | ||
int res = 0, mod = 1e9 + 7; | ||
|
||
for(int k=0; k<group.size(); k++) { | ||
int g = group[k], p = profit[k]; | ||
for(int i=P; i>=0; i--) { | ||
for(int j=G - g; j>=0; j--) { | ||
dp[min(i + p, P)][j + g] = (dp[min(i + p, P)][j + g] + dp[i][j]) % mod; | ||
} | ||
} | ||
} | ||
|
||
for(int x: dp[P]) res = (res + x) % mod; | ||
return res; | ||
} | ||
}; |
This file contains 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,38 @@ | ||
class Solution { | ||
public: | ||
int maxSatisfaction(vector<int>& satisfaction) { | ||
sort(satisfaction.begin(), satisfaction.end()); | ||
|
||
int res = 0, total = 0, n = satisfaction.size(); | ||
for(int i=n-1; i>=0 && satisfaction[i] > -total; --i) { | ||
total += satisfaction[i]; | ||
res += total; | ||
} | ||
return res; | ||
} | ||
}; | ||
|
||
|
||
// [-1,-8,0,5,-9] | ||
|
||
// [-9, -8, -1, 0, 5] | ||
|
||
// -5 | ||
// total = 5 | 5 | 4 | ||
// res = 5 | 10 | 14 | ||
|
||
// total = -5 | -5 | -4 | | ||
|
||
|
||
a - -2 | ||
// b = 2 | ||
// c = 3 | ||
l - 3 | ||
t - 4 | ||
q - 3 | ||
|
||
t a l a q l t | ||
4 -2 3 -2 3 3 4 | ||
|
||
13 | ||
|
75 changes: 75 additions & 0 deletions
75
LeetCode/Algorithms/Hard/RemoveMaxNumberOfEdgesToKeepGraphFullyTraversable.cpp
This file contains 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,75 @@ | ||
class Solution { | ||
public: | ||
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) { | ||
// [type, ui, v1] | ||
vector<int> rootA(n + 1); | ||
vector<int> rootB(n + 1); | ||
|
||
for(int i=1; i<=n; i++) { | ||
rootA[i] = i; | ||
rootB[i] = i; | ||
} | ||
|
||
int res = 0; | ||
int aliceEdges = 0; | ||
int bobEdges = 0; | ||
|
||
for(auto &edge: edges) { | ||
if(edge[0] == 3) { | ||
if(uni(edge[1], edge[2], rootA)) { | ||
aliceEdges++; | ||
if(uni(edge[1], edge[2], rootB)) { | ||
bobEdges++; | ||
} | ||
} else { | ||
res++; | ||
} | ||
} | ||
} | ||
|
||
vector<int> rootA_copy = rootA; | ||
|
||
for(auto &edge: edges) { | ||
if(edge[0] == 1) { | ||
if(uni(edge[1], edge[2], rootA)) { | ||
aliceEdges++; | ||
} else { | ||
res++; | ||
} | ||
} | ||
} | ||
|
||
rootA = rootA_copy; | ||
|
||
for(auto &edge: edges) { | ||
if(edge[0] == 2) { | ||
if(uni(edge[1], edge[2], rootB)) { | ||
bobEdges++; | ||
} else { | ||
res++; | ||
} | ||
} | ||
} | ||
|
||
return (aliceEdges == bobEdges && aliceEdges == n - 1) ? res : -1; | ||
} | ||
|
||
bool uni(int a, int b, vector<int> &root) { | ||
int rootA = find(a, root); | ||
int rootB = find(b, root); | ||
if(rootA == rootB) { | ||
return false; | ||
} | ||
|
||
root[rootA] = rootB; | ||
|
||
return true; | ||
} | ||
|
||
int find(int a, vector<int> &root) { | ||
if(root[a] != a) { | ||
root[a] = find(root[a], root); | ||
} | ||
return root[a]; | ||
} | ||
}; |
Oops, something went wrong.