Minimum Cost to Buy All Figures
A shop will release n action figures.
- Figure i costs i coins.
- It becomes available on day i and stays available until day n.
You are given a binary string s of length n.
- s[i] = '1' means Monocarp can visit the shop on day i + 1
- s[i] = '0' means he cannot visit on that day
Whenever Monocarp visits the shop, he can buy any number of figures that are already available and not bought earlier.
If he buys at least two figures on the same day, then the most expensive figure among them is free.
Monocarp wants to buy exactly one copy of every figure from 1 to n.
Return the minimum number of coins he needs to spend.
Notes:
- Figure i cannot be bought before day i
- A figure can be bought only once
- If Monocarp buys only one figure on a day, no discount is applied
- If he buys multiple figures on the same day, only the most expensive one is free
Example 1:
Input: s = "11"
Output: 1
Explanation:
On day 2, Monocarp can buy figures 1 and 2 together.
Figure 2 becomes free, so he only pays 1.
Example 2:
Input: s = "1011"
Output: 3
Now Your Turn!
Pick the correct output for the given inputInput: s = "1101"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 4 * 105
- s.length == n
- s[i] is either '0' or '1'
- s[i] = '1' means Monocarp can visit the shop on day i + 1