Nayeon Sort
InterviewTime limit2sMemory limit1024 MB
Given an array, find the minimum number of stacks needed to sort it in ascending order using Nayeon sort.
- Level
Medium5 of 10
- Topics
- Array, Binary search, Dynamic programming
- Solved
- No attempts yet
Problem
Nayeon, who studied hard in her data structures class, devised a new sorting algorithm and named it Nayeon sort after herself. Given an array to sort, Nayeon sort works as follows.
- Declare as many empty stacks as you want.
- Insert each element from to , in order, into any stack you choose.
- Declare an empty array .
- Repeat until every stack is empty: pop one element from any stack you choose and append it to the end of .
Whether Nayeon sort can sort the array depends on the array and on how many stacks you use. For example, if , put , , and into three stacks, then pop from the third, second, third, first, and first stacks in that order. The result is the array sorted in ascending order. Two stacks are enough to sort , but with only one stack, cannot be sorted in ascending order.
Nayeon offers to secretly add one problem to your HCPC solve count if you find the minimum number of stacks needed to sort the given array in ascending order. Solve it before anyone else and get one step closer to winning HCPC!
Input
The first line contains the length of the array . ()
The second line contains integers , separated by spaces. ()
Output
On the first line, print the minimum number of stacks needed to sort in ascending order.