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 write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
if(start >= end) {
return 0;
}
if(end - start == 1) {
return 1;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight <= 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
}
else {
blockEnd = i;
}
}
else {
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart, blockEnd);
}
else {
blocks += 1;
}
}
blockStart = null;
blockEnd = null;
}
}
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart, blockEnd);
}
}
}
}
Compilation failed: 4 error(s), 1 warnings Solution.cs(17,9): warning CS0162: Unreachable code detected Solution.cs(60,35): error CS1502: The best overloaded method match for `Solution.solve(int[], int, int)' has some invalid arguments Solution.cs(14,9): (Location of the symbol related to previous error) Solution.cs(60,44): error CS1503: Argument `#2' cannot convert `int?' expression to type `int' Solution.cs(73,27): error CS1502: The best overloaded method match for `Solution.solve(int[], int, int)' has some invalid arguments Solution.cs(14,9): (Location of the symbol related to previous error) Solution.cs(73,36): error CS1503: Argument `#2' cannot convert `int?' expression to type `int'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// if(start >= end) {
// return 0;
// }
// if(end - start == 1) {
// return 1;
// }
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
}
else {
blockEnd = i;
}
}
else {
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart, blockEnd);
}
else {
blocks += 1;
}
}
blockStart = null;
blockEnd = null;
}
}
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart, blockEnd);
}
}
}
}
Compilation failed: 4 error(s), 0 warnings Solution.cs(60,35): error CS1502: The best overloaded method match for `Solution.solve(int[], int, int)' has some invalid arguments Solution.cs(14,9): (Location of the symbol related to previous error) Solution.cs(60,44): error CS1503: Argument `#2' cannot convert `int?' expression to type `int' Solution.cs(73,27): error CS1502: The best overloaded method match for `Solution.solve(int[], int, int)' has some invalid arguments Solution.cs(14,9): (Location of the symbol related to previous error) Solution.cs(73,36): error CS1503: Argument `#2' cannot convert `int?' expression to type `int'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// if(start >= end) {
// return 0;
// }
// if(end - start == 1) {
// return 1;
// }
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
}
else {
blockEnd = i;
}
}
else {
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, blockEnd.Value);
}
else {
blocks += 1;
}
}
blockStart = null;
blockEnd = null;
}
}
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, blockEnd.Value);
}
}
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(43,9): error CS0161: `Solution.splitAndSolve(int[], int, int)': not all code paths return a value
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// if(start >= end) {
// return 0;
// }
// if(end - start == 1) {
// return 1;
// }
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
}
else {
blockEnd = i;
}
}
else {
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, blockEnd.Value);
}
else {
blocks += 1;
}
}
blockStart = null;
blockEnd = null;
}
}
if(blockStart.HasValue) {
if(blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, blockEnd.Value);
}
}
return blocks;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(14,9): error CS0161: `Solution.solve(int[], int, int)': not all code paths return a value
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i)
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i)
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
Compilation failed: 2 error(s), 0 warnings Solution.cs(51,20): error CS1525: Unexpected symbol `blocks' Solution.cs(60,16): error CS1525: Unexpected symbol `blocks'
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i);
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i);
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(59,75): 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 write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
return 0;
// int minHeight = int.MaxValue;
// int maxHeight = 0;
// for(int i = start; i < end; i++) {
// minHeight = Math.Min(minHeight, H[i]);
// maxHeight = Math.Max(maxHeight, H[i]);
// }
// if(maxHeight <= 0) {
// return 0;
// }
// for(int i = start; i < end; i++) {
// H[i] -= minHeight;
// }
// return splitAndSolve(H, start, end) + 1;
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i);
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, end);
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
Solve for: 0 9
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 1;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight <= 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i);
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, end);
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
Solve for: 0 9 Solve for: 0 7 Solve for: 0 2 Solve for: 3 7 Solve for: 4 6 Solve for: 4 5 Solve for: 8 9
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 1;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight <= 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i);
blocks += solve(H, blockStart.Value, i) + 1;
Console.WriteLine("Blocks: {0}", solve(H, blockStart.Value, i));
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, end);
blocks += solve(H, blockStart.Value, end) + 1;
Console.WriteLine("Blocks: {0}", solve(H, blockStart.Value, end));
}
}
return blocks;
}
}
Solve for: 0 9 Solve for: 0 7 Solve for: 0 2 Blocks: 0 Solve for: 3 7 Solve for: 4 6 Solve for: 4 5 Blocks: 1 Solve for: 4 5 Blocks: 1 Blocks: 2 Solve for: 4 5 Blocks: 1 Blocks: 2 Solve for: 0 2 Blocks: 0 Solve for: 4 5 Blocks: 1 Blocks: 3 Solve for: 8 9 Blocks: 1 Solve for: 0 2 Blocks: 0 Solve for: 4 5 Blocks: 1 Solve for: 8 9 Blocks: 1 Blocks: 5
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 0;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight == 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, i);
blocks += solve(H, blockStart.Value, i) + 1;
Console.WriteLine("Blocks: {0}", solve(H, blockStart.Value, i));
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
Console.WriteLine("Solve for: {0} {1}", blockStart.Value, end);
blocks += solve(H, blockStart.Value, end) + 1;
Console.WriteLine("Blocks: {0}", solve(H, blockStart.Value, end));
}
}
return blocks;
}
}
Solve for: 0 9 Solve for: 0 7 Solve for: 0 2 Blocks: 0 Solve for: 3 7 Solve for: 4 6 Solve for: 4 5 Blocks: 0 Solve for: 4 5 Blocks: 0 Blocks: 1 Solve for: 4 5 Blocks: 0 Blocks: 1 Solve for: 0 2 Blocks: 0 Solve for: 4 5 Blocks: 0 Blocks: 2 Solve for: 8 9 Blocks: 0 Solve for: 0 2 Blocks: 0 Solve for: 4 5 Blocks: 0 Solve for: 8 9 Blocks: 0 Blocks: 3
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 0;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight == 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 0;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight == 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
[5, 5, 5]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 0;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight == 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
[5, 5, 5]
[8, 8, 8, 0, 8, 8, 8]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 0;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight == 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
[5, 5, 5]
[8, 8, 8, 0, 8, 8, 8]
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout 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)
return splitAndSolve(H, 0, H.Length);
}
int solve(int[] H, int start, int end) {
if(end - start == 1) {
return 0;
}
int minHeight = int.MaxValue;
int maxHeight = 0;
for(int i = start; i < end; i++) {
minHeight = Math.Min(minHeight, H[i]);
maxHeight = Math.Max(maxHeight, H[i]);
}
if(maxHeight - minHeight == 0) {
return 0;
}
for(int i = start; i < end; i++) {
H[i] -= minHeight;
}
return splitAndSolve(H, start, end);
}
int splitAndSolve(int[] H, int start, int end) {
int? blockStart = null, blockEnd = null;
int blocks = 0;
for(int i = start; i < end; i++) {
if(H[i] != 0) {
if(blockStart == null) {
blockStart = i;
blockEnd = null;
}
}
else {
if(blockStart.HasValue) {
blockEnd = i;
blocks += solve(H, blockStart.Value, i) + 1;
}
blockStart = null;
}
}
if(blockStart.HasValue) {
if(!blockEnd.HasValue) {
blocks += solve(H, blockStart.Value, end) + 1;
}
}
return blocks;
}
}
The following issues have been detected: timeout errors.