You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by an array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall's left end and H[N−1] is the height of the wall's right end.
The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular). Your task is to compute the minimum number of blocks needed to build the wall.
Write a function:
function solution(H);
that, given an array H of N positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it.
For example, given array H containing N = 9 integers:
H[0] = 8 H[1] = 8 H[2] = 5 H[3] = 7 H[4] = 9 H[5] = 8 H[6] = 7 H[7] = 4 H[8] = 8the function should return 7. The figure shows one possible arrangement of seven blocks.
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..1,000,000,000].
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else{
var height = 0
while(stack.length > 0){
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else{
var height = 0
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else{
var height = H[i-1]
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(){
var height = H[i-1]
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(H[i]){
var height = H[i-1]
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1]){
var height = H[i-1]
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
while(stack.length > 0){
var wall = stack.pop()
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
while(stack.length > 0){
var wall = stack.pop()
if(H[i-1]-wall)
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height =
while(stack.length > 0){
var wall = stack.pop()
if(H[i-1]-wall)
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
var wall = stack.pop()
if(H[i-1]-wall)
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(H[i-1]-wall)
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return stack
}
function solution(H) {
var stack = []
var curr = 0
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}
if(curr < H[i]){
stack.push
}else if(curr > H[i]){
}
current_height = H[i]
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(i > 0 && H[i-1] > H[i]){
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(H[i-1] < H[i]){
stack.push(H[i])
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
}else if(){
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log("add")
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log("add")
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
stack.push()
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(stack)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(stack)
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
[ 8 ] [] [ 5, 2, 2 ] [] [ 8 ] [] [ 7 ] []
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(stack)
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(stack)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(stack)
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
console.log(stack)
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
console.log(stack)
}
}
return total
}
[ 8 ] [ 8 ] [] [ 5, 2 ] [ 5, 2, 2 ] [ 5, 2, 2 ] [] [ 8 ] [] [ 7 ] [] [ 4, 4 ]
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(stack, )
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(stack)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(stack)
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
console.log(stack)
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
console.log(stack)
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(stack, total)
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(stack, total)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(stack, total)
if(stack.length == 0){
total++
stack.push(H[i])
console.log(stack, total)
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
console.log(stack, total)
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
console.log(stack, total)
}
}
return total
}
[ 8 ] 1 [ 8 ] 1 [] 1 [ 5 ] 2 [ 5, 2 ] 3 [ 5, 2, 2 ] 4 [ 5, 2, 2 ] 4 [] 4 [ 8 ] 5 [ 8 ] 5 [] 5 [ 7 ] 6 [ 7 ] 6 [] 6 [ 4 ] 7 [ 4, 4 ] 8
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(i,stack, total)
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(stack, total)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(stack, total)
if(stack.length == 0){
total++
stack.push(H[i])
console.log(stack, total)
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
console.log(stack, total)
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
console.log(stack, total)
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(i,stack, total)
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(i,stack, total)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(i,stack, total)
if(stack.length == 0){
total++
stack.push(H[i])
console.log(i,stack, total)
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
console.log(i,stack, total)
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
console.log(i,stack, total)
}
}
return total
}
0 [ 8 ] 1 2 [ 8 ] 1 2 [] 1 2 [ 5 ] 2 3 [ 5, 2 ] 3 4 [ 5, 2, 2 ] 4 5 [ 5, 2, 2 ] 4 5 [] 4 5 [ 8 ] 5 6 [ 8 ] 5 6 [] 5 6 [ 7 ] 6 7 [ 7 ] 6 7 [] 6 7 [ 4 ] 7 8 [ 4, 4 ] 8
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(i,stack, total)
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
console.log(i,stack, total)
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
console.log(i,stack, total)
if(stack.length == 0){
total++
stack.push(H[i])
console.log(i,stack, total)
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
console.log(i,stack, total)
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
console.log(i,stack, total)
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
0 [ 8 ] 1 0 [ 8 ] 1 1 [ 8 ] 1 2 [ 5 ] 2 3 [ 5, 2 ] 3 4 [ 5, 2, 2 ] 4 5 [ 8 ] 5 6 [ 7 ] 6 7 [ 4 ] 7 8 [ 4, 4 ] 8
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height == H[i]) break
}
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
0 [ 8 ] 1 1 [ 8 ] 1 2 [ 5 ] 2 3 [ 5, 2 ] 3 4 [ 5, 2, 2 ] 4 5 [ 8 ] 5 6 [ 7 ] 6 7 [ 4 ] 7 8 [ 4, 4 ] 8
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) break
}
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height <= H[i]) break
}
if(stack.length == 0){
total++
stack.push(H[i])
}else
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height <= H[i]) {
break
}
}
if(stack.length == 0){
total++
stack.push(H[i])
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height <= H[i]) {
total++
stack.push(H[i])
break
}
}
if(stack.length == 0){
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height <= H[i]) {
total++
stack.push(H[i]-height)
break
}
}
if(stack.length == 0){
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height <= H[i]) {
total++
stack.push(H[i]-height)
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
0 [ 8 ] 1 1 [ 8 ] 1 2 [ 5 ] 2 3 [ 5, 2 ] 3 4 [ 5, 2, 2 ] 4 5 [ 5, 2, 1 ] 5 6 [ 5, 2, 0 ] 6 7 [ 4 ] 7 8 [ 4, 4 ] 8
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
0 [ 8 ] 1 1 [ 8 ] 1 2 [ 5 ] 2 3 [ 5, 2 ] 3 4 [ 5, 2, 2 ] 4 5 [ 5, 2, 1 ] 5 6 [ 5, 2 ] 6 7 [ 4 ] 7 8 [ 4, 4 ] 8
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height <= H[i]) {
total++
stack.push(H[i]-height)
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
break
}else if(){
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
}else if(height == H[i]){
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
0 [ 8 ] 1 1 [ 8 ] 1
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
}else if(height = H[i]){
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
break
}else if(height == H[i]){
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
console.log(i,stack, total)
}
return total
}
0 [ 8 ] 1 1 [ 8 ] 1 2 [ 5 ] 2 3 [ 5, 2 ] 3 4 [ 5, 2, 2 ] 4 5 [ 5, 2, 1 ] 5 6 [ 5, 2 ] 5 7 [ 4 ] 6 8 [ 4, 4 ] 7
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
break
}else if(height == H[i]){
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
//console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
break
}else if(height == H[i]){
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
//console.log(i,stack, total)
}
return total
}
function solution(H) {
var stack = []
var total = 0
for(var i = 0;i<H.length;i++){
if(stack.length == 0){
stack.push(H[i])
total++
}else if(i > 0 && H[i-1] > H[i]){
var height = H[i-1]
while(stack.length > 0){
height = height - stack.pop()
if(height < H[i]) {
total++
stack.push(H[i]-height)
break
}else if(height == H[i]){
break
}
}
}else if(i > 0 && H[i-1] < H[i]){
stack.push(H[i]-H[i-1])
total++
}else if(i == 0 && H[i-1] < H[i]){
stack.push(H[i])
total++
}
//console.log(i,stack, total)
}
return total
}
The solution obtained perfect score.