bablu_45's blog

By bablu_45, history, 5 weeks ago, In English,

Given an array of roses. roses[i] means rose i will bloom on day roses[i]. Also given an int k, which is the minimum number of adjacent bloom roses required for a bouquet, and an int n, which is the number of bouquets we need. Return the earliest day that we can get n bouquets of roses.

Example: Input: roses = [1, 2, 4, 9, 3, 4, 1], k = 2, n = 2

Output: 4


day 1: [b, n, n, n, n, n, b]. The first and the last rose bloom.

day 2: [b, b, n, n, n, n, b]. The second rose blooms. Here the first two bloom roses make a bouquet.

day 3: [b, b, n, n, b, n, b]

day 4: [b, b, b, n, b, b, b]

Here the last three bloom roses make a bouquet, meeting the required n = 2 bouquets of bloom roses. So return day 4.

I am looking for a efficient D.P solution (if possible).

  • Vote: I like it
  • +1
  • Vote: I do not like it

5 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

Do a binary search on number of days. the complexity would be O(log(num_days)) * O(num_roses)

  • »
    5 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    I am sorry that I forgot to mention that I was looking for an efficient D.P solution (if possible) even if it's worse than binary search.

    Thanks anyway.