-
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.
- Loading branch information
1 parent
6c92824
commit 3a63dd1
Showing
4 changed files
with
91 additions
and
0 deletions.
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
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,24 @@ | ||
# seeBuildingsLeft | ||
|
||
Interview question of the [issue #378 of rendezvous with cassidoo](https://buttondown.com/cassidoo/archive/stand-for-something-or-you-will-fall-for-anything/). | ||
|
||
## The Question | ||
|
||
**Given a list of integers representing the heights of buildings, return the maximum number of buildings that can be seen when looking from the left. A building can see another building if it is taller than all the buildings to its left.** The height of the tallest building is included in the count. | ||
|
||
Example: | ||
|
||
```js | ||
seeBuildingsLeft([1,2,3,4,5]) | ||
5 | ||
|
||
seeBuildingsLeft([5,4,3,2,1]) | ||
1 | ||
|
||
seeBuildingsLeft([3,7,8,3,6,1]) | ||
3 | ||
``` | ||
|
||
## Installing & Running | ||
|
||
Just `pnpm i` to install all dependencies and then `pnpm t` to run the tests! |
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,39 @@ | ||
import { seeBuildingsLeft } from './seeBuildingsLeft'; | ||
|
||
describe('#seeBuildingsLeft', () => { | ||
it('returns the correct count for a strictly increasing list', () => { | ||
expect(seeBuildingsLeft([1, 2, 3, 4, 5])).toBe(5); | ||
}); | ||
|
||
it('returns the correct count for a strictly decreasing list', () => { | ||
expect(seeBuildingsLeft([5, 4, 3, 2, 1])).toBe(1); | ||
}); | ||
|
||
it('returns the correct count for a list with mixed values', () => { | ||
expect(seeBuildingsLeft([3, 7, 8, 3, 6, 1])).toBe(3); | ||
}); | ||
|
||
it('returns 1 for a list with all buildings of the same height', () => { | ||
expect(seeBuildingsLeft([5, 5, 5, 5, 5])).toBe(1); | ||
}); | ||
|
||
it('returns the correct count for a list with one building', () => { | ||
expect(seeBuildingsLeft([10])).toBe(1); | ||
}); | ||
|
||
it('returns the correct count for a list with some buildings taller after shorter ones', () => { | ||
expect(seeBuildingsLeft([4, 3, 7, 6, 9, 2])).toBe(3); | ||
}); | ||
|
||
it('returns the correct count when the tallest building is at the end of the list', () => { | ||
expect(seeBuildingsLeft([1, 2, 3, 4, 10])).toBe(5); | ||
}); | ||
|
||
it('returns the correct count for an empty list', () => { | ||
expect(seeBuildingsLeft([])).toBe(0); | ||
}); | ||
|
||
it('returns the correct count when there are zeros in the heights', () => { | ||
expect(seeBuildingsLeft([0, 2, 0, 4, 6, 0, 8])).toBe(4); | ||
}); | ||
}); |
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,27 @@ | ||
/* | ||
Given a list of integers representing the heights of buildings, | ||
return the maximum number of buildings that can be seen when looking from the left. | ||
A building can see another building if it is taller than all the buildings to its left. | ||
The height of the tallest building is included in the count. | ||
Examples: | ||
seeBuildingsLeft([1,2,3,4,5]) | ||
5 | ||
seeBuildingsLeft([5,4,3,2,1]) | ||
1 | ||
seeBuildingsLeft([3,7,8,3,6,1]) | ||
3 | ||
*/ | ||
|
||
export function seeBuildingsLeft(heights: number[]): number { | ||
return heights.reduce( | ||
({ count, maxHeight }, height) => ({ | ||
count: count + Number(height > maxHeight), | ||
maxHeight: Math.max(height, maxHeight), | ||
}), | ||
{ count: 0, maxHeight: 0 }, | ||
).count; | ||
} |