Maximum Bouquet Value
Problem statement
A row of flowers is given as a string s. The character 0 is a Rose and the character 1 is a Cosmos.
You can make two kinds of bouquet from flowers that stand next to each other:
- Three Roses in a row (
000) make a bouquet worthp. - Two different flowers in a row (
01or10) make a bouquet worthq.
Each flower can be used in at most one bouquet, and a flower may stay unused. Return the largest total value you can get.
Function
maxValue(s: String, p: int, q: int) → intExamples
Example 1
s = "00010"p = 5q = 3return = 8Use 000 from the first three flowers for 5, then 10 from the last two for 3. The total is 8.
Example 2
s = "0110"p = 10q = 4return = 8The string has no three Roses in a row. The pairs 01 and 10 do not share a flower, so they give 4 + 4 = 8. The two Cosmos in the middle are not a bouquet by themselves.
Example 3
s = "0001"p = 1q = 5return = 5Using 000 gives only 1 and leaves the Cosmos alone. Using 01 from the last two flowers gives 5 and leaves two Roses unused, which is better.
Constraints
1 <= s.length <= 20000scontains only the characters0and1.1 <= p <= 10^51 <= q <= 10^5