-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTandemBicycle.java
More file actions
31 lines (30 loc) · 816 Bytes
/
TandemBicycle.java
File metadata and controls
31 lines (30 loc) · 816 Bytes
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
import java.util.*;
class Program {
// O(nlog(n)) time | O(1) space - where n is the number of tandem bicycle
public int tandemBicycle(int[] redShirtSpeeds, int[] blueShirtSpeeds, boolean fastest) {
// Write your code here.
int n = redShirtSpeeds.length;
Arrays.sort(redShirtSpeeds);
Arrays.sort(blueShirtSpeeds);
if (fastest) {
int maxTotalSpeed = 0;
int l = n - 1, r = n - 1;
for (int i = 0; i < n; i++) {
if (redShirtSpeeds[l] >= blueShirtSpeeds[r]) {
maxTotalSpeed += redShirtSpeeds[l];
--l;
} else {
maxTotalSpeed += blueShirtSpeeds[r];
--r;
}
}
return maxTotalSpeed;
} else {
int minTotalSpeed = 0;
for (int i = 0; i < n; i++) {
minTotalSpeed += Math.max(redShirtSpeeds[i], blueShirtSpeeds[i]);
}
return minTotalSpeed;
}
}
}