LeetCode in Kotlin

2136. Earliest Possible Day of Full Bloom

Hard

You have n flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two 0-indexed integer arrays plantTime and growTime, of length n each:

From the beginning of day 0, you can plant the seeds in any order.

Return the earliest possible day where all seeds are blooming.

Example 1:

Input: plantTime = [1,4,3], growTime = [2,3,1]

Output: 9

Explanation: The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.

One optimal way is:

On day 0, plant the 0th seed. The seed grows for 2 full days and blooms on day 3.

On days 1, 2, 3, and 4, plant the 1st seed. The seed grows for 3 full days and blooms on day 8.

On days 5, 6, and 7, plant the 2nd seed. The seed grows for 1 full day and blooms on day 9.

Thus, on day 9, all the seeds are blooming.

Example 2:

Input: plantTime = [1,2,3,2], growTime = [2,1,2,1]

Output: 9

Explanation: The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.

One optimal way is:

On day 1, plant the 0th seed. The seed grows for 2 full days and blooms on day 4.

On days 0 and 3, plant the 1st seed. The seed grows for 1 full day and blooms on day 5.

On days 2, 4, and 5, plant the 2nd seed. The seed grows for 2 full days and blooms on day 8.

On days 6 and 7, plant the 3rd seed. The seed grows for 1 full day and blooms on day 9.

Thus, on day 9, all the seeds are blooming.

Example 3:

Input: plantTime = [1], growTime = [1]

Output: 2

Explanation: On day 0, plant the 0th seed. The seed grows for 1 full day and blooms on day 2.

Thus, on day 2, all the seeds are blooming.

Constraints:

Solution

import java.util.Arrays
import java.util.Collections

class Solution {
    fun earliestFullBloom(plantTime: IntArray, growTime: IntArray): Int {
        val n = plantTime.size
        if (n == 1) {
            return plantTime[0] + growTime[0]
        }
        val arr = arrayOfNulls<Seed>(n)
        for (i in 0 until n) {
            arr[i] = Seed(plantTime[i], growTime[i])
        }
        Arrays.sort(arr, Collections.reverseOrder())
        var ans = arr[0]!!.plantTime + arr[0]!!.growTime
        var lastPlantDay = arr[0]!!.plantTime
        for (i in 1 until n) {
            val currBloomDay = lastPlantDay + arr[i]!!.plantTime + arr[i]!!.growTime
            ans = Math.max(ans, currBloomDay)
            lastPlantDay += arr[i]!!.plantTime
        }
        return ans
    }

    internal class Seed(var plantTime: Int, var growTime: Int) : Comparable<Seed> {
        override fun compareTo(other: Seed): Int {
            return growTime - other.growTime
        }
    }
}