1
+ /**
2
+ *
3
+ A tandem bicycle is a bicycle that's operated by two people: person A and
4
+ person B. Both people pedal the bicycle, but the person that pedals faster
5
+ dictates the speed of the bicycle. So if person A pedals at a speed of
6
+ 5, and person B pedals at a speed of 4, the tandem
7
+ bicycle moves at a speed of 5 (i.e.,
8
+ tandemSpeed = max(speedA, speedB)).
9
+
10
+
11
+ You're given two lists of positive integers: one that contains the speeds of
12
+ riders wearing red shirts and one that contains the speeds of riders wearing
13
+ blue shirts. Each rider is represented by a single positive integer, which is
14
+ the speed that they pedal a tandem bicycle at. Both lists have the same
15
+ length, meaning that there are as many red-shirt riders as there are
16
+ blue-shirt riders. Your goal is to pair every rider wearing a red shirt with a
17
+ rider wearing a blue shirt to operate a tandem bicycle.
18
+
19
+
20
+ Write a function that returns the maximum possible total speed or the minimum
21
+ possible total speed of all of the tandem bicycles being ridden based on an
22
+ input parameter, fastest. If fastest = true, your
23
+ function should return the maximum possible total speed; otherwise it should
24
+ return the minimum total speed.
25
+
26
+
27
+ "Total speed" is defined as the sum of the speeds of all the tandem bicycles
28
+ being ridden. For example, if there are 4 riders (2 red-shirt riders and 2
29
+ blue-shirt riders) who have speeds of 1, 3, 4, 5, and if they're
30
+ paired on tandem bicycles as follows: [1, 4], [5, 3], then the
31
+ total speed of these tandem bicycles is 4 + 5 = 9.
32
+
33
+ Sample Input
34
+ redShirtSpeeds = [5, 5, 3, 9, 2]
35
+ blueShirtSpeeds = [3, 6, 7, 2, 1]
36
+ fastest = true
37
+
38
+ Sample Output
39
+ 32
40
+
41
+ } listOne
42
+ * @param {* } listTwo
43
+ * @param {* } fastest
44
+ * @returns
45
+ */
46
+ function makeTandemBicycle ( listOne , listTwo , fastest ) {
47
+
48
+ if ( fastest ) {
49
+ listOne . sort ( ( a , b ) =>
50
+ a < b
51
+ ? 1
52
+ : a === b
53
+ ? 0
54
+ : - 1
55
+ ) ;
56
+ } else {
57
+ listOne . sort ( ( a , b ) => a - b ) ;
58
+ }
59
+
60
+ listTwo . sort ( ( a , b ) => a - b ) ;
61
+
62
+ let sum = 0 ;
63
+
64
+ for ( let i = 0 ; i < listOne . length ; i ++ ) {
65
+ sum += Math . max ( listOne [ i ] , listTwo [ i ] ) ;
66
+ }
67
+
68
+ return sum ;
69
+ }
70
+
71
+ module . exports = {
72
+ makeTandemBicycle
73
+ }
0 commit comments