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;
  Seed(int p, int g) : p(p), g(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, [](const Seed& a, const Seed& b) { return a.g > b.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
22
23
24
25
26
27
28
class Seed {
  public int p;
  public int g;
  public Seed(int p, int g) {
    this.p = p;
    this.g = g;
  }
};

class Solution {
  public int earliestFullBloom(int[] plantTime, int[] growTime) {
    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) -> 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
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