Skip to content

Commit

Permalink
question: seeBuildingsLeft
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelriosoliveira committed Nov 11, 2024
1 parent 6c92824 commit 3a63dd1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ I started this in the [issue #176](https://buttondown.email/cassidoo/archive/we-
- [361 - increasingSubsequence](src/2024/361-increasingSubsequence)
- [368 - maxPairs](src/2024/368-maxPairs)
- [377 - groupAnagrams](src/2024/377-groupAnagrams)
- [378 - seeBuildingsLeft](src/2024/378-seeBuildingsLeft)
</details>

## Installing & Running
Expand Down
24 changes: 24 additions & 0 deletions src/2024/378-seeBuildingsLeft/README.md
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!
39 changes: 39 additions & 0 deletions src/2024/378-seeBuildingsLeft/seeBuildingsLeft.test.ts
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);
});
});
27 changes: 27 additions & 0 deletions src/2024/378-seeBuildingsLeft/seeBuildingsLeft.ts
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;
}

0 comments on commit 3a63dd1

Please sign in to comment.