Skip to content

Commit 803cabd

Browse files
committed
feat: solve problem
1 parent 789baf7 commit 803cabd

22 files changed

+541
-31
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
//Runtime: 4 ms, faster than 96.40% of C++ online submissions for Construct Binary Search Tree from Preorder Traversal.
12+
//Memory Usage: 8.7 MB, less than 97.42% of C++ online submissions for Construct Binary Search Tree from Preorder Traversal.
13+
public:
14+
TreeNode* bstFromPreorder(vector<int>& preorder) {
15+
if(not preorder.size())
16+
return nullptr;
17+
auto r=new TreeNode(preorder[0]);
18+
auto now=r;
19+
vector<TreeNode*>st;
20+
for(int i=1;i<preorder.size();++i){
21+
auto t=preorder[i];
22+
if(t<now->val){
23+
st.push_back(now);
24+
now=now->left=new TreeNode(t);
25+
}
26+
else{
27+
while(st.size()&&t>st.back()->val){
28+
now=st.back();
29+
st.pop_back();
30+
}
31+
now=now->right=new TreeNode(t);
32+
}
33+
}
34+
return r;
35+
}
36+
};

cpp/1025.DivisorGame.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Divisor Game.
3+
//Memory Usage: 8.6 MB, less than 12.27% of C++ online submissions for Divisor Game.
4+
public:
5+
bool divisorGame(int N) {
6+
vector<int>dp={0,0,1,0};
7+
dp.resize(N+1);
8+
for(int i=4;i<=N;++i){
9+
dp[i]=0;
10+
for(int j=i/2;j>=1;--j){
11+
if(i%j==0 and dp[i-j]==0){
12+
dp[i]=1;
13+
break;
14+
}
15+
}
16+
}
17+
return dp[N];
18+
}
19+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
//Runtime: 8 ms, faster than 92.53% of C++ online submissions for Maximum Difference Between Node and Ancestor.
12+
public:
13+
int maxAncestorDiff(TreeNode* root) {
14+
int ret=0;
15+
function<void(TreeNode*,int,int)> recursion = [&ret,&recursion](auto root,auto maxVal,auto minVal){
16+
ret=max({abs(maxVal-root->val),abs(minVal-root->val),ret});
17+
maxVal=max(root->val,maxVal);
18+
minVal=min(root->val,minVal);
19+
if(root->left)
20+
recursion(root->left,maxVal,minVal);
21+
if(root->right)
22+
recursion(root->right,maxVal,minVal);
23+
};
24+
recursion(root,root->val,root->val);
25+
return ret;
26+
}
27+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
//Runtime: 20 ms, faster than 79.16% of C++ online submissions for Construct Binary Tree from Preorder and Inorder Traversal.
12+
//Memory Usage: 19.2 MB, less than 25.65% of C++ online submissions for Construct Binary Tree from Preorder and Inorder Traversal.
13+
public:
14+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
15+
if(!preorder.size())
16+
return nullptr;
17+
using itr = vector<int>::iterator;
18+
function<TreeNode*(itr,itr,itr&)> build=[&build](itr first,itr last,itr& t)->TreeNode* {
19+
auto it=find(first,last,*t);
20+
if(it==last)
21+
return nullptr;
22+
TreeNode *root=new TreeNode(*t++);
23+
root->left=build(first,it,t);
24+
root->right=build(it+1,last,t);
25+
return root;
26+
};
27+
auto it=begin(preorder);
28+
return build(begin(inorder),end(inorder),it);
29+
}
30+
};

cpp/105.ConstructBinaryTreeFromPreorderAndInorderTraversal/recursion.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

cpp/239.SlidingWindowMaximum.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
//Runtime: 60 ms, faster than 67.00% of C++ online submissions for Sliding Window Maximum.
3+
//Memory Usage: 13.4 MB, less than 27.55% of C++ online submissions for Sliding Window Maximum.
4+
public:
5+
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
6+
if(k>=nums.size()&&nums.size())
7+
return {*max_element(begin(nums),end(nums))};
8+
if(k<=0)
9+
return {};
10+
deque<int> dq;
11+
vector<int>ret;
12+
for(int i=0;i<nums.size();++i){
13+
if(dq.empty() or dq.back()>=nums[i])
14+
dq.push_back(nums[i]);
15+
else{
16+
while(dq.size() and dq.back()<nums[i])
17+
dq.pop_back();
18+
dq.push_back(nums[i]);
19+
}
20+
if(i+1>=k){
21+
ret.push_back(dq.front());
22+
if(nums[i+1-k]==dq.front())
23+
dq.pop_front();
24+
}
25+
}
26+
return ret;
27+
}
28+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
//Runtime: 124 ms, faster than 15.73% of C++ online submissions for Remove Invalid Parentheses.
3+
//Memory Usage: 10.9 MB, less than 64.65% of C++ online submissions for Remove Invalid Parentheses.
4+
public:
5+
vector<string> removeInvalidParentheses(string_view s) {
6+
unordered_set<string> ret;
7+
using itr=string_view::iterator;
8+
function<void(itr,itr,int,string&)> dfs=[&ret,&dfs](auto first,auto last,auto remain,auto &now){
9+
if(ret.size() and last-first+now.size() < begin(ret)->size())
10+
return;
11+
if(first==last){
12+
if(remain)
13+
return;
14+
if(not ret.size() or begin(ret)->size()==now.size())
15+
ret.insert(now);
16+
if(begin(ret)->size()<now.size())
17+
ret={now};
18+
return;
19+
}
20+
int remainIfPush;
21+
switch(*first){
22+
case '(':remainIfPush=remain+1;break;
23+
case ')':remainIfPush=remain-1;break;
24+
default:remainIfPush=remain;
25+
}
26+
if(remainIfPush>=0){
27+
now.push_back(*first);
28+
dfs(first+1,last,remainIfPush,now);
29+
now.pop_back();
30+
}
31+
dfs(first+1,last,remain,now);
32+
};
33+
string now;
34+
dfs(begin(s),end(s),0,now);
35+
return {begin(ret),end(ret)};
36+
}
37+
};

cpp/394.DecodeString.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution
2+
//Runtime: 4 ms, faster than 84.37% of C++ online submissions for Decode String.
3+
//Memory Usage: 8.7 MB, less than 91.46% of C++ online submissions for Decode String.
4+
public:
5+
string decodeString(string s) {
6+
function<string(string_view)> recursion=[&recursion](auto s){
7+
string res;
8+
for (int i=0;i<s.length();)
9+
if (!isdigit(s[i]))
10+
res += s[i++];
11+
else {
12+
int n = 0;
13+
while (i < s.length() && isdigit(s[i]))
14+
n = n * 10 + s[i++] - '0';
15+
int j=i+1;
16+
for(int count=1;count;++j)
17+
if(s[j]=='[')
18+
++count;
19+
else if(s[j]==']')
20+
--count;
21+
for(auto t=recursion(s.substr(i+1,(j-2)-(i+1)+1));n;--n)
22+
res += t;
23+
i=j;
24+
}
25+
return res;
26+
};
27+
return recursion(s);
28+
}
29+
};

cpp/399.EvaluateDivision.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
//speed can be improved but it reach 0ms now
3+
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Evaluate Division.
4+
//Memory Usage: 9.3 MB, less than 81.50% of C++ online submissions for Evaluate Division.
5+
public:
6+
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
7+
using pp = pair<bool, double>;
8+
using sv = string_view;
9+
unordered_map<sv, vector<pair<sv, double>>> children;
10+
for (int i = 0; i < equations.size(); i++) {
11+
children[equations[i][0]].push_back({equations[i][1], values[i]});
12+
children[equations[i][1]].push_back({equations[i][0], 1.0 / values[i]});
13+
}
14+
unordered_set<sv> visited;
15+
function<pp(sv,sv,double)> dfs=[&dfs,&visited,&children](sv a, sv b, double val)->pp {
16+
if(!visited.count(a)){
17+
visited.insert(a);
18+
for (auto &p : children[a])
19+
if (p.first == b)
20+
return {true, val * p.second};
21+
else if (auto result=dfs(p.first,b,val*p.second);result.first)
22+
return result;
23+
}
24+
return {false, -1.0};
25+
};
26+
vector<double> ans;
27+
for (auto &p : queries) {
28+
ans.push_back(p[0] == p[1] && children.count(p[0]) ? 1.0 : dfs(p[0], p[1], 1.0).second);
29+
visited.clear();
30+
}
31+
return ans;
32+
}
33+
};

cpp/403.FrogJump.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
//Runtime: 52 ms, faster than 80.66% of C++ online submissions for Frog Jump.
3+
//Memory Usage: 145.6 MB, less than 5.04% of C++ online submissions for Frog Jump.
4+
public:
5+
bool canCross(vector<int>& stones) {
6+
vector<vector<int>> visited(stones.size(),vector<int>(stones.size()));
7+
function<bool(int,int)> dfs=[&stones,&visited,&dfs](int now,int k){
8+
for (int i=now+1;i<stones.size();++i) {
9+
int gap = stones[i] - stones[now];
10+
if (gap < k - 1)
11+
continue;
12+
if (gap > k + 1)
13+
return false;
14+
if (not visited[now][i] and dfs(i, gap))
15+
return true;
16+
else
17+
visited[now][i]=1;
18+
}
19+
return now==stones.size()-1;
20+
};
21+
return dfs(0,0);
22+
}
23+
};

0 commit comments

Comments
 (0)