Skip to content

Commit 99df4d1

Browse files
Add files via upload
1 parent 2b49d6f commit 99df4d1

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

BuildAnArrayWithStackOperations.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
You are given an integer array target and an integer n.
3+
You have an empty stack with the two following operations:
4+
"Push": pushes an integer to the top of the stack.
5+
"Pop": removes the integer on the top of the stack.
6+
You also have a stream of the integers in the range [1, n].
7+
Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:
8+
If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
9+
If the stack is not empty, pop the integer at the top of the stack.
10+
If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.
11+
Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them.
12+
13+
Example 1:
14+
Input: target = [1,3], n = 3
15+
Output: ["Push","Push","Pop","Push"]
16+
Explanation: Initially the stack s is empty. The last element is the top of the stack.
17+
Read 1 from the stream and push it to the stack. s = [1].
18+
Read 2 from the stream and push it to the stack. s = [1,2].
19+
Pop the integer on the top of the stack. s = [1].
20+
Read 3 from the stream and push it to the stack. s = [1,3].
21+
22+
Example 2:
23+
Input: target = [1,2,3], n = 3
24+
Output: ["Push","Push","Push"]
25+
Explanation: Initially the stack s is empty. The last element is the top of the stack.
26+
Read 1 from the stream and push it to the stack. s = [1].
27+
Read 2 from the stream and push it to the stack. s = [1,2].
28+
Read 3 from the stream and push it to the stack. s = [1,2,3].
29+
30+
Example 3:
31+
Input: target = [1,2], n = 4
32+
Output: ["Push","Push"]
33+
Explanation: Initially the stack s is empty. The last element is the top of the stack.
34+
Read 1 from the stream and push it to the stack. s = [1].
35+
Read 2 from the stream and push it to the stack. s = [1,2].
36+
Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
37+
The answers that read integer 3 from the stream are not accepted.
38+
*/
39+
40+
class Solution {
41+
public:
42+
vector<string> buildArray(vector<int>& target, int n) {
43+
int i=1, j=0, sz=target.size();
44+
vector<string> result;
45+
while(i<n+1 and j<sz){
46+
result.push_back("Push");
47+
if(target[j]==i){
48+
j++;
49+
}else{
50+
result.push_back("Pop");
51+
}
52+
i++;
53+
}
54+
return result;
55+
}
56+
};

CountNodesEqualToAverageOfSubtree.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.
3+
Note:
4+
The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
5+
A subtree of root is a tree consisting of root and all of its descendants.
6+
7+
Example 1:
8+
Input: root = [4,8,5,0,1,null,6]
9+
Output: 5
10+
Explanation:
11+
For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4.
12+
For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5.
13+
For the node with value 0: The average of its subtree is 0 / 1 = 0.
14+
For the node with value 1: The average of its subtree is 1 / 1 = 1.
15+
For the node with value 6: The average of its subtree is 6 / 1 = 6.
16+
17+
Example 2:
18+
Input: root = [1]
19+
Output: 1
20+
Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1.
21+
*/
22+
23+
lass Solution {
24+
public:
25+
int result=0;
26+
pair<int,int> helper(TreeNode* root){
27+
if(!root) return {0,0};
28+
int counter=1, sum=root->val;
29+
auto p1=helper(root->left);
30+
auto p2=helper(root->right);
31+
counter+=(p1.first+p2.first);
32+
sum+=(p1.second+p2.second);
33+
if(sum/counter == root->val) result++;
34+
return {counter,sum};
35+
}
36+
int averageOfSubtree(TreeNode* root){
37+
helper(root);
38+
return result;
39+
}
40+
};

0 commit comments

Comments
 (0)