-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
1,676 additions
and
57 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,5 +1,67 @@ | ||
__________________________________________________________________________________________________ | ||
sample 4 ms submission | ||
class Solution { | ||
public: | ||
string removeOuterParentheses(string S) { | ||
int num_left = 0; | ||
int num_right = 0; | ||
int break_point = 0; | ||
string ret = ""; | ||
for (int idx = 0; idx < S.length(); ++idx) { | ||
if (S[idx] == '(') { | ||
num_left++; | ||
} | ||
else { | ||
num_right++; | ||
} | ||
if (num_left == num_right) { | ||
ret.append(S.substr(break_point+1, idx-break_point-1)); | ||
num_left = num_right = 0; | ||
break_point = idx+1; | ||
} | ||
} | ||
return ret; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ | ||
|
||
sample 8 ms submission | ||
class Solution { | ||
public: | ||
string removeOuterParentheses(string S) { | ||
int n = S.length(); | ||
int i = 0,j = 0; | ||
int a = 0; | ||
string ans = ""; | ||
while(j < n){ | ||
if(S[j] == '('){ | ||
a -= 1; | ||
} | ||
else if(S[j] == ')'){ | ||
a += 1; | ||
} | ||
if(a == 0 && j > 0){ | ||
int k = i+1; | ||
while(k<j){ | ||
ans += S[k]; | ||
k++; | ||
} | ||
i = j + 1; | ||
} | ||
j++; | ||
} | ||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
class Solution { | ||
public: | ||
string removeOuterParentheses(string S) { | ||
string res; | ||
int opened = 0; | ||
for (char c : S) { | ||
if (c == '(' && opened++ > 0) res += c; | ||
if (c == ')' && opened-- > 1) res += c; | ||
} | ||
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 |
---|---|---|
@@ -1,5 +1,102 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 8 ms submission | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
static const int _ = []() { | ||
ios::sync_with_stdio(false); | ||
cin.tie(0); | ||
cout.tie(0); | ||
return 0; | ||
}(); | ||
class Solution { | ||
public: | ||
void help(TreeNode* node, int &sum, int cur){ | ||
if(!node){ | ||
return; | ||
} | ||
int temp = cur * 2 + node->val; | ||
help(node->left, sum, temp); | ||
help(node->right, sum, temp); | ||
if(!node->left && !node->right){ | ||
sum += temp; | ||
} | ||
} | ||
|
||
int sumRootToLeaf(TreeNode* root) { | ||
int sum = 0; | ||
help(root, sum, 0); | ||
return sum; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 12 ms submission | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
void dfs(TreeNode* root,int sum,int &ans){ | ||
if(root == NULL){ | ||
return; | ||
} | ||
if(root->left == NULL && root->right == NULL){ | ||
ans += (sum*2 + root->val); | ||
return; | ||
} | ||
dfs(root->left,sum*2 + root->val,ans); | ||
dfs(root->right,sum*2 + root->val,ans); | ||
} | ||
int sumRootToLeaf(TreeNode* root) { | ||
int ans = 0; | ||
dfs(root,0,ans); | ||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
sample 16 ms submission | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
int sumRootToLeaf(TreeNode* root) { | ||
if(root == NULL)return 0; | ||
int sum = 0; | ||
helper(root, 0, sum); | ||
return sum; | ||
} | ||
|
||
void helper(TreeNode* root, int cur, int& sum){ | ||
cur = (cur << 1) | root->val; | ||
if(root->left == NULL && root->right == NULL){ | ||
sum += cur; | ||
return; | ||
} | ||
|
||
if(root->left){ | ||
helper(root->left, cur, sum); | ||
} | ||
if(root->right){ | ||
helper(root->right, cur, sum); | ||
} | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,85 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 4 ms submission | ||
class Solution { | ||
public: | ||
vector<bool> camelMatch(vector<string>& queries, string pattern) { | ||
int n = queries.size(); | ||
vector<bool> ans(n); | ||
int sz = pattern.length(); | ||
for(int i=0;i<n;i++){ | ||
int m = queries[i].length(); | ||
int j=0,k=0; | ||
int flag = 0; | ||
while(j<m && k<sz){ | ||
if(pattern[k] == queries[i][j]){ | ||
k++; | ||
} | ||
else if(queries[i][j] >= 65 && queries[i][j] <= 90){ | ||
flag = 1;break; | ||
} | ||
j++; | ||
} | ||
while(j<m){ | ||
if(queries[i][j] >= 65 && queries[i][j] <= 90){ | ||
flag = 1;break; | ||
} | ||
j++; | ||
} | ||
if(k<sz){flag = 1;} | ||
if(flag == 0){ans[i] = true;} | ||
else{ans[i] = false;} | ||
} | ||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 8 ms submission | ||
class Solution { | ||
public: | ||
vector<bool> camelMatch(vector<string>& queries, string pattern) { | ||
vector<bool> res; | ||
for(string& s : queries){ | ||
res.push_back(match(s, pattern)); | ||
} | ||
return res; | ||
} | ||
|
||
bool match(string& query, string& pattern){ | ||
auto q = segment(query), p = segment(pattern); | ||
if(q.size() < p.size() || q.size() >= p.size() + 2) return false; | ||
int iq = 0, ip = 0; | ||
if(q.size() == p.size() + 1){ | ||
// q = "aAaAa", p = "AaAa" | ||
if(q[0][0] >= 'A' && q[0][0] <= 'Z') return false; | ||
iq = 1; | ||
} | ||
for(; ip < p.size(); ip++, iq++){ | ||
int idx = 0; | ||
if(q[iq][0] >= 'A' && q[iq][0] <= 'Z'){ | ||
if(q[iq][0] != p[ip][0]) return false; | ||
idx = 1; | ||
} | ||
if(q[iq].length() < p[ip].length()) return false; | ||
for(int i = idx; i < p[ip].length(); i++){ | ||
int next = q[iq].find_first_of(p[ip][i], idx); | ||
if(next == -1) return false; | ||
idx = next + 1; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
vector<string> segment(string& s){ | ||
vector<string> res; | ||
int begin = 0; | ||
for(int i = 1; i < s.length(); i++){ | ||
if(s[i] >= 'A' && s[i] <= 'Z'){ | ||
res.push_back(s.substr(begin, i - begin)); | ||
begin = i; | ||
} | ||
} | ||
res.push_back(s.substr(begin)); | ||
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 |
---|---|---|
@@ -1,5 +1,38 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 4 ms submission | ||
class Solution { | ||
public: | ||
int videoStitching(vector<vector<int>>& clips, int T) { | ||
sort(clips.begin(), clips.end()); | ||
vector<int> dp(101, 101); | ||
dp[0] = 0; | ||
for (auto& c : clips) | ||
for (int i = c[0] + 1; i <= c[1]; i++) | ||
dp[i] = min(dp[i], dp[c[0]] + 1); | ||
return dp[T] > 100 ? -1 : dp[T]; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 8 ms submission | ||
class Solution { | ||
public: | ||
int videoStitching(vector<vector<int>>& clips, int T) { | ||
int cur = 0; | ||
int ret = 0; | ||
while (cur < T) { | ||
vector<int> best; | ||
for (auto d : clips) { | ||
if (d[0] > cur || d[1] <= cur) continue; | ||
if (best.empty() || best[1] < d[1]) { | ||
best = d; | ||
} | ||
} | ||
if (best.empty()) break; | ||
cur = best[1]; | ||
ret++; | ||
} | ||
if (cur < T) return -1; | ||
return ret; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
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 |
---|---|---|
@@ -1,5 +1,50 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 4 ms submission | ||
class Solution { | ||
public: | ||
bool divisorGame(int N) { | ||
vector<int> mem(N+1, 0); | ||
return divisorGame(mem, N); | ||
} | ||
private: | ||
bool divisorGame(vector<int>& mem, int N){ | ||
if(N == 1) | ||
return false; | ||
if(N == 2) | ||
return true; | ||
if(mem[N] != 0) | ||
return mem[N] == 1 ? true : false; | ||
for(int i = 1; i < N; ++i){ | ||
if(N%i == 0){ | ||
if(!divisorGame(mem, N-i)){ | ||
mem[N] = 1; | ||
return true; | ||
} | ||
} | ||
} | ||
mem[N] = -1; | ||
return false; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 8 ms submission | ||
class Solution { | ||
public: | ||
bool divisorGame(int N) { | ||
vector<bool> dp(N + 1); | ||
dp[0] = false; | ||
dp[1] = false; | ||
for(int i = 2; i < N + 1; i++){ | ||
for(int j = 1; j < i; j++){ | ||
if(i % j == 0){ | ||
if(dp[i - j] == false){ | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return dp[N]; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
Oops, something went wrong.