Skip to content

Commit 27d973a

Browse files
committed
Practise 07-Jul-2020
1 parent 6306a77 commit 27d973a

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.
2+
3+
Note:
4+
The number of people is less than 1,100.
5+
6+
7+
Example
8+
9+
Input:
10+
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
11+
12+
Output:
13+
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
14+
15+
16+
Hide Hint #1
17+
What can you say about the position of the shortest person?
18+
If the position of the shortest person is i, how many people would be in front of the shortest person?
19+
Hide Hint #2
20+
Once you fix the position of the shortest person, what can you say about the position of the second shortest person?
21+
22+
23+
24+
25+
26+
class Solution {
27+
public:
28+
static bool comp(vector<int> a, vector<int> b){
29+
return (a[0] > b[0] || (a[0]==b[0] && a[1] < b[1]));
30+
}
31+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
32+
33+
sort(people.begin(), people.end(), comp);
34+
vector<vector<int> > result;
35+
for(auto x: people)
36+
result.insert(result.begin() + x[1], x);
37+
38+
return result;
39+
}
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.
2+
3+
Note:
4+
The number of people is less than 1,100.
5+
6+
7+
Example
8+
9+
Input:
10+
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
11+
12+
Output:
13+
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
14+
15+
16+
17+
class Solution {
18+
public:
19+
static bool comp(vector<int> a, vector<int> b){
20+
return (a[0] > b[0] || (a[0]==b[0] && a[1] < b[1]));
21+
}
22+
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
23+
24+
sort(people.begin(), people.end(), comp);
25+
vector<vector<int> > result;
26+
for(auto x: people)
27+
result.insert(result.begin() + x[1], x);
28+
29+
return result;
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct node{
5+
int data;
6+
struct node* left;
7+
struct node* right;
8+
};
9+
10+
struct node* newNode(int value){
11+
struct node* temp=(struct node*)malloc(sizeof(struct node));
12+
temp->data=value;
13+
temp->left=temp->right=NULL;
14+
return temp;
15+
}
16+
17+
bool isValidBST(node *root, long long mini, long long maxi){
18+
if(!root) return true;
19+
if(root->data<=mini || root->data>=maxi) return false;
20+
return isValidBST(root->left, mini, root->data) && isValidBST(root->right, root->data, maxi);
21+
}
22+
int main(){
23+
24+
struct node* root=newNode(5);
25+
root->left=newNode(1);
26+
root->right=newNode(9);
27+
root->right->left=newNode(6);
28+
root->right->right=newNode(12);
29+
if(isValidBST(root, (long long)INT_MIN-1, (long long)INT_MAX+1)) cout<<"VALID BST"<<endl;
30+
else cout<<"INVALID BST"<<endl;
31+
32+
return 0;
33+
}

tree/range_sum_query_segment_tree.cpp

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int max_size;
4+
5+
6+
// st, si, ss and se are same as getSumUtil()
7+
// i --> index of the element to be updated
8+
// diff --> Value to be added to all nodes which have i in range
9+
10+
void updateValueUtil(int *st, int ss, int se, int i, int diff, int si){
11+
// Base Case: If the input index lies outside the range of this segment
12+
if (i < ss || i > se)
13+
return;
14+
15+
// If the input index is in range of this node, then update the value of the node and its children
16+
st[si] = st[si] + diff;
17+
if (se != ss){
18+
int mid = ss + (se - ss)/2;
19+
updateValueUtil(st, ss, mid, i, diff, 2*si + 1);
20+
updateValueUtil(st, mid+1, se, i, diff, 2*si + 2);
21+
}
22+
}
23+
24+
25+
26+
void updateValue(int arr[], int *st, int n, int i, int new_val){
27+
// Check for erroneous input index
28+
if (i < 0 || i > n-1){
29+
cout<<"Invalid Input";
30+
return;
31+
}
32+
33+
// Get the difference between new value and old value
34+
int diff = new_val - arr[i];
35+
36+
// Update the value in array
37+
arr[i] = new_val;
38+
39+
// Update the values of nodes in segment tree
40+
updateValueUtil(st, 0, n-1, i, diff, 0);
41+
}
42+
43+
44+
45+
46+
// st --> Pointer to segment tree
47+
// si --> Index of current node in the segment tree.
48+
// Initially 0 is passed as root is always at index 0
49+
// ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[si]
50+
// qs & qe --> Starting and ending indexes of query range
51+
52+
int getSumUtil(int *st, int ss, int se, int qs, int qe, int si){
53+
// If segment of this node is a part of given range, then return the sum of the segment ( Total Overlap )
54+
if (qs <= ss && qe >= se)
55+
return st[si];
56+
57+
// If segment of this node is outside the given range ( No Overlap )
58+
if (qe < ss || qs > se)
59+
return 0;
60+
61+
// If a part of this segment overlaps with the given range
62+
int mid = ss + (se - ss)/2;
63+
64+
return getSumUtil(st, ss, mid, qs, qe, 2*si+1) + getSumUtil(st, mid+1, se, qs, qe, 2*si+2);
65+
}
66+
67+
68+
69+
int getSum(int *st, int n, int qs, int qe){
70+
// Check for erroneous input values
71+
if (qs < 0 || qe > n-1 || qs > qe){
72+
cout<<"Invalid Input";
73+
return -1;
74+
}
75+
return getSumUtil(st, 0, n-1, qs, qe, 0);
76+
}
77+
78+
79+
80+
int constructSTUtil(int arr[], int ss, int se, int *st, int si){ // arr[ss......se]
81+
82+
// If there is one element in array, store it in current node of segment tree and return
83+
if (ss == se){
84+
st[si] = arr[ss];
85+
return arr[ss];
86+
}
87+
88+
// If there are more than one elements, then recur for left and right subtrees and store the sum of values in this node
89+
int mid = ss + (se - ss)/2;
90+
91+
st[si] = constructSTUtil(arr, ss, mid, st, 2*si+1) + constructSTUtil(arr, mid+1, se, st, 2*si+2);
92+
93+
return st[si];
94+
}
95+
96+
97+
98+
int *constructST(int arr[], int n){
99+
int x = (int)(ceil(log2(n))); // height of tree
100+
max_size = 2*(int)pow(2, x) - 1; // total number of nodes are 2n-1 but we also need space for dummy nodes
101+
int *st = new int[max_size];
102+
constructSTUtil(arr, 0, n-1, st, 0);
103+
return st;
104+
}
105+
106+
107+
108+
109+
int main(){
110+
int arr[] = {1, 3, 5, 7, 9, 11};
111+
int n = sizeof(arr)/sizeof(arr[0]);
112+
113+
int *st = constructST(arr, n);
114+
115+
cout<<getSum(st, n, 1, 3)<<endl; // 15
116+
updateValue(arr, st, n, 1, 10); //arr[1]=10
117+
cout<<getSum(st, n, 1, 3)<<endl; // 22
118+
119+
return 0;
120+
}

0 commit comments

Comments
 (0)