Skip to content

Commit 07a5817

Browse files
committed
Bench
1 parent 589ba1f commit 07a5817

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
5454

5555

56-
# Total: 475
56+
# Total: 476
5757

5858
| Easy | Medium | Hard | - |
5959
|:-------:|:-------:|:----:|:-:|
60-
| 127 | 266 | 77 | 5 |
60+
| 127 | 266 | 77 | 6 |
6161

6262

6363
| Question | Solution | Difficulty |
@@ -538,4 +538,4 @@
538538
| Insert into a Cyclic Sorted List | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/InsertIntoACyclicSortedList.java) | - |
539539
| Robot Room Cleaner | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/RobotRoomCleaner.java) | - |
540540
| AsyncJobMonitor | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/AsyncJobMonitor.java) | - |
541-
541+
| Bench | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/Bench.java) | - |

src/Bench.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* https://www.careercup.com/question?id=6204431937830912
3+
*/
4+
5+
import java.util.Arrays;
6+
import java.util.PriorityQueue;
7+
import java.util.Comparator;
8+
9+
public class Bench {
10+
public static void sitFarestFromPeople(boolean[] bench, int k) {
11+
if (bench == null || bench.length == 0 || k <= 0) return;
12+
Comparator<Range> comp = (r1, r2) -> Integer.compare(r2.right - r2.left, r1.right - r1.left);
13+
PriorityQueue<Range> pq = new PriorityQueue<>(1, comp);
14+
int L = bench.length;
15+
int pre = -1;
16+
for (int i=0; i<L; i++) {
17+
if (!bench[i]) continue;
18+
if (pre == -1) {
19+
pq.add(new Range(-i, i));
20+
} else if (pre + 1 < i) {
21+
pq.add(new Range(pre, i));
22+
}
23+
pre = i;
24+
}
25+
if (pre != -1) {
26+
pq.add(new Range(pre, (L-1-pre)*2+pre));
27+
} else {
28+
pq.add(new Range(-(L-1), L-1));
29+
}
30+
31+
for (int j=0; j<k; j++) {
32+
if (pq.isEmpty()) return;
33+
Range curr = pq.poll();
34+
int mid = (curr.right - curr.left) / 2 + curr.left;
35+
bench[mid] = true;
36+
if (curr.left >= 0 && curr.left + 1 < mid) {
37+
pq.add(new Range(curr.left, mid));
38+
}
39+
if (curr.right < L && mid + 1 < curr.right) {
40+
pq.add(new Range(mid, curr.right));
41+
}
42+
}
43+
}
44+
45+
static class Range {
46+
int left;
47+
int right;
48+
Range (int l, int r) {
49+
this.left = l;
50+
this.right = r;
51+
}
52+
}
53+
54+
55+
public static void main(String[] args) {
56+
boolean[] bench1 = new boolean[]{true, false, false, false, true, false, true};
57+
sitFarestFromPeople(bench1, 1);
58+
System.out.println(Arrays.toString(bench1));
59+
60+
boolean[] bench2 = new boolean[]{true, false, false, false, false, true, false, true};
61+
sitFarestFromPeople(bench2, 2);
62+
System.out.println(Arrays.toString(bench2));
63+
64+
boolean[] bench3 = new boolean[]{false, false, false, true, false, true};
65+
sitFarestFromPeople(bench3, 2);
66+
System.out.println(Arrays.toString(bench3));
67+
}
68+
69+
}

0 commit comments

Comments
 (0)