본문 바로가기

공부하자/Codility

[Codility] Lesson7. StoneWall (C#)

문제: app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/

 

StoneWall coding task - Learn to Code - Codility

Cover "Manhattan skyline" using the minimum number of rectangles.

app.codility.com

 

 

 

            Stack<int> stack = new Stack<int>();
            int result = 0;
            for (int i = 0; i < H.Length; i++)
            {
                if (stack.Count == 0)
                    stack.Push(H[i]);
                else
                {
                    while (stack.Count > 0)
                    {
                        if (stack.Peek() == H[i])
                        {
                            stack.Pop();
                            if (stack.Count == 0)
                            {
                                stack.Push(H[i]);
                                break;
                            }

                        }
                        else if (stack.Peek() > H[i])
                        {
                            stack.Pop();
                            result++;
                            if (stack.Count == 0)
                            {
                                stack.Push(H[i]);
                                break;
                            }
                        }
                        else
                        {
                            stack.Push(H[i]);
                            break;
                        }
                    }
                }
            }

            return result + stack.Count;

 

'공부하자 > Codility' 카테고리의 다른 글

[Codility] Lesson8. EquiLeader (C#)  (0) 2020.11.20
[Codility] Lesson8. Dominator (C#)  (0) 2020.11.20
[Codility] Lesson7. Nesting (C#)  (0) 2020.11.20
[Codility] Lesson7. Fish (C#)  (0) 2020.11.20
[Codility] Lesson7. Brackets (C#)  (0) 2020.11.20