> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/sunset-views.md).

# Sunset Views

![](/files/8gSHR0otq3QOHFP5RLWg)

* 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;
```
