Buildings With an Ocean View
Example
// input example: [6, 3, 4, 1, 2, 1] ~~sea~~
function checkIfSeaCanBeSeen(arr){
// TODOS
}
console.log(checkIfSeaCanBeSeen([6, 3, 4, 1, 2, 1]));
// [true, false, true, false, true, true]Answer
function checkIfSeaCanBeSeen(array) {
let high = array[array.length - 1];
let length = array.length - 2;
let newArray = [true];
for (let i = length; i >= 0; i--) {
if (array[i] > high) {
high = array[i];
newArray.unshift(true);
} else {
newArray.unshift(false);
}
}
return newArray;
}Last updated