-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): add solution for challenge #17
- Loading branch information
Showing
4 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Challenge #17: Optimizing the rental | ||
|
||
In Rovaniemi, Finland 🇫🇮, sleds 🛷 are rented by time intervals. **Each interval is represented as an array of two elements**, where the first element is the start of the rental and the second one is the end. | ||
|
||
For example, the array `[2, 7]` represents a rental that begins at hour `2` and ends at hour `7`. The problem is that sometimes the intervals overlap with each other, making it confusing to figure out from what time to what time the sled was rented. | ||
|
||
We're asked to, in order to simplify the task of calculating the total rental time, **write a function that merges all overlapping intervals** and **returns an array of sorted intervals**: | ||
|
||
```js | ||
optimizeIntervals([ | ||
[5, 8], | ||
[2, 7], | ||
[3, 4], | ||
]) // [[2, 8]] | ||
|
||
optimizeIntervals([ | ||
[1, 3], | ||
[8, 10], | ||
[2, 6], | ||
]) // [[1, 6], [8, 10]] | ||
|
||
optimizeIntervals([ | ||
[3, 4], | ||
[1, 2], | ||
[5, 6], | ||
]) // [[1, 2], [3, 4], [5, 6]] | ||
``` | ||
|
||
You can assume that **the first element of each interval is always less than or equal to the second element**. But **the intervals are not necessarily sorted.** | ||
|
||
The hour numbers can go up to the figure `9999`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe } from 'vitest' | ||
import { optimizeIntervals } from './challenge-17' | ||
|
||
const TEST_CASES: TestCases< | ||
Array<[number, number]>, | ||
Array<[number, number]> | ||
> = [ | ||
{ | ||
args: [ | ||
[2, 7], | ||
[3, 4], | ||
[5, 8], | ||
], | ||
expected: [[2, 8]], | ||
}, | ||
{ | ||
args: [ | ||
[3, 4], | ||
[5, 8], | ||
[2, 7], | ||
], | ||
expected: [[2, 8]], | ||
}, | ||
{ | ||
args: [ | ||
[1, 3], | ||
[2, 6], | ||
[8, 10], | ||
], | ||
expected: [ | ||
[1, 6], | ||
[8, 10], | ||
], | ||
}, | ||
{ | ||
args: [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
], | ||
expected: [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
], | ||
}, | ||
{ | ||
args: [ | ||
[5, 7], | ||
[6, 8], | ||
], | ||
expected: [[5, 8]], | ||
}, | ||
{ | ||
args: [ | ||
[1, 5], | ||
[6, 10], | ||
[11, 15], | ||
[16, 20], | ||
], | ||
expected: [ | ||
[1, 5], | ||
[6, 10], | ||
[11, 15], | ||
[16, 20], | ||
], | ||
}, | ||
{ | ||
args: [ | ||
[1, 15], | ||
[8, 12], | ||
[4, 7], | ||
], | ||
expected: [[1, 15]], | ||
}, | ||
] | ||
|
||
describe('Challenge #17: Optimizing the rental', () => { | ||
buildChallengeTestCases({ | ||
cases: TEST_CASES, | ||
fn: optimizeIntervals, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function optimizeIntervals(intervals: Array<[number, number]>) { | ||
intervals.sort((a, b) => a[0] - b[0]) | ||
|
||
const result = [intervals[0]] | ||
|
||
for (const currentInterval of intervals) { | ||
const lastMergedInterval = result[result.length - 1] | ||
|
||
if (currentInterval[0] <= lastMergedInterval[1]) { | ||
lastMergedInterval[1] = Math.max( | ||
lastMergedInterval[1], | ||
currentInterval[1], | ||
) | ||
} else { | ||
result.push(currentInterval) | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters