-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_144_PS.java
109 lines (77 loc) · 2.71 KB
/
a_144_PS.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.* ;
public class a_144_PS {
static void generatePrintBinary(int n){
Queue<String> q = new LinkedList<String>();
q.add("1");
while (n-- > 0) {
String s1 = q.peek();
q.remove();
System.out.print(s1 +" ");
String s2 = s1;
q.add(s1 + "0");
q.add(s2 + "1");
}
}
public static int minCost(int arr[], int n){
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
pq.add(arr[i]) ;
}
int result = 0 ;
while (pq.size() > 1) {
int first = pq.poll();
int second = pq.poll();
result += first + second;
pq.add(first + second);
}
return result;
}
public static void reverse(Queue<Integer> q , int k) {
Stack<Integer> s = new Stack<>() ;
int n = q.size() ;
for(int i=0; i<k; i++){
s.push(q.remove()) ;
}
while(!s.isEmpty()) {
q.add(s.pop()) ;
}
for(int i=k; i<n; i++){
q.add(q.remove()) ;
}
}
public static void printMax(int arr[], int n, int k){
Deque<Integer> Qq = new LinkedList<Integer>();
int i;
for ( i = 0; i < k; ++i) {
while (!Qq.isEmpty() && arr[i] >= arr[Qq.peekLast()])
Qq.removeLast();
Qq.addLast(i);
}
for (; i < n; ++i) {
System.out.print(arr[Qq.peek()] + " ");
while ((!Qq.isEmpty()) && Qq.peek() <=i - k)
Qq.removeFirst();
while ((!Qq.isEmpty()) && arr[i] >= arr[Qq.peekLast()])
Qq.removeLast();
Qq.addLast(i);
}
System.out.print(arr[Qq.peek()]);
}
public static void main(String[] args){
// Q.1 print binary number
// int n = 10;
// generatePrintBinary(n);
// Q.2 minimum cost of ropes
// int len[] = { 4, 3, 2, 6 };
// int size = len.length;
// System.out.println("Total cost for connecting"+ " ropes is "+ minCost(len,size));
// Q.4 Reversing the first K elements of a Queue
// Queue<Integer> q = new LinkedList<>() ;
// q.add(10) ; q.add(20) ; q.add(30) ; q.add(40) ; q.add(50) ; q.add(60) ; q.add(70) ; q.add(80) ; q.add(90) ; q.add(100) ;
// Q.5 Maximum of all subarrays of size k
int arr[] = {1, 2, 3, 1, 4 , 5, 2, 3 , 6 } ;
int N = arr.length ;
int k = 3 ;
printMax(arr, N, k);
}
}