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:
int solution(vector<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].
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++)
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
if
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
if (H[i] != i)
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
if (H[i] != i) {
if (H[i] > 0) ans++;
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
if (prev != 0) ans ++;
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i<H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
if (prev != 0) ans ++;
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i <H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
if (prev != 0) ans ++;
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i < H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
if (prev != 0) ans ++;
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = 0, ans = 0;
for(int i=0; i < H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H, ans = 0;
for(int i=0; i < H.size(); i++) {
if (H[i] != prev) {
if (H[i] > 0) ans++;
prev = H[i];
}
}
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H[0], ans = 0;
for(int i=1; i < H.size(); i++) {
if (H[i] != prev) {
prev = H[i];
}
}
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H[0], ans = 0;
for(int i=1; i < H.size(); i++) {
if (H[i] != prev) {
ans++;
prev = H[i];
}
}
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H[0], ans = 0;
for(un i=1; i < H.size(); i++) {
if (H[i] != prev) {
ans++;
prev = H[i];
}
}
return ans;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H[0], ans = 0;
for(unsigned i=1; i < H.size(); i++) {
if (H[i] != prev) {
ans++;
prev = H[i];
}
}
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H[0], ans = 0;
for(unsigned i=1; i < H.size(); i++) {
if (H[i] != prev) {
ans++;
prev = H[i];
}
}
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
[8, 8, 8, 8]
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int prev = H[0], ans = 0;
for(unsigned i=1; i < H.size(); i++) {
if (H[i] != prev) {
ans++;
prev = H[i];
}
}
return ans;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
[8, 8, 8, 8]
[1, 2, 3, 1, 2, 3]
function result: 7
function result: 0
function result: 5
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], size=0;
for(unsigned i=0; i<H.size(); i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], s=0;
for(unsigned i=0; i<H.size(); i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stack_size=0;
for(unsigned i=0; i<H.size(); i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0;
for(unsigned i=0; i<H.size(); i++) {
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0;
for(unsigned i=0; i<H.size(); i++) {
if
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0;
for(unsigned i=0; i<H.size(); i++) {
if (stackSize > 0 && H[i] )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[])
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (stackSize > 0 && H[i] > )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (stackSize > 0 && H[i] > stack[stackSize - 1] || )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (stackSize > 0 && H[i] > stack[stackSize - 1] || stackSize )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (stackSize > 0 ? H[i] > stack[stackSize - 1] : stackSize )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (stackSize > 0 ? H[i] > stack[stackSize - 1] : H[] )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stack[stackSize - 1]) )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0) )
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0)) {
stack[stackSize++] = H[i];
}
}
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
unsigned stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0)) {
stack[stackSize++] = H[i];
}
}
return ans + stackSize;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
[8, 8, 8, 8]
[1, 2, 3, 1, 2, 3]
function result: 7
function result: 1
function result: 5
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0)) {
stack[stackSize++] = H[i];
}
}
return ans + stackSize;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0)) {
stack[stackSize++] = H[i];
}
}
return ans + stackSize;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
[8, 8, 8, 8]
[1, 2, 3, 1, 2, 3]
function result: 7
function result: 1
function result: 5
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0)) {
stack[stackSize++] = H[i];
}
}
return ans + stackSize;
}
[8, 8, 5, 7, 9, 8, 7, 4, 8]
[8, 8, 8, 8]
[1, 2, 3, 1, 2, 3]
function result: 7
function result: 1
function result: 5
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
int stack[100000], stackSize=0, ans = 0;
for(unsigned i=0; i<H.size(); i++) {
while (stackSize > 0 && H[i] < stack[stackSize - 1]) {
stackSize --;
ans ++;
}
if (H[i] > (stackSize > 0 ? stack[stackSize - 1] : 0)) {
stack[stackSize++] = H[i];
}
}
return ans + stackSize;
}
The solution obtained perfect score.