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].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int blocks = H.Length;
(for int=i; i<H.Length-1; I++)
if (H[i] == H[i+1]) blocks--;
}
}
Compilation failed: 4 error(s), 0 warnings Solution.cs(13,9): error CS1525: Unexpected symbol `for' Solution.cs(13,13): error CS1525: Unexpected symbol `int' Solution.cs(13,16): error CS1525: Unexpected symbol `=' Solution.cs(13,37): error CS1525: Unexpected symbol `)', expecting `;' or `}'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int blocks = H.Length;
for (int=i; i<H.Length-1; I++)
{ if (H[i] == H[i+1]) blocks--; }
return blocks;
}
}
Compilation failed: 2 error(s), 0 warnings Solution.cs(13,16): error CS1525: Unexpected symbol `=' Solution.cs(13,37): error CS1525: Unexpected symbol `)', expecting `;' or `}'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int blocks = H.Length;
for (int i=0; i<H.Length-1; I++)
{ if (H[i] == H[i+1]) blocks--; }
return blocks;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(13,37): error CS0103: The name `I' does not exist in the current context
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int blocks = H.Length;
for (int i=0; i<H.Length-1; i++)
{ if (H[i] == H[i+1]) blocks--; }
return blocks;
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack();
int wallNum=0;
for (int i=0l i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i] break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
Compilation failed: 3 error(s), 1 warnings Solution.cs(17,20): warning CS0078: The `l' suffix is easily confused with the digit `1' (use `L' for clarity) Solution.cs(17,22): error CS1525: Unexpected symbol `i' Solution.cs(17,29): error CS1525: Unexpected symbol `}' Solution.cs(20,55): error CS1525: Unexpected symbol `break'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i] break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(20,55): error CS1525: Unexpected symbol `break'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i] continue;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(20,55): error CS1525: Unexpected symbol `continue'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i] wallNum = wallNum;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(20,55): error CS1525: Unexpected symbol `wallNum'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(28,246): error CS1525: Unexpected symbol `end-of-file'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
}
Compilation failed: 4 error(s), 0 warnings Solution.cs(14,31): error CS0305: Using the generic type `System.Collections.Generic.Stack<T>' requires `1' type argument(s) /opt/codility-mono/lib/mono/4.5/System.dll (Location of the symbol related to previous error) Solution.cs(19,36): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>' Solution.cs(20,35): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>' Solution.cs(24,25): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack<int>();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 & wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
}
Compilation failed: 3 error(s), 0 warnings Solution.cs(19,36): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>' Solution.cs(20,35): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>' Solution.cs(24,25): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
Stack<int> wall = new Stack<int>();
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
}
Compilation failed: 3 error(s), 0 warnings Solution.cs(19,37): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>' Solution.cs(20,35): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>' Solution.cs(24,25): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.Stack<int>'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
int [] wall = new int [L];
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
return stones;
}
}
}
Compilation failed: 1 error(s), 1 warnings Solution.cs(9,16): error CS0161: `Solution.solution(int[])': not all code paths return a value Solution.cs(17,28): warning CS0162: Unreachable code detected
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
int [] wall = new int [L];
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
}
return stones;
}
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(31,0): error CS1525: Unexpected symbol `}'
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
int [] wall = new int [L];
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) break;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
}
return stones;
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
int [] wall = new int [L];
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) continue;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
}
return stones;
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
int [] wall = new int [L];
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) continue;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
}
return stones;
}
}
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] H) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int L = H.Length;
int stones =0;
int [] wall = new int [L];
int wallNum=0;
for (int i=0; i<L; i++)
{
while (wallNum>0 && wall[wallNum-1] > H[i]) wallNum -=1;
if (wallNum >0 && wall[wallNum -1] == H[i]) continue;
else
{
stones +=1;
wall[wallNum] = H[i];
wallNum +=1;
}
}
return stones;
}
}
The solution obtained perfect score.