Pancake

Chris is famous for baking perfectly round pancakes but they all come in different sizes. The customers usually don't care as long as its delicious, but today this one customer demands the pancakes comes in order, that is the smallest one being the top of the stack.

Chris is very annoyed but he will do it anyway. After baking a stack of pancakes, he will use a spatula to flip some top section of the pancake. For clarity, see the illustration below

sorting pancake with a spatula sorting pancake with a spatula

Your task is to sort the pancakes. First, you keep track of 1 stack PANCAKE and 1 queue ORDERED. While PANCAKE is not empty, perform the following

  • Flip the biggest size to the top.

  • Remove the top of PANCAKE x x away and push x x to ORDERED.

  • Flip PANCAKE

Let the number of flips you need to perform be n n . What is the complexity of this algorithm?

O ( n 3 ) O(n^3) O ( lg n ) O(\lg n) O ( n 2 ) O(n^2) O ( n lg n ) O(n\lg n) O ( n ) O(n) O ( n n ) O(n\sqrt{n}) O ( n ) O(\sqrt{n})

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

0 solutions

No explanations have been posted yet. Check back later!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...