Skip to content

Commit 75ea8a9

Browse files
author
Sherali Obidov
committed
another nice problem
1 parent e57e1d4 commit 75ea8a9

4 files changed

Lines changed: 111 additions & 22 deletions

File tree

‎src/problems/Medium.txt‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,3 +3156,29 @@ Note:
31563156
times is a strictly increasing array with all elements in [0, 10^9].
31573157
TopVotedCandidate.q is called at most 10000 times per test case.
31583158
TopVotedCandidate.q(int t) is always called with t >= times[0].
3159+
3160+
203)Complete Binary Tree Inserter
3161+
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
3162+
3163+
Write a data structure CBTInserter that is initialized with a complete binary tree and supports the following operations:
3164+
3165+
CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root;
3166+
CBTInserter.insert(int v) will insert a TreeNode into the tree with value node.val = v so that the tree remains complete, and returns the value of the parent of the inserted TreeNode;
3167+
CBTInserter.get_root() will return the head node of the tree.
3168+
3169+
3170+
Example 1:
3171+
3172+
Input: inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]]
3173+
Output: [null,1,[1,2]]
3174+
Example 2:
3175+
3176+
Input: inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]]
3177+
Output: [null,3,4,[1,2,3,4,5,6,7,8]]
3178+
3179+
3180+
Note:
3181+
3182+
The initial given tree is complete and contains between 1 and 1000 nodes.
3183+
CBTInserter.insert is called at most 10000 times per test case.
3184+
Every value of a given or inserted node is between 0 and 5000.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package problems.medium;
2+
3+
import problems.utils.TreeNode;
4+
5+
import java.util.*;
6+
7+
/**
8+
* Why Did you create this class? what does it do?
9+
*/
10+
public class CBTInserter {
11+
public static void main(String[] args) {
12+
13+
CBTInserter inserter = new CBTInserter(new TreeNode(1));
14+
inserter.insert(2);
15+
inserter.insert(3);
16+
inserter.insert(4);
17+
inserter.insert(5);
18+
inserter.insert(6);
19+
System.out.println(inserter.get_root());
20+
}
21+
22+
TreeNode root;
23+
int level = 0;
24+
Map<Integer, List<TreeNode>> map = new HashMap<>();
25+
26+
public CBTInserter(TreeNode root) {
27+
this.root = root;
28+
pop(root);
29+
}
30+
31+
void pop(TreeNode x) {
32+
int l = 1;
33+
Queue<TreeNode> q = new LinkedList<>();
34+
q.add(x);
35+
List<TreeNode> list = new ArrayList<>(q);
36+
map.put(1, list);
37+
level = 1;
38+
while (!q.isEmpty()) {
39+
l--;
40+
TreeNode c = q.remove();
41+
if (c.left != null)
42+
q.add(c.left);
43+
if (c.right != null)
44+
q.add(c.right);
45+
if (l == 0 && q.size() > 0) {
46+
l += q.size();
47+
map.put(++level, new ArrayList<>(q));
48+
}
49+
}
50+
}
51+
52+
public int insert(int v) {
53+
54+
TreeNode nn = new TreeNode(v);
55+
int levelSize = map.get(level).size();
56+
if (levelSize == (int) Math.pow(2, level - 1)) {
57+
TreeNode parent = map.get(level).get(0);
58+
parent.left = nn;
59+
level++;
60+
map.put(level, new ArrayList<>());
61+
map.get(level).add(nn);
62+
return parent.val;
63+
} else {
64+
int sz = map.get(level).size();
65+
TreeNode parent = map.get(level - 1).get(sz / 2);
66+
if (parent.left == null)
67+
parent.left = nn;
68+
else
69+
parent.right = nn;
70+
map.get(level).add(nn);
71+
return parent.val;
72+
}
73+
74+
}
75+
76+
public TreeNode get_root() {
77+
return root;
78+
}
79+
}

‎src/problems/medium/NumberofIslands.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public int numIslands(char[][] a) {
1414
}
1515
}
1616
}
17-
1817
return counter - 1;
1918
}
2019

‎src/problems/medium/TopVotedCandidate.java‎

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,22 @@
33
import java.util.Arrays;
44
import java.util.HashMap;
55
import java.util.Map;
6-
import java.util.TreeMap;
76

87
/**
98
* Why Did you create this class? what does it do?
109
*/
1110
public class TopVotedCandidate {
12-
public static void main(String[] args) {
13-
TopVotedCandidate candidate = new TopVotedCandidate(new int[] {
14-
0, 1, 1, 0, 0, 1, 0
15-
}, new int[] {
16-
0, 5, 10, 15, 20, 25, 30
17-
});
18-
System.out.println(candidate.q(3));
19-
System.out.println(candidate.q(12));
20-
System.out.println(candidate.q(25));
21-
System.out.println(candidate.q(15));
22-
System.out.println(candidate.q(24));
23-
System.out.println(candidate.q(8));
24-
25-
}
26-
2711
int[] persons;
2812
int[] times;
29-
int[] cans;
13+
int[] candidates;
3014
Map<Integer, Integer> votes;
3115

3216
public TopVotedCandidate(int[] persons, int[] times) {
3317
this.persons = persons;
3418
this.times = times;
3519
this.votes = new HashMap<>();
36-
this.cans = new int[persons.length + 1];
20+
this.candidates = new int[persons.length + 1];
21+
3722
preProcess();
3823
}
3924

@@ -48,10 +33,10 @@ void preProcess() {
4833
for (int i = 0; i < times.length; i++) {
4934
int votedTime = times[i];
5035
int votedPerson = persons[i];
51-
cans[votedPerson]++;
52-
if (cans[votedPerson] >= maxVote) {
36+
candidates[votedPerson]++;
37+
if (candidates[votedPerson] >= maxVote) {
5338
maxPerson = votedPerson;
54-
maxVote = cans[votedPerson];
39+
maxVote = candidates[votedPerson];
5540
}
5641
votes.put(votedTime, maxPerson);
5742
}

0 commit comments

Comments
 (0)