Advertisement
pac1250

Codility Sigma 2012

May 23rd, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Solution
  5. {
  6.     public int stone_wall(int[] H)
  7.     {
  8.         var stoneStorage = new Stack<int>();
  9.         var stoneCount = 1;
  10.         var currentStoneHeight = H[0];
  11.         for (var currentPosition = 1; currentPosition < H.Length; currentPosition++)
  12.         {
  13.             var currentHeight = H[currentPosition];
  14.             if (currentHeight > currentStoneHeight)
  15.             {
  16.                 stoneCount++;
  17.                 stoneStorage.Push(currentStoneHeight);
  18.                 currentStoneHeight = currentHeight;
  19.             }
  20.             else if (currentHeight < currentStoneHeight)
  21.             {
  22.                 while (stoneStorage.Count > 0 && stoneStorage.Peek() >= currentHeight)
  23.                 {
  24.                     currentStoneHeight = stoneStorage.Pop();
  25.                 }
  26.  
  27.                 if (currentHeight < currentStoneHeight)
  28.                 {
  29.                     stoneCount++;
  30.                     currentStoneHeight = currentHeight;
  31.                 }
  32.             }
  33.         }
  34.         return stoneCount;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement