Skip to content

Commit 54429e6

Browse files
committed
add sol to fix algo
1 parent a65403a commit 54429e6

3 files changed

Lines changed: 24 additions & 34 deletions

File tree

src/main/java/sorting/FixQuickSort.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ private static int partition(int[] a, int lo, int hi) {
4343
int v = a[lo];
4444
while (true) {
4545
// find item on lo to swap
46-
while (a[++i] <= v) {
46+
// STUDENT while (a[++i] <= v) {
47+
// BEGIN STRIP
48+
while (a[++i] < v) {
49+
// END STRIP
4750
if (i == hi) break;
4851
}
4952
// find item on hi to swap
50-
while (v <= a[--j]) {
53+
// STUDENT while (v <= a[--j]) {
54+
// BEGIN STRIP
55+
while (v < a[--j]) {
56+
// END STRIP
5157
if (j == lo) break; // redundant since a[lo] acts as sentinel
5258
}
5359
// check if pointers cross

src/main/java/sorting/TrainingSessions.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sorting;
22
// BEGIN STRIP
3+
import java.util.ArrayList;
34
import java.util.Arrays;
5+
import java.util.List;
46
import java.util.PriorityQueue;
57
// END STRIP
68

@@ -52,36 +54,22 @@ public int minFacilitiesRequired(int[][] sessions) {
5254
// TODO
5355
// STUDENT return -1;
5456
// BEGIN STRIP
55-
56-
if (sessions.length == 0) {
57-
return 0;
57+
// sweep line algorithm
58+
List<int[]> events = new ArrayList<>();
59+
for (int[] session : sessions) {
60+
events.add(new int[]{session[0], 1});
61+
events.add(new int[]{session[1], -1});
5862
}
59-
// Sorting the sessions by the starting time. If they are the same compare the end time O(nlogn)
60-
Arrays.sort(sessions, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
61-
62-
// Priority queue with end times of the sessions
63-
PriorityQueue<Integer> queue = new PriorityQueue<>();
64-
queue.add(sessions[0][1]);
65-
66-
// The idea is that for each new session, we check if we can use
67-
// an existing facility or if we need an additional one of a session that was completed
68-
// This is done lazily so at the end, the number of facilities in the queue is the final answer.
69-
// Tnhis is node in O(n.log(n))
70-
for (int i = 1; i < sessions.length; i++) {
71-
// check if we can use an existing session ?
72-
if (queue.peek() <= sessions[i][0]) {
73-
// using an existing facility means deleting it from the queue
74-
// we delete the one finishing first (min priority queue)
75-
queue.poll();
76-
}
77-
queue.add(sessions[i][1]); // then we use a new facility
63+
events.sort((a, b) -> (a[0] == b[0]) ? a[1] - b[1] : a[0] - b[0]);
64+
int max = 0;
65+
int current = 0;
66+
for (int[] event : events
67+
) {
68+
current += event[1];
69+
max = Math.max(max, current);
7870
}
79-
return queue.size();
71+
return max;
8072
// END STRIP
8173
}
8274

83-
84-
85-
86-
8775
}

src/test/java/searching/FixQuickSortTest.java renamed to src/test/java/sorting/FixQuickSortTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
package searching;
1+
package sorting;
22

33
import org.javagrader.ConditionalOrderingExtension;
44
import org.javagrader.Grade;
55
import org.javagrader.GradeFeedback;
66
import org.javagrader.TestResultStatus;
77
import org.junit.jupiter.api.*;
88
import org.junit.jupiter.api.extension.ExtendWith;
9-
import sorting.FixQuickSort;
109

11-
import java.util.Collections;
12-
import java.util.List;
1310
import java.util.Random;
1411
import java.util.concurrent.TimeUnit;
15-
import java.util.stream.Collectors;
1612
import java.util.stream.IntStream;
1713

1814
import static org.junit.jupiter.api.Assertions.*;

0 commit comments

Comments
 (0)