Skip to content

2136. Earliest Possible Day of Full Bloom 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct Seed {
  int p;
  int g;
};

class Solution {
 public:
  int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
    int ans = 0;
    int time = 0;
    vector<Seed> seeds;

    for (int i = 0; i < plantTime.size(); ++i)
      seeds.emplace_back(plantTime[i], growTime[i]);

    ranges::sort(seeds, ranges::greater{},
                 [](const Seed& seed) { return seed.g; });

    for (const auto& [p, g] : seeds) {
      time += p;
      ans = max(ans, time + g);
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
  public int earliestFullBloom(int[] plantTime, int[] growTime) {
    record Seed(int p, int g) {}
    final int n = plantTime.length;
    int ans = 0;
    int time = 0;
    Seed[] seeds = new Seed[n];

    for (int i = 0; i < plantTime.length; ++i)
      seeds[i] = new Seed(plantTime[i], growTime[i]);

    Arrays.sort(seeds, (a, b) -> Integer.compare(b.g, a.g));

    for (Seed seed : seeds) {
      time += seed.p;
      ans = Math.max(ans, time + seed.g);
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
  def earliestFullBloom(self, plantTime: list[int], growTime: list[int]) -> int:
    ans = 0
    time = 0

    for p, g in sorted(
        [(p, g) for (p, g) in zip(plantTime, growTime)],
            key=lambda x: -x[1]):
      time += p
      ans = max(ans, time + g)

    return ans