Skip to content

Commit c6370fa

Browse files
committed
adding tandem bicycle and changed flight travels
1 parent 446a40e commit c6370fa

File tree

5 files changed

+170
-17
lines changed

5 files changed

+170
-17
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
1. [Word Search](/word-search/index.test.js)
104104
1. [Product Array Except Self](/product-array-except-self/index.test.js)
105105
1. [Best time to Buy and Sell](/best-time-to-buy-and-sell/index.test.js)
106+
1. [Tandem Bicycle](/tandem-bicycle/index.test.js)
106107

107108
## Setup
108109

flight-travels/index.js

+42-7
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
* @returns {number[]}
88
*/
99
const getTwoFlightIndex = (flights = [], maxDistanceFlyAllowed) => {
10-
11-
if ((flights.length < 2) || maxDistanceFlyAllowed < 1) {
12-
throw Error('Given flags could not be less than two flights, and maxDistanceAllowed should be higher than zero');
10+
if (flights.length < 2 || maxDistanceFlyAllowed < 1) {
11+
throw Error(
12+
'Given flags could not be less than two flights, and maxDistanceAllowed should be higher than zero'
13+
);
1314
}
1415

1516
let indexA = 0;
1617
let indexB = 1;
1718
let maxDistancePossible = Number.MIN_VALUE; // to be sure to be override first time
18-
19+
1920
// O(n 2) time complexity
2021
// O(1) space time
2122
for (let i = 0; i < flights.length - 1; i++) {
22-
for(let j = i + 1; j < flights.length; j++) {
23+
for (let j = i + 1; j < flights.length; j++) {
2324
const currentFlySum = flights[i] + flights[j];
2425
if (currentFlySum === maxDistanceFlyAllowed) {
2526
return [i, j];
26-
} else if (currentFlySum < maxDistanceFlyAllowed && currentFlySum > maxDistancePossible) {
27+
} else if (
28+
currentFlySum < maxDistanceFlyAllowed &&
29+
currentFlySum > maxDistancePossible
30+
) {
2731
indexA = i;
2832
indexB = j;
2933
maxDistancePossible = currentFlySum;
@@ -33,6 +37,37 @@ const getTwoFlightIndex = (flights = [], maxDistanceFlyAllowed) => {
3337
return [indexA, indexB];
3438
};
3539

40+
const getTwoFlightIndexPerformance = (flights = [], maxDistanceFlyAllowed) => {
41+
flights.sort((a, b) => a - b); // O(n log n)
42+
// CONTINUE..........
43+
let leftIndex = 0;
44+
let rightIndex = flights.length - 1;
45+
let minValue = Math.MAX_VALUE;
46+
let maxValue = Math.MIN_VALUE;
47+
48+
while (leftIndex < rightIndex) {
49+
const currentSum = flights[leftIndex] + flights[rightIndex];
50+
if (currentSum === maxDistanceFlyAllowed) {
51+
return [leftIndex, rightIndex];
52+
} else if (currentSum > maxDistanceFlyAllowed) {
53+
rightIndex--;
54+
if (flights[leftIndex] < minValue) {
55+
minValue = flights[leftIndex];
56+
minValueIndex = leftIndex;
57+
}
58+
} else {
59+
leftIndex++;
60+
61+
if (flights[rightIndex] > maxValue) {
62+
maxValue = flights[rightIndex]
63+
}
64+
}
65+
}
66+
67+
return [leftIndex, rightIndex];
68+
};
69+
3670
module.exports = {
37-
getTwoFlightIndex
71+
getTwoFlightIndex,
72+
getTwoFlightIndexPerformance
3873
};

flight-travels/index.test.js

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
const {getTwoFlightIndex} = require('.');
1+
const { getTwoFlightIndex, getTwoFlightIndexPerformance } = require('.');
22

33
test('Flow travels should return the longest flight possibles', () => {
44
// Arrange
55
const flights = [10, 20, 5, 1, 40];
66
const maxAirplaneDistance = 16;
7-
7+
88
// Act
99
const flightsResults = getTwoFlightIndex(flights, maxAirplaneDistance);
10-
10+
11+
// Assert
12+
expect(flightsResults.length).toEqual(2);
13+
expect(flightsResults[0]).toEqual(0);
14+
expect(flightsResults[1]).toEqual(2);
15+
});
16+
17+
test('Flow travels should return the longest flight possibles', () => {
18+
// Arrange
19+
const flights = [10, 20, 5, 1, 40];
20+
const maxAirplaneDistance = 16;
21+
22+
// Act
23+
const flightsResults = getTwoFlightIndexPerformance(
24+
flights,
25+
maxAirplaneDistance
26+
);
27+
1128
// Assert
1229
expect(flightsResults.length).toEqual(2);
1330
expect(flightsResults[0]).toEqual(0);
@@ -18,26 +35,30 @@ test('Flight travels should not be less than 2 flight', () => {
1835
// Arrange
1936
const flights = [];
2037
const maxAirplaneDistance = 16;
21-
38+
2239
// Act
2340
function shouldThrowError() {
2441
getTwoFlightIndex(flights, maxAirplaneDistance);
2542
}
26-
43+
2744
// Assert
28-
expect(shouldThrowError.bind(null, flights, maxAirplaneDistance)).toThrowError(/Given flags could not be less than two flights/i)
45+
expect(
46+
shouldThrowError.bind(null, flights, maxAirplaneDistance)
47+
).toThrowError(/Given flags could not be less than two flights/i);
2948
});
3049

3150
test('Flight travels should not be less distance than 1', () => {
3251
// Arrange
3352
const flights = [10, 20, 30, 40];
3453
const maxAirplaneDistance = 0;
35-
54+
3655
// Act
3756
function shouldThrowError() {
3857
getTwoFlightIndex(flights, maxAirplaneDistance);
3958
}
40-
59+
4160
// Assert
42-
expect(shouldThrowError.bind(null, flights, maxAirplaneDistance)).toThrowError(/Given flags could not be less than two flights/i)
43-
});
61+
expect(
62+
shouldThrowError.bind(null, flights, maxAirplaneDistance)
63+
).toThrowError(/Given flags could not be less than two flights/i);
64+
});

tandem-bicycle/index.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

tandem-bicycle/index.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { makeTandemBicycle } = require('.');
2+
3+
describe('tandem bicycle', () => {
4+
it('should return fastest', () => {
5+
const redShirtSpeeds = [5, 5, 3, 9, 2];
6+
const blueShirtSpeeds = [3, 6, 7, 2, 1];
7+
const isFastest = true;
8+
9+
const result = makeTandemBicycle(redShirtSpeeds, blueShirtSpeeds, isFastest);
10+
11+
expect(result).toEqual(32);
12+
});
13+
14+
it('should return slowest', () => {
15+
const redShirtSpeeds = [5, 5, 3, 9, 2];
16+
const blueShirtSpeeds = [3, 6, 7, 2, 1];
17+
const isFastest = false;
18+
19+
const result = makeTandemBicycle(redShirtSpeeds, blueShirtSpeeds, isFastest);
20+
21+
expect(result).toEqual(25);
22+
});
23+
});

0 commit comments

Comments
 (0)