Skip to content

Commit

Permalink
feat(2023): add solution for challenge #17
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 17, 2023
1 parent 40a8880 commit 0baf54b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
31 changes: 31 additions & 0 deletions 2023/challenge-17/README.md
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`.
83 changes: 83 additions & 0 deletions 2023/challenge-17/challenge-17.test.ts
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,
})
})
20 changes: 20 additions & 0 deletions 2023/challenge-17/challenge-17.ts
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
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
| 14 | Avoid the alarm | 🟠 | [Show](https://adventjs.dev/challenges/2023/14) | [Go](./2023/challenge-14/challenge-14.ts) |
| 15 | Autonomous robot | 🟠 | [Show](https://adventjs.dev/challenges/2023/15) | [Go](./2023/challenge-15/challenge-15.ts) |
| 16 | Friday deployment | 🟢 | [Show](https://adventjs.dev/challenges/2023/16) | [Go](./2023/challenge-16/challenge-16.ts) |
| 17 | -- | -- | -- | -- |
| 17 | Optimizing the rental | 🟢 | [Show](https://adventjs.dev/challenges/2023/17) | [Go](./2023/challenge-17/challenge-17.ts) |
| 18 | -- | -- | -- | -- |
| 19 | -- | -- | -- | -- |
| 20 | -- | -- | -- | -- |
Expand Down

0 comments on commit 0baf54b

Please sign in to comment.