Skip to content

Commit 8b01e34

Browse files
author
Skm2000
committed
DP
1 parent c966bd8 commit 8b01e34

5 files changed

+139
-0
lines changed

Diff for: Diameter_Of_Binary_Tree.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//Function Written only
2+
int helper(Node* node,int &res){
3+
if(node==NULL) return 0;
4+
int left=helper(node->left,res);
5+
int right=helper(node->right,res);
6+
int tempAns=1+max(left,right);
7+
int ans=max(tempAns,1+left+right);
8+
res=max(res,ans);
9+
return tempAns;
10+
}
11+
int diameter(Node* node) {
12+
int res=INT_MIN;
13+
helper(node,res);
14+
return res;
15+
}

Diff for: Jump_Game_3.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool canReach(vector<int>& arr, int start) {
4+
int N = arr.size();
5+
vector<int> tab(N, 0);
6+
return help(arr, start, tab, N);
7+
}
8+
bool help(vector<int>& arr, int start, vector<int>& tab, int N) {
9+
if ((start < 0 || start > N - 1) || tab[start] == 1) {
10+
return false;
11+
}
12+
if (arr[start] == 0) {
13+
return true;
14+
}
15+
tab[start] = 1;
16+
if (help(arr, start-arr[start], tab, N)) {
17+
return true;
18+
}
19+
if (help(arr, start+arr[start], tab, N)) {
20+
return true;
21+
}
22+
return false;
23+
}
24+
};

Diff for: Jump_Game_5.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
//LEETCODE QUESTION
4+
5+
int dp[1001];
6+
int helper(vector<int>& arr,int d,int i){
7+
if(dp[i]!=-1) return dp[i];
8+
int ans=1,n=arr.size();
9+
//right
10+
for(int j=i+1;j<=min(n-1,i+d)&&arr[i]>arr[j];j++)ans=max(ans,1+helper(arr,d,j));
11+
//left
12+
for(int j=i-1;j>=max(0,i-d)&&arr[i]>arr[j];j--)ans=max(ans,1+helper(arr,d,j));
13+
return dp[i]=ans;
14+
}
15+
int maxJumps(vector<int>& arr, int d) {
16+
int ans=1;
17+
for(int i=0;i<arr.size();i++)dp[i]=-1;
18+
for(int i=0;i<arr.size();i++)ans=max(ans,helper(arr,d,i));
19+
return ans;
20+
}

Diff for: Last_Stone_3.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
//Leetcode Question
4+
5+
//Recurssion base soln
6+
int helper(vector<int>& s,int i){
7+
if(i>=s.size()) return 0;
8+
else{
9+
int ans=INT_MIN;
10+
ans=max(ans,s[i]-helper(s,i+1));
11+
if(i+1<s.size()) ans=max(ans,s[i]+s[i+1]-helper(s,i+2));
12+
if(i+2<s.size()) ans=max(ans,s[i]+s[i+1]+s[i+2]-helper(s,i+3));
13+
return ans;
14+
}
15+
}
16+
string stoneGameIII(vector<int>& s) {
17+
int ans=helper(s,0);
18+
if(ans<0) return "Bob";
19+
else if(ans==0) return "Tie";
20+
return "Alice";
21+
}
22+
//Memonization based soln
23+
int dp[500001];
24+
int helper(vector<int>&s,int i){
25+
if(i>=s.size()) return 0;
26+
if(dp[i]!=-1) return dp[i];
27+
else{
28+
int ans=INT_MIN;
29+
ans=max(ans,s[i]-helper(s,i+1));
30+
if(i+1<s.size()) ans=max(ans,s[i]+s[i+1]-helper(s,i+2));
31+
if(i+2<s.size()) ans=max(ans,dp[i]+dp[i+1]+dp[i+2]-helper(s,i+3));
32+
return dp[i]=ans;
33+
}
34+
}
35+
string stoneGameIII(vector<int>& s) {
36+
for(int i=0;i<s.size();i++)dp[i]=-1;
37+
int ans=helper(s,0);
38+
if(ans<0) return "Bob";
39+
else if(ans==0) return "Tie";
40+
return "Alice";
41+
}
42+
//Bottom Up approach
43+
string stoneGameIII(vector<int>& s) {
44+
int n=s.size();
45+
vector<int>dp(n+1,0);
46+
int i=n-1;
47+
while(i>=0){
48+
int ans=INT_MIN;
49+
ans=max(ans,s[i]-dp[i+1]);
50+
if(i+1<s.size()) ans=max(ans,s[i]+s[i+1]-dp[i+2]);
51+
if(i+2<s.size()) ans=max(ans,s[i]+s[i+1]+s[i+2]-dp[i+3]);
52+
dp[i]=ans;
53+
}
54+
int ans=dp[0];
55+
if(ans<0) return "Bob";
56+
else if(ans==0) return "Tie";
57+
return "Alice";
58+
}
59+
60+
int main(){
61+
//take input
62+
}

Diff for: Maximum_Path_Sum_in_Binary_tree.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//Only function Written
2+
class Solution {
3+
int maxToRoot(TreeNode *root, int &re) {
4+
if (!root) return 0;
5+
int l = maxToRoot(root->left, re);
6+
int r = maxToRoot(root->right, re);
7+
if (l < 0) l = 0;
8+
if (r < 0) r = 0;
9+
re=max(re,l+r+root->val);
10+
return root->val += max(l, r);
11+
}
12+
public:
13+
int maxPathSum(TreeNode *root) {
14+
int re = -2147483648;
15+
maxToRoot(root, re);
16+
return re;
17+
}
18+
};

0 commit comments

Comments
 (0)