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:
class Solution { public int solution(int[] 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].
import java.util.ArrayList;
import java.util.List;
public class Solution {
public int solution(int[] H) {
int len = H.length;
Stack<Integer> stack = new Stack<>(len);
int blockCounter = 0;
for (int i = 0; i < len; ++i) {
int element = H[i];
if (stack.isEmpty()) {
stack.push(element);
++blockCounter;
} else {
if (stack.peek() > element) {
stack.push(element);
++blockCounter;
} else if (stack.peek() == element) {
stack.pop();
--blockCounter;
} else if (stack.peek() < element) {
stack.pop();
stack.push(element);
++blockCounter;
}
}
}
return blockCounter;
}
public static class Stack<T> {
public List<T> stack;
public Stack(int capacity) {
stack = new ArrayList<>(capacity);
}
public void push(T item) {
stack.add(item);
}
public T pop() {
T item = peek();
stack.remove(stack.size() - 1);
return item;
}
public T peek() {
int position = stack.size();
T item = stack.get(position - 1);
return item;
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
}
import java.util.ArrayList;
import java.util.List;
public class Solution {
public int solution(int[] H) {
int len = H.length;
Stack<Integer> stack = new Stack<>(len);
int blockCounter = 0;
for (int i = 0; i < len; ++i) {
int element = H[i];
if (stack.isEmpty()) {
stack.push(element);
++blockCounter;
} else {
if (stack.peek() > element) {
stack.push(element);
++blockCounter;
} else if (stack.peek() == element) {
continue;
} else if (stack.peek() < element) {
stack.pop();
stack.push(element);
++blockCounter;
}
}
}
return blockCounter;
}
public static class Stack<T> {
public List<T> stack;
public Stack(int capacity) {
stack = new ArrayList<>(capacity);
}
public void push(T item) {
stack.add(item);
}
public T pop() {
T item = peek();
stack.remove(stack.size() - 1);
return item;
}
public T peek() {
int position = stack.size();
T item = stack.get(position - 1);
return item;
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
}
import java.util.ArrayList;
import java.util.List;
public class Solution {
public int solution(int[] H) {
int len = H.length;
Stack<Integer> stack = new Stack<>(len);
int blockCounter = 0;
for (int i = 0; i < len; ++i) {
int element = H[i];
if (stack.isEmpty()) {
stack.push(element);
++blockCounter;
} else {
while (stack.peek() > element) {
stack.pop();
}
if (stack.peek() == element) {
continue;
} else {
stack.push(element);
++blockCounter;
}
}
}
return blockCounter;
}
public static class Stack<T> {
public List<T> stack;
public Stack(int capacity) {
stack = new ArrayList<>(capacity);
}
public void push(T item) {
stack.add(item);
}
public T pop() {
T item = peek();
stack.remove(stack.size() - 1);
return item;
}
public T peek() {
int position = stack.size();
T item = stack.get(position - 1);
return item;
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:418) at java.util.ArrayList.get(ArrayList.java:431) at Solution$Stack.peek(Solution.java:51) at Solution.solution(Solution.java:17) at wrapper.run(wrapper.java:40) at wrapper.main(wrapper.java:29)
import java.util.ArrayList;
import java.util.List;
public class Solution {
public int solution(int[] H) {
int len = H.length;
Stack<Integer> stack = new Stack<>(len);
int blockCounter = 0;
for (int i = 0; i < len; ++i) {
int element = H[i];
if (stack.isEmpty()) {
stack.push(element);
++blockCounter;
} else {
while (!stack.isEmpty() && stack.peek() > element) {
stack.pop();
}
if (!stack.isEmpty() && stack.peek() == element) {
continue;
} else {
stack.push(element);
++blockCounter;
}
}
}
return blockCounter;
}
public static class Stack<T> {
public List<T> stack;
public Stack(int capacity) {
stack = new ArrayList<>(capacity);
}
public void push(T item) {
stack.add(item);
}
public T pop() {
T item = peek();
stack.remove(stack.size() - 1);
return item;
}
public T peek() {
int position = stack.size();
T item = stack.get(position - 1);
return item;
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
}
import java.util.ArrayList;
import java.util.List;
public class Solution {
public int solution(int[] H) {
int len = H.length;
Stack<Integer> stack = new Stack<>(len);
int blockCounter = 0;
for (int i = 0; i < len; ++i) {
int element = H[i];
if (stack.isEmpty()) {
stack.push(element);
++blockCounter;
} else {
while (!stack.isEmpty() && stack.peek() > element) {
stack.pop();
}
if (!stack.isEmpty() && stack.peek() == element) {
continue;
} else {
stack.push(element);
++blockCounter;
}
}
}
return blockCounter;
}
public static class Stack<T> {
public List<T> stack;
public Stack(int capacity) {
stack = new ArrayList<>(capacity);
}
public void push(T item) {
stack.add(item);
}
public T pop() {
T item = peek();
stack.remove(stack.size() - 1);
return item;
}
public T peek() {
int position = stack.size();
T item = stack.get(position - 1);
return item;
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
}
import java.util.ArrayList;
import java.util.List;
public class Solution {
public int solution(int[] H) {
int len = H.length;
Stack<Integer> stack = new Stack<>(len);
int blockCounter = 0;
for (int i = 0; i < len; ++i) {
int element = H[i];
if (stack.isEmpty()) {
stack.push(element);
++blockCounter;
} else {
while (!stack.isEmpty() && stack.peek() > element) {
stack.pop();
}
if (!stack.isEmpty() && stack.peek() == element) {
continue;
} else {
stack.push(element);
++blockCounter;
}
}
}
return blockCounter;
}
public static class Stack<T> {
public List<T> stack;
public Stack(int capacity) {
stack = new ArrayList<>(capacity);
}
public void push(T item) {
stack.add(item);
}
public T pop() {
T item = peek();
stack.remove(stack.size() - 1);
return item;
}
public T peek() {
int position = stack.size();
T item = stack.get(position - 1);
return item;
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
}
The solution obtained perfect score.