Skip to content

Commit 41d7f5c

Browse files
committed
Added 3 solutions
1 parent 56f372a commit 41d7f5c

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public static int nchoc(int A, ArrayList<Integer> B) {
3+
final int MOD = 1000000007;
4+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
5+
@Override
6+
public int compare(Integer o1, Integer o2) {
7+
return o2 - o1;
8+
}
9+
});
10+
11+
for (Integer choc : B) {
12+
pq.add(choc);
13+
}
14+
15+
long total = 0;
16+
17+
while (A-- > 0) {
18+
int choc = pq.poll();
19+
total += choc;
20+
total %= MOD;
21+
pq.add(choc/2);
22+
}
23+
24+
return (int) total;
25+
}
26+
}
27+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public ArrayList<Integer> solve(ArrayList<Integer> A, ArrayList<Integer> B) {
5+
6+
int n = A.size();
7+
PriorityQueue<SumElement> pq = new PriorityQueue<>(new Comparator<SumElement>() {
8+
@Override
9+
public int compare(SumElement o1, SumElement o2) {
10+
return o2.sum - o1.sum;
11+
}
12+
});
13+
14+
Collections.sort(A);
15+
Collections.sort(B);
16+
17+
pq.add(new SumElement(A.get(n-1) + B.get(n-1), n-1, n-1));
18+
19+
ArrayList<Integer> ans = new ArrayList<>();
20+
Set<String> set = new HashSet<>();
21+
set.add(new StringBuilder().append(n-1).append("|").append(n-1).toString());
22+
23+
while (n-- > 0) {
24+
SumElement element = pq.poll();
25+
ans.add(element.sum);
26+
if (!set.contains(new StringBuilder().append(element.ind1-1).append("|").append(element.ind2).toString())) {
27+
if (element.ind1-1 >= 0) {
28+
pq.add(new SumElement(A.get(element.ind1 - 1) + B.get(element.ind2), element.ind1 - 1, element.ind2));
29+
set.add(new StringBuilder().append(element.ind1-1).append("|").append(element.ind2).toString());
30+
}
31+
}
32+
33+
if (!set.contains(new StringBuilder(element.ind1).append("|").append(element.ind2-1).toString())) {
34+
if (element.ind2-1 >= 0) {
35+
pq.add(new SumElement(A.get(element.ind1) + B.get(element.ind2 - 1), element.ind1, element.ind2 - 1));
36+
set.add(new StringBuilder(element.ind1).append("|").append(element.ind2-1).toString());
37+
}
38+
}
39+
}
40+
41+
return ans;
42+
}
43+
}
44+
45+
class SumElement {
46+
int sum;
47+
int ind1;
48+
int ind2;
49+
50+
public SumElement(int sum, int ind1, int ind2) {
51+
this.sum = sum;
52+
this.ind1 = ind1;
53+
this.ind2 = ind2;
54+
}
55+
}
56+
57+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* public class TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode left, right, next;
6+
* TreeLinkNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public void connect(TreeLinkNode root) {
11+
if (root == null) {
12+
return;
13+
}
14+
15+
Queue<TreeLinkNode> queue = new LinkedList<>();
16+
queue.add(root);
17+
queue.add(null);
18+
19+
while (queue.peek() != null) {
20+
int size = queue.size()-1;
21+
22+
while (size-- > 0) {
23+
TreeLinkNode node = queue.remove();
24+
node.next = queue.peek();
25+
26+
if (node.left != null) {
27+
queue.add(node.left);
28+
}
29+
30+
if (node.right != null) {
31+
queue.add(node.right);
32+
}
33+
}
34+
35+
queue.remove();
36+
queue.add(null);
37+
}
38+
}
39+
}
40+

0 commit comments

Comments
 (0)