There are N rectangular buildings standing along the road next to each other. The K-th building is of size H[K] × 1.
Because a renovation of all of the buildings is planned, we want to cover them with rectangular banners until the renovations are finished. Of course, to cover a building, the banner has to be at least as high as the building. We can cover more than one building with a banner if it is wider than 1.
For example, to cover buildings of heights 3, 1, 4 we could use a banner of size 4×3 (i.e. of height 4 and width 3), marked here in blue:
We can order at most two banners and we want to cover all of the buildings. Also, we want to minimize the amount of material needed to produce the banners.
What is the minimum total area of at most two banners which cover all of the buildings?
Write a function:
function solution(H);
that, given an array H consisting of N integers, returns the minimum total area of at most two banners that we will have to order.
Examples:
1. Given H = [3, 1, 4], the function should return 10. The result can be achieved by covering the first two buildings with a banner of size 3×2 and the third building with a banner of size 4×1:
2. Given H = [5, 3, 2, 4], the function should return 17. The result can be achieved by covering the first building with a banner of size 5×1 and the other buildings with a banner of size 4×3:
3. Given H = [5, 3, 5, 2, 1], your function should return 19. The result can be achieved by covering the first three buildings with a banner of size 5×3 and the other two with a banner of size 2×2:
4. Given H = [7, 7, 3, 7, 7], your function should return 35. The result can be achieved by using one banner of size 7×5:
5. Given H = [1, 1, 7, 6, 6, 6], your function should return 30. The result can be achieved by using banners of size 1×2 and 7×4:
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array H is an integer within the range [1..10,000].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
return H
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
return H.length *
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
for (let i = H.length-1; i > H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
for (let i = H.length-1; i >= 0; i--) {
forward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
for (let i = H.length-2; i >= 0; i--) {
forward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-1-i] =
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-1-i] = H[]
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-1-i] = H[H.length-1]
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-1-i] = H[H.length-1] * (i + 1)
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-2] * (i + 1)
}
for (let i = H.length-2; i >= 0; i--) {
backward[i] = H[i] * (i + 1);
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-2] * (i + 1)
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1)
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1)
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1)
}
for (let i = 0; i < forward.length; i++)
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1)
}
for (let i = 0; i < forward.length; i++) {
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1)
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas[]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas[]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas[]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
let minArea =
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas[]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
let minArea = 0;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas[]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
let minArea;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
areas[i]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
let minArea;
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
for (let i = 0; i < areas.length; i++) {
if (areas[i]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
for (let i = 0; i < areas.length; i++) {
if (areas[i]
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea
}
}
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea[])
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < )
return H.length * max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
}
return ;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
function result: 26
function result: 1
function result: 23
function result: 10
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
if (max < H[i]) {
max = H[i];
}
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
[ 3, 2 ] [ 8, 4 ] [ 11, 6 ]
[ 5, 6, 6 ] [ 12, 8, 4 ] [ 17, 14, 10 ]
[ 5, 6, 15, 8 ] [ 4, 3, 2, 1 ] [ 9, 9, 17, 9 ]
[ 7, 14, 9, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 23, 35 ]
[ 1, 2, 21, 24, 30 ] [ 30, 24, 18, 12, 6 ] [ 31, 26, 39, 36, 36 ]
function result: 26
[ 1, 2, 21, 24, 30 ] [ 30, 24, 18, 12, 6 ] [ 31, 26, 39, 36, 36 ]
function result: 1
[] [] []
function result: 23
[ 7, 14, 9, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 23, 35 ]
function result: 10
[ 5, 6, 6 ] [ 12, 8, 4 ] [ 17, 14, 10 ]
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = 1;
let areas = [];
for (let i = 0; i < H.length-1; i++) {
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = H[0];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (max < H[i]) {
max = H[i];
}
forward[i] = H[i] * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = H[0];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (max < H[i]) {
max = H[i];
}
forward[i] = max * (i + 1);
backward[H.length-2-i] = H[H.length-1] * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let max = H[0];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (max < H[i]) {
max = H[i];
}
forward[i] = max * (i + 1);
backward[H.length-2-i] = max * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[]
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (max < H[i]) {
max = H[i];
}
forward[i] = max * (i + 1);
backward[H.length-2-i] = max * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
forward[i] = max * (i + 1);
backward[H.length-2-i] = max * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxForward < H[i]) {
maxForward = H[i];
}
forward[i] = max * (i + 1);
backward[H.length-2-i] = max * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[i]) {
maxBackward = H[i];
}
forward[i] = max * (i + 1);
backward[H.length-2-i] = max * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[i]) {
maxBackward = H[i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-]) {
maxBackward = H[i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 3, 6 ] [ 8, 4 ] [ 11, 10 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 5, 10, 15 ] [ 12, 8, 4 ] [ 17, 18, 19 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 5, 10, 15, 20 ] [ 20, 15, 4, 1 ] [ 25, 25, 19, 21 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 7, 14, 21, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 35, 35 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 1, 2, 21, 28, 35 ] [ 35, 28, 18, 12, 6 ] [ 36, 30, 39, 40, 41 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 1, 2, 21, 28, 35 ] [ 35, 28, 18, 12, 6 ] [ 36, 30, 39, 40, 41 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[] [] []
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 7, 14, 21, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 35, 35 ]
ReferenceError: max is not defined at solution (solution.js:34:30) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
[ 5, 10, 15 ] [ 12, 8, 4 ] [ 17, 18, 19 ]
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * maxForw;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * maxForward;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
[ 3, 6 ] [ 8, 4 ] [ 11, 10 ]
[ 5, 10, 15 ] [ 12, 8, 4 ] [ 17, 18, 19 ]
[ 5, 10, 15, 20 ] [ 20, 15, 4, 1 ] [ 25, 25, 19, 21 ]
[ 7, 14, 21, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 35, 35 ]
[ 1, 2, 21, 28, 35 ] [ 35, 28, 18, 12, 6 ] [ 36, 30, 39, 40, 41 ]
function result: 30
[ 1, 2, 21, 28, 35 ] [ 35, 28, 18, 12, 6 ] [ 36, 30, 39, 40, 41 ]
function result: 2
[] [] []
function result: 35
[ 7, 14, 21, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 35, 35 ]
function result: 17
[ 5, 10, 15 ] [ 12, 8, 4 ] [ 17, 18, 19 ]
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * maxForward;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * maxForward;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
console.log(minArea)
console.log(maxArea)
console.log(maxForwar)
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * maxForward;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
console.log(minArea)
console.log(maxArea)
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let minArea = areas[0];
let maxArea = H.length * maxForward;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let max = maxForward > maxBackward ? maxForward : maxBackwar
let minArea = areas[0];
let maxArea = H.length * maxForward;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
console.log(forward)
console.log(backward)
console.log(areas)
let max = maxForward > maxBackward ? maxForward : maxBackward;
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
[ 3, 6 ] [ 8, 4 ] [ 11, 10 ]
[ 5, 10, 15 ] [ 12, 8, 4 ] [ 17, 18, 19 ]
[ 5, 10, 15, 20 ] [ 20, 15, 4, 1 ] [ 25, 25, 19, 21 ]
[ 7, 14, 21, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 35, 35 ]
[ 1, 2, 21, 28, 35 ] [ 35, 28, 18, 12, 6 ] [ 36, 30, 39, 40, 41 ]
function result: 30
[ 1, 2, 21, 28, 35 ] [ 35, 28, 18, 12, 6 ] [ 36, 30, 39, 40, 41 ]
function result: 2
[] [] []
function result: 35
[ 7, 14, 21, 28 ] [ 28, 21, 14, 7 ] [ 35, 35, 35, 35 ]
function result: 17
[ 5, 10, 15 ] [ 12, 8, 4 ] [ 17, 18, 19 ]
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let max = maxForward > maxBackward ? maxForward : maxBackward;
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let max = maxForward > maxBackward ? maxForward : maxBackward;
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
function result: 30
function result: 2
function result: 35
function result: 17
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let max = maxForward > maxBackward ? maxForward : maxBackward;
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
function result: 30
function result: 2
function result: 35
function result: 17
[1, 1, 7, 6, 6, 6]
[2]
[7, 7, 3, 7, 7]
[5, 3, 2, 4]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 8.9.4)
let forward = [];
let backward = [];
let maxForward = H[0];
let maxBackward = H[H.length-1];
let areas = [];
for (let i = 0; i < H.length-1; i++) {
if (maxForward < H[i]) {
maxForward = H[i];
}
if (maxBackward < H[H.length-1-i]) {
maxBackward = H[H.length-1-i];
}
forward[i] = maxForward * (i + 1);
backward[H.length-2-i] = maxBackward * (i + 1);
}
for (let i = 0; i < forward.length; i++) {
areas[i] = forward[i] + backward[i];
}
let max = maxForward > maxBackward ? maxForward : maxBackward;
let minArea = areas[0];
let maxArea = H.length * max;
for (let i = 0; i < areas.length; i++) {
if (areas[i] < minArea) {
minArea = areas[i];
}
}
if (minArea < maxArea) {
return minArea;
} else {
return maxArea;
}
}
The solution obtained perfect score.
Values in the tests are arranged into ascending or descending pyramids. N=100,000.