Skip to content

Commit e9e7f05

Browse files
committed
StoneWall
1 parent 1bcc40c commit e9e7f05

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <vector>
2+
#include <stack>
3+
4+
void heightCheck(size_t i, const std::vector<int>& H, std::stack<int>& heights, int& counter) noexcept
5+
{
6+
if (heights.empty() || heights.top() < H[i])
7+
{
8+
heights.push(H[i]);
9+
++counter;
10+
}
11+
else if (heights.top() > H[i])
12+
{
13+
heights.pop();
14+
heightCheck(i, H, heights, counter);
15+
}
16+
}
17+
18+
// Recursive solution. Detected time complexity: O(N)
19+
int solution(std::vector<int>& H) noexcept
20+
{
21+
std::stack<int> heights;
22+
int counter = 0;
23+
for (size_t i = 0; i != H.size(); ++i)
24+
{
25+
heightCheck(i, H, heights, counter);
26+
}
27+
28+
return counter;
29+
}

0 commit comments

Comments
 (0)