Search code examples
algorithmdata-structuresarray-algorithms

How do I analyze k empty slots algorithm?


There is an algorithm on Leet Code called K Empty Slots. I don't understand the constraints. I tried researching a better explanation of the question but I cannot find one. It is as follows:

There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will be in the status of blooming since then.

Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.

For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, where i and x will be in the range from 1 to N.

Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is k and these flowers are not blooming.

If there isn't such day, output -1.

Example 1:

Input:

flowers: [1,3,2]

k: 1

Output: 2

Explanation: In the second day, the first and the third flower have become blooming.

Example 2:

Input:

flowers: [1,2,3]

k: 1

Output: -1

Note:

The given array will be in the range [1, 20000].

I want to solve it myself. I want to know if there is a simpler explanation of the problem. I do not understand the k input. I do not understand why the first example returned 2 but the second example returned -1.


Solution

  • According to the problem statements:

    1) There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will be in the status of blooming since then

    It basically tells that there is an array of size N, which are slots in which flowers will be bloomed one on each day. So the array size will tell you the number of flowers.

    2) Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.

    The arr[i] will tell you the slot in which the flower will be bloomed and i (index) will tell you the day on which flower will be bloomed on that day. For example:

    [1,3,2] here tells that

    • Day 1: flower will be bloomed in slot 1.
    • Day 2: flower will be bloomed in slot 3.
    • Day 3: flower will be bloomed in slot 2

    3) Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is k and these flowers are not blooming.

    From this statement, we understand that we have to output that day when two flowers that are blooming are in those slots such that the slots between them should be empty and the number of slots between them equals k. If there is no such slot, return -1.

    For the first example. [1,3,2] here as explained above, on day 2 the flower will be blooming on slot 3 and on day 1 the flower bloomed on slot 1, so the number of slots on day 2 free is 1 (equal to k) between them. Hence the output is day 2.

    For the second example, we see the flowers are in consecutive slots, so no un-bloomed flower slot exists between any of them, hence the output is -1.

    Hope this helps!