Maximum Washable Prefix
You are given an integer array plates, where plates[i] is the label of the ith plate from the top of the dirty stack.
You need to process the plates using these rules:
- You always take the next plate from the top of the dirty stack.
- After taking a plate, you place it on a soapy stack.
- You may: put it on top of an existing soapy stack, or start a new soapy stack to the right of all existing soapy stacks.
- Later, plates can only be removed from the top of the leftmost soapy stack and moved to the clean stack.
Your goal is to make the clean stack sorted properly:
- smaller labels should be below larger labels,
- so the plates placed into the clean stack must come out in increasing order.
It may not be possible to do this for all plates.
Return the length of the longest prefix of plates that can be processed successfully.
Example 1:
Input: plates = [4,5,2,3,1]
Output: 4
Explanation:
- The first 4 plates can be processed in a valid way.
- After including the last plate, it becomes impossible to keep the clean stack in the required order.
Example 2:
Input: plates = [1,2,3,4]
Output: 4
Explanation:
- All plates can be processed successfully.
Now Your Turn!
Pick the correct output for the given inputInput: plates = [3,1,2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= plates.length <= 100000
- 1 <= plates[i] <= plates.length
- All values in plates are distinct
- plates is a permutation of integers from 1 to plates.length