Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Solution
- {
- public int stone_wall(int[] H)
- {
- var stoneStorage = new Stack<int>();
- var stoneCount = 1;
- var currentStoneHeight = H[0];
- for (var currentPosition = 1; currentPosition < H.Length; currentPosition++)
- {
- var currentHeight = H[currentPosition];
- if (currentHeight > currentStoneHeight)
- {
- stoneCount++;
- stoneStorage.Push(currentStoneHeight);
- currentStoneHeight = currentHeight;
- }
- else if (currentHeight < currentStoneHeight)
- {
- while (stoneStorage.Count > 0 && stoneStorage.Peek() >= currentHeight)
- {
- currentStoneHeight = stoneStorage.Pop();
- }
- if (currentHeight < currentStoneHeight)
- {
- stoneCount++;
- currentStoneHeight = currentHeight;
- }
- }
- }
- return stoneCount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement