To get the maximum number of length from an array of length :
Use a monotonic decreasing stack. For each digit, while the stack is not empty, the top is smaller, and you have enough remaining digits to fill slots, pop the stack.
Push the current digit if the stack size is less than .
function maxFromOne(nums, m)
stack := []
drop := length - m
for d in nums
while stack not empty and stack.top < d and drop > 0
stack.pop()
drop := drop - 1
if stack.size < m then
stack.push(d)
return stack