Skip to content

Commit d4f6da4

Browse files
Added solutions:
1 parent e62405f commit d4f6da4

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

amazon/vertical_sum.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
struct Node
5+
{
6+
int data;
7+
struct Node *left;
8+
struct Node *right;
9+
10+
Node(int x){
11+
data = x;
12+
left = NULL;
13+
right = NULL;
14+
}
15+
};
16+
void insert(Node *root,int a1,int a2,char lr){
17+
if(root==NULL)
18+
return;
19+
if(root->data==a1){
20+
switch(lr){
21+
case 'L':root->left=new Node(a2);
22+
break;
23+
case 'R':root->right=new Node(a2);
24+
break;
25+
}
26+
}
27+
else{
28+
insert(root->left,a1,a2,lr);
29+
insert(root->right,a1,a2,lr);
30+
}
31+
}
32+
int mn=0;
33+
int aa[10000];
34+
void printVertical(Node *root);
35+
int main(){
36+
int t;
37+
cin>>t;
38+
while(t--){
39+
memset(aa,0,sizeof(aa));
40+
int n;
41+
cin>>n;
42+
mn=0;
43+
Node *root=NULL;
44+
while(n--){
45+
int a1,a2;
46+
char lr;
47+
cin>>a1>>a2;
48+
scanf(" %c",&lr);
49+
if(root==NULL){
50+
root=new Node(a1);
51+
switch(lr){
52+
case 'L':root->left=new Node(a2);
53+
break;
54+
case 'R':root->right=new Node(a2);
55+
break;
56+
}
57+
}
58+
else{
59+
insert(root,a1,a2,lr);
60+
}
61+
}
62+
printVertical(root);
63+
cout<<endl;
64+
}
65+
}
66+
}
67+
/*This is a function problem.You only need to complete the function given below*/
68+
/*Complete the function below
69+
Node is as follows:
70+
struct Node{
71+
int data;
72+
Node *left,*right;
73+
};
74+
*/
75+
76+
map<int, int> m;
77+
void printVerticalUtil(Node *root, int hd){
78+
if(!root) return;
79+
m[hd] += root->data;
80+
printVerticalUtil(root->left, hd - 1);
81+
printVerticalUtil(root->right, hd + 1);
82+
}
83+
84+
void printVertical(Node *root)
85+
{
86+
//add code here.
87+
if(!root) return;
88+
/*
89+
queue<pair<Node*, int>> q; //node and its horizontal distance
90+
map<int, int> m; //distance
91+
92+
q.push({root, 0});
93+
94+
while(!q.empty()){
95+
pair<Node*, int> p = q.front();
96+
Node* curr = p.first;
97+
int hd = p.second;
98+
m[hd] += curr->data;
99+
100+
if(curr->left != NULL)
101+
q.push({curr->left, hd - 1});
102+
if(curr->right != NULL)
103+
q.push({curr->right, hd + s1});
104+
}*/
105+
printVerticalUtil(root, 0);
106+
107+
for(auto it = m.begin(); it != m.end(); it++)
108+
cout<<it->second<<" ";
109+
m.clear();
110+
}

count_palindromic_substrings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countSubstrings(self, s):
3+
4+
leftrights = [(x,x) for x in list(range(len(s)))] + [(x,x+1) for x in list(range(len(s)-1))]
5+
count = 0
6+
for left, right in leftrights:
7+
while left >= 0 and right < len(s) and s[left]==s[right]:
8+
count += 1
9+
left -= 1
10+
right += 1
11+
return count

0 commit comments

Comments
 (0)