FastPrepMaximum Bouquet Value

Maximum Bouquet Value

Oracle logoOracle● MediumFULLTIMEONSITE INTERVIEW

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 worth p.
  • Two different flowers in a row (01 or 10) make a bouquet worth q.

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) → int

Examples

Example 1

s = "00010"p = 5q = 3return = 8

Use 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 = 8

The 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 = 5

Using 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 <= 20000
  • s contains only the characters 0 and 1.
  • 1 <= p <= 10^5
  • 1 <= q <= 10^5

More Oracle problems

See Oracle hiring insights
Saved
public int maxValue(String s, int p, int q) {
    // Write your code here.
}
Checking account…