|
| 1 | +/** |
| 2 | + * 2136. Earliest Possible Day of Full Bloom |
| 3 | + * https://leetcode.com/problems/earliest-possible-day-of-full-bloom/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You have n flower seeds. Every seed must be planted first before it can begin to grow, then |
| 7 | + * bloom. Planting a seed takes time and so does the growth of a seed. You are given two 0-indexed |
| 8 | + * integer arrays plantTime and growTime, of length n each: |
| 9 | + * - plantTime[i] is the number of full days it takes you to plant the ith seed. Every day, you can |
| 10 | + * work on planting exactly one seed. You do not have to work on planting the same seed on |
| 11 | + * consecutive days, but the planting of a seed is not complete until you have worked plantTime[i] |
| 12 | + * days on planting it in total. |
| 13 | + * - growTime[i] is the number of full days it takes the ith seed to grow after being completely |
| 14 | + * planted. After the last day of its growth, the flower blooms and stays bloomed forever. |
| 15 | + * |
| 16 | + * From the beginning of day 0, you can plant the seeds in any order. |
| 17 | + * |
| 18 | + * Return the earliest possible day where all seeds are blooming. |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {number[]} plantTime |
| 23 | + * @param {number[]} growTime |
| 24 | + * @return {number} |
| 25 | + */ |
| 26 | +var earliestFullBloom = function(plantTime, growTime) { |
| 27 | + const seeds = plantTime.map((plant, index) => ({ plant, grow: growTime[index] })); |
| 28 | + seeds.sort((a, b) => b.grow - a.grow); |
| 29 | + |
| 30 | + let plantingDays = 0; |
| 31 | + let result = 0; |
| 32 | + |
| 33 | + for (const { plant, grow } of seeds) { |
| 34 | + plantingDays += plant; |
| 35 | + result = Math.max(result, plantingDays + grow); |
| 36 | + } |
| 37 | + |
| 38 | + return result; |
| 39 | +}; |
0 commit comments