Good afternoon! Here's our prompt for today.
We're given a histogram like the following, where contiguous (sharing a common border or touching) bars are made up of different heights. Let's assume all bars have the same width.

The histogram is converted to an array of the heights of the bars:
JAVASCRIPT
1const histArr = [3, 1, 4, 2, 2, 1]
With this knowledge, can we find the largest rectangular area within the histogram? For the above, it would be the area outlined below:

With the above histogram, calling maxRectInHist(histArr)
would get us 6
. Can you fill in the method?
Constraints
- Length of the array <=
100000
- The array can be empty
- The array will contain values between
1
and10000
- The final answer will always fit in the integer range
- Expected time complexity :
O(n)
- Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
35
var assert = require('assert');
​
const histArr = [3, 1, 4, 2, 2, 1];
​
function maxRectInHist(histArr) {
// Fill in this method
return histArr;
}
​
console.log(maxRectInHist(histArr));
​
try {
assert.equal(maxRectInHist([3, 1, 4, 2, 2, 1]), 6);
​
console.log('PASSED: ' + 'maxRectInHist([3, 1, 4, 2, 2, 1])');
} catch (err) {
console.log(err);
}
​
try {
assert.equal(maxRectInHist([]), 0);
​
console.log('PASSED: ' + 'maxRectInHist([])');
} catch (err) {
console.log(err);
}
​
try {
assert.equal(maxRectInHist([1, 2, 3, 4]), 6);
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
We'll now take you through what you need to know.
How do I use this guide?