# Sunset Views

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-c13fdd691000f8857d34b7812ec475e37fa7656f%2FScreenshot%202023-02-04%20at%2017.08.34.png?alt=media)

* n, n

```tsx
const EAST = 'EAST';
const WEST = 'WEST';

function sunsetViews(buildings, direction) {

  if (direction === EAST) {
    buildings.reverse();
  }

  let maxHeight = -Infinity;

  let answer = [];
  for (let i = 0; i < buildings.length; i++) {
    const curr = buildings[i];
    if (maxHeight >= curr) continue;
    
    maxHeight = curr;
    answer.push(i);
  }

  if (direction === EAST) {
    answer = answer.map(idx => buildings.length - 1 - idx).reverse();
  }
  
  return answer;
}

// Do not edit the line below.
exports.sunsetViews = sunsetViews;
```
