Skip to content

Commit e23beae

Browse files
committed
nextcommit
1 parent 55b8a0f commit e23beae

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

src/revision/SumMinMaxSubarrK.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package revision;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
public class SumMinMaxSubarrK {
7+
8+
// method to find max in subarray of size K using deque in java
9+
10+
public static void maxsubk(int arr[], int k) {
11+
12+
int n = arr.length;
13+
14+
Deque<Integer> dq = new LinkedList<>();
15+
int i;
16+
for (i = 0; i < k; i++) {
17+
18+
while (!dq.isEmpty() && arr[i] >= arr[dq.peekLast()]) {
19+
dq.removeLast();
20+
21+
}
22+
dq.addLast(i);
23+
}
24+
25+
for (; i < n; i++) {
26+
27+
System.out.println(arr[dq.peek()]);
28+
29+
while (!dq.isEmpty() && dq.peek() <= (i - k)) {
30+
dq.removeFirst();
31+
}
32+
33+
while (!dq.isEmpty() && arr[i] >= arr[dq.peekLast()]) {
34+
dq.removeLast();
35+
36+
}
37+
dq.addLast(i);
38+
39+
}
40+
41+
42+
System.out.println(arr[dq.peek()]);
43+
44+
45+
46+
}
47+
48+
public static int sumMinMax(int arr[], int k) {
49+
50+
int n = arr.length;
51+
int sum = 0;
52+
Deque<Integer> asc = new LinkedList<>();
53+
Deque<Integer> desc = new LinkedList<>();
54+
55+
int i;
56+
for (i = 0; i < k; i++) {
57+
58+
while (!asc.isEmpty() && arr[asc.peekLast()] >= arr[i]) {
59+
asc.removeLast();
60+
}
61+
62+
while (!desc.isEmpty() && arr[desc.peekLast()] <= arr[i]) {
63+
desc.removeLast();
64+
}
65+
desc.addLast(i);
66+
asc.addLast(i);
67+
}
68+
69+
70+
for (; i < n; i++) {
71+
72+
sum += (arr[asc.peekFirst()] + arr[desc.peekFirst()]);
73+
74+
while (!asc.isEmpty() && asc.peek() <= (i - k)) {
75+
asc.removeFirst();
76+
}
77+
while (!desc.isEmpty() && desc.peek() <= (i - k)) {
78+
desc.removeFirst();
79+
}
80+
81+
82+
while (!asc.isEmpty() && arr[asc.peekLast()] >= arr[i]) {
83+
asc.removeLast();
84+
}
85+
86+
while (!desc.isEmpty() && arr[desc.peekLast()] <= arr[i]) {
87+
desc.removeLast();
88+
}
89+
desc.addLast(i);
90+
asc.addLast(i);
91+
92+
}
93+
94+
sum += (arr[asc.peekFirst()] + arr[desc.peekFirst()]);
95+
return sum;
96+
97+
}
98+
99+
100+
public static void main(String[] args) {
101+
102+
int arr[] = {2, 5, 6, 3, 5};
103+
int k = 2;
104+
System.out.println(sumMinMax(arr, k));
105+
}
106+
}

src/revision/circularTour.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package revision;
2+
3+
4+
class PetrolPump {
5+
6+
int petrol;
7+
int distance;
8+
9+
public PetrolPump(int petrol, int distance) {
10+
11+
this.petrol = petrol;
12+
this.distance = distance;
13+
}
14+
15+
}
16+
17+
18+
public class circularTour {
19+
20+
21+
// In this ques we are given pairs {petrol pump capacity,dist to next pump}
22+
// find the index of pump from where truck can complete entire circle.
23+
// pump are at dist in circular dist. and truck has infinite capacity.
24+
25+
26+
27+
public static int findstart(PetrolPump arr[]) {
28+
29+
int start = 0;
30+
int end = 1;
31+
32+
int n = arr.length;
33+
int currPetrol = arr[start].petrol - arr[start].distance;
34+
35+
while (end != start || currPetrol < 0) {
36+
37+
38+
while (currPetrol < 0 && end != start) {
39+
40+
currPetrol -= arr[start].petrol - arr[start].distance;
41+
start = (start + 1) % n;
42+
43+
if (start == 0) {
44+
return -1;
45+
}
46+
47+
}
48+
49+
currPetrol += arr[end].petrol - arr[end].distance;
50+
end = (end + 1) % n;
51+
52+
53+
}
54+
55+
return start;
56+
57+
}
58+
59+
public static void main(String[] args) {
60+
61+
PetrolPump arr[] = {new PetrolPump(6, 4), new PetrolPump(3, 6), new PetrolPump(7, 3)};
62+
System.out.println(findstart(arr));
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)