forked from Anuj-Kumar-Sharma/Java-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
88 lines (69 loc) · 1.83 KB
/
Copy pathMainClass.java
File metadata and controls
88 lines (69 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package priorityQueue2;
import java.util.*;
public class MainClass {
//////// find median in a running stream
PriorityQueue<Integer> minHeap, maxHeap;
boolean even;
public MedianFinder() {
minHeap = new PriorityQueue<>();
maxHeap = new PriorityQueue<>(Collections.reverseOrder());
even = true;
}
public void addNum(int num) {
if(even) {
minHeap.offer(num);
maxHeap.offer(minHeap.poll());
} else {
maxHeap.offer(num);
minHeap.offer(maxHeap.poll());
}
even = !even;
}
public double findMedian() {
if(even) {
return (minHeap.peek() + maxHeap.peek())/2.0;
} else {
return (double)maxHeap.peek();
}
}
///////////
static int splitArrayInKSubsets(int a[], int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i<k; i++) {
pq.offer(0);
}
Arrays.sort(a);
for(int i = a.length-1; i>=0; i--) {
int cur = a[i];
int top = pq.poll();
int toAdd = cur + top;
pq.offer(toAdd);
}
int max = -1;
for(int e: pq) {
max = Math.max(max, e);
}
return max;
}
//Function to return the minimum cost of connecting the ropes.
long minCost(long a[], int n) {
PriorityQueue<Long> pq = new PriorityQueue<>();
for(long e: a) {
pq.offer(e);
}
long ans = 0;
while(pq.size() > 1) {
long first = pq.poll();
long second = pq.poll();
long cost = first + second;
ans += cost;
pq.offer(cost);
}
return ans;
}
public static void main(String[] args) {
int a[] = {1, 4, 2, 3, 7, 2, 4, 5, 6, 3};
int k = 3;
System.out.println(splitArrayInKSubsets(a, k));
}
}