Skip to content

Commit 8cd032b

Browse files
committed
Practise 14-Jul-2020
1 parent 0d516f0 commit 8cd032b

10 files changed

+658
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Given a non-empty binary tree, find the maximum path sum.
2+
3+
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
4+
5+
Example 1:
6+
7+
Input: [1,2,3]
8+
9+
1
10+
/ \
11+
2 3
12+
13+
Output: 6
14+
Example 2:
15+
16+
Input: [-10,9,20,null,null,15,7]
17+
18+
-10
19+
/ \
20+
9 20
21+
/ \
22+
15 7
23+
24+
Output: 42
25+
26+
27+
28+
29+
/**
30+
* Definition for a binary tree node.
31+
* struct TreeNode {
32+
* int val;
33+
* TreeNode *left;
34+
* TreeNode *right;
35+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
36+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
37+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
38+
* };
39+
*/
40+
class Solution {
41+
public:
42+
int maxPathSumUtil(TreeNode* root, int &res){
43+
if(!root) return 0;
44+
45+
int l=maxPathSumUtil(root->left, res);
46+
int r=maxPathSumUtil(root->right, res);
47+
48+
int temp=max(max(l,r)+root->val, root->val);
49+
int ans=max(temp, l+r+root->val);
50+
res=max(res, ans);
51+
52+
return temp;
53+
}
54+
55+
int maxPathSum(TreeNode* root) {
56+
int res=INT_MIN;
57+
maxPathSumUtil(root, res);
58+
return res;
59+
}
60+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies:
2+
3+
Si % Sj = 0 or Sj % Si = 0.
4+
5+
If there are multiple solutions, return any subset is fine.
6+
7+
Example 1:
8+
9+
Input: [1,2,3]
10+
Output: [1,2] (of course, [1,3] will also be ok)
11+
Example 2:
12+
13+
Input: [1,2,4,8]
14+
Output: [1,2,4,8]
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
vector<int> largestDivisibleSubset(vector<int>& nums) {
24+
int n=nums.size();
25+
vector<int>v;
26+
if(n==0) return v;
27+
int dp[n];
28+
for(int i=0;i<n;i++) dp[i]=1;
29+
30+
sort(nums.begin(), nums.end());
31+
32+
for(int i=1;i<n;i++){
33+
for(int j=i-1;j>=0;j--){
34+
if(nums[i]%nums[j]==0){
35+
dp[i]=max(dp[i], dp[j]+1);
36+
}
37+
}
38+
}
39+
40+
int maxIndex=0;
41+
for(int i=0;i<n;i++)
42+
maxIndex= dp[i]>dp[maxIndex] ? i: maxIndex;
43+
44+
int divCnt=dp[maxIndex];
45+
for(int i=maxIndex;i>=0;i--){
46+
if(nums[maxIndex]%nums[i]==0 && divCnt==dp[i]){
47+
divCnt--;
48+
v.push_back(nums[i]);
49+
}
50+
}
51+
reverse(v.begin(), v.end());
52+
return v;
53+
}
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies:
2+
3+
Si % Sj = 0 or Sj % Si = 0.
4+
5+
If there are multiple solutions, return any subset is fine.
6+
7+
Example 1:
8+
9+
Input: [1,2,3]
10+
Output: [1,2] (of course, [1,3] will also be ok)
11+
Example 2:
12+
13+
Input: [1,2,4,8]
14+
Output: [1,2,4,8]
15+
16+
17+
18+
19+
20+
class Solution {
21+
public:
22+
vector<int> largestDivisibleSubset(vector<int>& nums) {
23+
int n=nums.size();
24+
vector<int>v;
25+
if(n==0) return v;
26+
int dp[n];
27+
for(int i=0;i<n;i++) dp[i]=1;
28+
29+
sort(nums.begin(), nums.end());
30+
31+
for(int i=1;i<n;i++){
32+
for(int j=i-1;j>=0;j--){
33+
if(nums[i]%nums[j]==0){
34+
dp[i]=max(dp[i], dp[j]+1);
35+
}
36+
}
37+
}
38+
39+
int maxIndex=0;
40+
for(int i=0;i<n;i++)
41+
maxIndex= dp[i]>dp[maxIndex] ? i: maxIndex;
42+
43+
int divCnt=dp[maxIndex];
44+
for(int i=maxIndex;i>=0;i--){
45+
if(nums[maxIndex]%nums[i]==0 && divCnt==dp[i]){
46+
divCnt--;
47+
v.push_back(nums[i]);
48+
}
49+
}
50+
reverse(v.begin(), v.end());
51+
return v;
52+
}
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
Given a boolean expression with following symbols.
3+
4+
Symbols
5+
'T' ---> true
6+
'F' ---> false
7+
8+
And following operators filled between symbols
9+
10+
Operators
11+
& ---> boolean AND
12+
| ---> boolean OR
13+
^ ---> boolean XOR
14+
15+
Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true.
16+
17+
For Example:
18+
The expression is "T | T & F ^ T", it evaluates true
19+
in 4 ways ((T|T)&(F^T)), (T|(T&(F^T))), (((T|T)&F)^T)
20+
and (T|((T&F)^T)).
21+
22+
Return No_of_ways Mod 1003.
23+
24+
Input:
25+
First line contains the test cases T. 1<=T<=500
26+
Each test case have two lines
27+
First is length of string N. 1<=N<=100
28+
Second line is string S (boolean expression).
29+
Output:
30+
No of ways Mod 1003.
31+
32+
33+
Example:
34+
Input:
35+
2
36+
7
37+
T|T&F^T
38+
5
39+
T^F|F
40+
41+
Output:
42+
4
43+
2
44+
*/
45+
46+
#include<bits/stdc++.h>
47+
using namespace std;
48+
49+
int dp[201][201][2];
50+
51+
int booleanParenthesis(string s, int i, int j, bool isTrue){
52+
if(i>j) return 0;
53+
if(i==j){
54+
if(isTrue==true)
55+
return s[i]=='T';
56+
else return s[i]=='F';
57+
}
58+
59+
if(dp[i][j][isTrue]!=-1) return dp[i][j][isTrue];
60+
61+
int ans=0, lf, lt, rf, rt;
62+
for(int k=i+1;k<j;k+=2){
63+
if(dp[i][k-1][true]!=-1) lt=dp[i][k-1][true];
64+
else {
65+
dp[i][k-1][true]=booleanParenthesis(s, i, k-1, true);
66+
lt=dp[i][k-1][true];
67+
}
68+
if(dp[i][k-1][false]!=-1) lf=dp[i][k-1][false];
69+
else {
70+
dp[i][k-1][false]=booleanParenthesis(s, i, k-1,false);
71+
lf=dp[i][k-1][false];
72+
}
73+
if(dp[k+1][j][true]!=-1) rt=dp[k+1][j][true];
74+
else {
75+
dp[k+1][j][true]=booleanParenthesis(s, k+1, j, true);
76+
rt=dp[k+1][j][true];
77+
}
78+
if(dp[k+1][j][false]!=-1) rf=dp[k+1][j][false];
79+
else {
80+
dp[k+1][j][false]=booleanParenthesis(s, k+1, j, false);
81+
rf=dp[k+1][j][false];
82+
}
83+
84+
if(s[k]=='&'){
85+
if(isTrue==true)
86+
ans+=lt*rt;
87+
else ans+=lt*rf + lf*rt + lf*rf;
88+
} else if(s[k]=='|'){
89+
if(isTrue==true)
90+
ans+=lt*rt + lt*rf + lf*rt;
91+
else ans+=lf*rf;
92+
} else if(s[k]=='^'){
93+
if(isTrue==true)
94+
ans+=lt*rf + lf*rt;
95+
else ans+=lf*rf + lt*rt;
96+
}
97+
}
98+
return dp[i][j][isTrue]=ans%1003;
99+
}
100+
101+
int main(){
102+
ios_base::sync_with_stdio(false);
103+
cin.tie(NULL);
104+
cout.tie(NULL);
105+
int t;
106+
cin>>t;
107+
while(t--){
108+
int n;
109+
cin>>n;
110+
string s;
111+
cin>>s;
112+
memset(dp, -1, sizeof(dp));
113+
cout<<booleanParenthesis(s, 0, n-1, true)<<endl;
114+
}
115+
116+
return 0;
117+
}
118+
119+
120+
121+
122+
/*
123+
Alternative Approach
124+
125+
#include<bits/stdc++.h>
126+
using namespace std;
127+
128+
map<string, int> mp;
129+
130+
int booleanParenthesis(string s, int i, int j, bool isTrue){
131+
if(i>j) return 0;
132+
if(i==j){
133+
if(isTrue==true)
134+
return s[i]=='T';
135+
else return s[i]=='F';
136+
}
137+
138+
string tmp=to_string(i);
139+
tmp.push_back(' ');
140+
tmp.append(to_string(j));
141+
tmp.push_back(' ');
142+
tmp.append(to_string(isTrue));
143+
144+
if(mp.find(tmp)!=mp.end()) return mp[tmp];
145+
146+
int ans=0;
147+
for(int k=i+1;k<j;k+=2){
148+
int lt=booleanParenthesis(s, i, k-1, true);
149+
int lf=booleanParenthesis(s, i, k-1, false);
150+
int rt=booleanParenthesis(s, k+1, j, true);
151+
int rf=booleanParenthesis(s, k+1, j, false);
152+
153+
if(s[k]=='&'){
154+
if(isTrue==true)
155+
ans+=lt*rt;
156+
else ans+=lt*rf + lf*rt + lf*rf;
157+
} else if(s[k]=='|'){
158+
if(isTrue==true)
159+
ans+=lt*rt + lt*rf + lf*rt;
160+
else ans+=lf*rf;
161+
} else if(s[k]=='^'){
162+
if(isTrue==true)
163+
ans+=lt*rf + lf*rt;
164+
else ans+=lf*rf + lt*rt;
165+
}
166+
}
167+
return mp[tmp]=ans%1003;
168+
}
169+
170+
int main(){
171+
ios_base::sync_with_stdio(false);
172+
cin.tie(NULL);
173+
cout.tie(NULL);
174+
int t;
175+
cin>>t;
176+
while(t--){
177+
int n;
178+
cin>>n;
179+
string s;
180+
cin>>s;
181+
mp.clear();
182+
cout<<booleanParenthesis(s, 0, n-1, true)<<endl;
183+
}
184+
185+
return 0;
186+
}
187+
*/

dynamic programming/boolean_patenthesization_recursive.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ int booleanParenthesis(string s, int i, int j, bool isTrue){
5454
if(i>j) return 0;
5555
if(i==j){
5656
if(isTrue==true)
57-
return s[i]=='T'?1:0;
58-
else return s[i]=='F'?1:0;
57+
return s[i]=='T';
58+
else return s[i]=='F';
5959
}
6060
int ans=0;
6161
for(int k=i+1;k<j;k+=2){
@@ -65,7 +65,7 @@ int booleanParenthesis(string s, int i, int j, bool isTrue){
6565
int rf=booleanParenthesis(s, k+1, j, false);
6666

6767
if(s[k]=='&'){
68-
if(isTrue=true)
68+
if(isTrue==true)
6969
ans+=lt*rt;
7070
else ans+=lt*rf + lf*rt + lf*rf;
7171
} else if(s[k]=='|'){

0 commit comments

Comments
 (0)