Skip to content

Commit 9fc05a0

Browse files
committed
biweekly-51
Signed-off-by: ashKIK <[email protected]>
1 parent c1b74c5 commit 9fc05a0

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

biweekly-51/ClosestRoom.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
import java.util.TreeSet;
4+
5+
// https://leetcode.com/contest/biweekly-contest-51/problems/closest-room/
6+
7+
public class ClosestRoom {
8+
9+
// We sort queries by the decreasing of its minSize order.
10+
// We sort rooms by the decreasing of its size order.
11+
// We initialize roomIdsSoFar TreeSet,
12+
// this includes all room ids which have size >=minSize of current query so far.
13+
// For each query:
14+
// - Add all room ids which have size >=minSize of current query.
15+
// - Query floor and ceiling of q[0] (preferredId) from roomIdsSoFar
16+
// to pick the id which closest to our preferredId
17+
18+
// online query processing
19+
// floor and ceiling operate in O(logN)
20+
// TC: O(NlogN + KlogK + KlogN)
21+
// SC: O(N + K)
22+
public int[] closestRoom(int[][] rooms, int[][] queries) {
23+
Integer[] indices = new Integer[queries.length];
24+
for (int i = 0; i < queries.length; i++) {
25+
indices[i] = i;
26+
}
27+
// Sort by decreasing order of room size
28+
Arrays.sort(rooms, Comparator.comparingInt(a -> a[1]));
29+
// Sort by decreasing order of query minSize
30+
Arrays.sort(indices, (a, b) -> Integer.compare(queries[b][1], queries[a][1]));
31+
TreeSet<Integer> roomIdsSoFar = new TreeSet<>();
32+
int[] result = new int[queries.length];
33+
int id = 0;
34+
for (int index : indices) {
35+
// Add id of the room when its size >= query minSize
36+
while (id < rooms.length && rooms[id][1] >= queries[index][1]) {
37+
roomIdsSoFar.add(rooms[id++][0]);
38+
}
39+
result[index] = searchClosetRoomId(roomIdsSoFar, queries[index][0]);
40+
}
41+
return result;
42+
}
43+
44+
private int searchClosetRoomId(TreeSet<Integer> roomIdsSoFar, int preferredId) {
45+
Integer floor = roomIdsSoFar.floor(preferredId);
46+
Integer ceiling = roomIdsSoFar.ceiling(preferredId);
47+
int k = Integer.MAX_VALUE;
48+
int closestRoomId = -1;
49+
if (floor != null) {
50+
closestRoomId = floor;
51+
k = Math.abs(preferredId - floor);
52+
}
53+
if (ceiling != null) {
54+
if (k > Math.abs(preferredId - ceiling)) {
55+
closestRoomId = ceiling;
56+
}
57+
}
58+
return closestRoomId;
59+
}
60+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Arrays;
2+
3+
// https://leetcode.com/contest/biweekly-contest-51/problems/maximum-element-after-decreasing-and-rearranging/
4+
5+
public class MaximumElementAfterDecreasingAndRearranging {
6+
7+
public int maximumElementAfterDecrementingAndRearranging(int[] arr) {
8+
Arrays.sort(arr);
9+
int cur = 0;
10+
for (int v : arr) {
11+
cur = Math.min(cur + 1, v);
12+
}
13+
return cur;
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
// https://leetcode.com/contest/biweekly-contest-51/problems/replace-all-digits-with-characters/
3+
4+
public class ReplaceAllDigitsWithCharacters {
5+
6+
public String replaceDigits(String s) {
7+
StringBuilder str = new StringBuilder(s.length());
8+
for (int i = 0; i < s.length(); i++) {
9+
str.append(i % 2 == 0 ? s.charAt(i) : shift(s, i));
10+
}
11+
return str.toString();
12+
}
13+
14+
private char shift(String s, int idx) {
15+
return (char) (s.charAt(idx - 1) + (s.charAt(idx) - '0'));
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Queue;
3+
4+
// https://leetcode.com/contest/biweekly-contest-51/problems/seat-reservation-manager/
5+
6+
public class SeatReservationManager {
7+
8+
class SeatManager {
9+
10+
private int count;
11+
private final Queue<Integer> queue;
12+
13+
public SeatManager(int n) {
14+
this.count = 1;
15+
this.queue = new PriorityQueue<>();
16+
}
17+
18+
public int reserve() {
19+
if (queue.isEmpty()) {
20+
return count++;
21+
}
22+
return queue.poll();
23+
}
24+
25+
public void unreserve(int seatNumber) {
26+
queue.offer(seatNumber);
27+
}
28+
}
29+
30+
/**
31+
* Your SeatManager object will be instantiated and called as such:
32+
* SeatManager obj = new SeatManager(n);
33+
* int param_1 = obj.reserve();
34+
* obj.unreserve(seatNumber);
35+
*/
36+
}

0 commit comments

Comments
 (0)