Sitemap
1 min readSep 27, 2020

Woww it's an excellent explanation, thanks for sharing!!

What do you think about this other example?

```

// Time complexity => O(n)

arr.push(0)

const n = arr.length

const windowSizes = new Array(arr.length).fill(0)

const positions = []

let i = 0

while (i < n) {

if (!positions.length || arr[positions[positions.length-1]] <= arr[i]) {

positions.push(i++)

} else {

const top = positions.pop()

// Where range represents every window size

const range = positions.length ? (i - positions[positions.length-1] - 1) : i

if (range < 1 || range > arr.length || arr[top] === 0) continue

windowSizes[range-1] = Math.max(windowSizes[range-1], arr[top])

}

}

for (let i = n - 2; i >= 0; i--) {

windowSizes[i] = Math.max(windowSizes[i], windowSizes[i+1])

}

windowSizes.pop()

return windowSizes

```

I think it's easy to understand If you're familiar with the Largest Rectangle problem of HackerRank :)

--