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:
object Solution { def solution(h: Array[Int]): Int }
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].
Solution.scala:9: error: not found: value mutable val st = new mutable.Stack() ^ Solution.scala:12: error: type mismatch; found : Unit required: Int } ^ two errors found
import scala.collection.JavaConversions._
import scala.collection.mutable
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(H: Array[Int]): Int = {
val st = new mutable.Stack()
7
// write your code in Scala 2.10
}
}
Solution.scala:9: error: not found: value mutable val st = new mutable.Stack() ^ one error found
import scala.collection.JavaConversions._
import scala.collection.mutable
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(H: Array[Int]): Int = {
val st = new Stack()
7
// write your code in Scala 2.10
}
}
Solution.scala:10: error: not found: type Stack val st = new Stack() ^ one error found
import scala.collection.JavaConversions._
import scala.collection.mutable
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(H: Array[Int]): Int = {
val stack = new mutable.Stack[Int]()
var blockCounter :Int = 0
for (i <- 0 to (H.length-1)) {
var element = H(i)
if (stack.isEmpty) {
stack.push(element)
blockCounter = blockCounter+1
} else {
while (!stack.isEmpty && stack.top > element) {
stack.pop()
}
if (!stack.isEmpty && stack.top == element) {
} else {
stack.push(element)
blockCounter = blockCounter+1
}
}
}
return blockCounter
}
}
import scala.collection.JavaConversions._
import scala.collection.mutable
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(H: Array[Int]): Int = {
val stack = new mutable.Stack[Int]()
var blockCounter :Int = 0
for (i <- 0 to (H.length-1)) {
var element = H(i)
if (stack.isEmpty) {
stack.push(element)
blockCounter = blockCounter+1
} else {
while (!stack.isEmpty && stack.top > element) {
stack.pop()
}
if (!stack.isEmpty && stack.top == element) {
} else {
stack.push(element)
blockCounter = blockCounter+1
}
}
}
return blockCounter
}
}
import scala.collection.JavaConversions._
import scala.collection.mutable
// you can write to stdout for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(H: Array[Int]): Int = {
val stack = new mutable.Stack[Int]()
var blockCounter :Int = 0
for (i <- 0 to (H.length-1)) {
var element = H(i)
if (stack.isEmpty) {
stack.push(element)
blockCounter = blockCounter+1
} else {
while (!stack.isEmpty && stack.top > element) {
stack.pop()
}
if (!stack.isEmpty && stack.top == element) {
} else {
stack.push(element)
blockCounter = blockCounter+1
}
}
}
return blockCounter
}
}
The solution obtained perfect score.