[LeetCode] Earliest Possible Day of Full Bloom

2136. Earliest Possible Day of Full Bloom

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:

  • plantTime[i] is the number of full days it takes you to plant the ith seed. Every day, you can work on planting exactly one seed. You do not have to work on planting the same seed on consecutive days, but the planting of a seed is not complete until you have worked plantTime[i] days on planting it in total.
  • growTime[i] is the number of full days it takes the ith seed to grow after being completely planted. After the last day of its growth, the flower blooms and stays bloomed forever.
    From the beginning of day 0, you can plant the seeds in any order.

Return the earliest possible day where all seeds are blooming.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool compare(pair<int, int> p1, pair<int, int> p2) {
return p1.second > p2.second;
}

class Solution {
public:
int earliestFullBloom(vector<int>& p, vector<int>& g) {
int pSum(0), res(0);
vector<pair<int, int>> pairs;
for(int i = 0; i < p.size(); i++) {
pairs.push_back({p[i], g[i]});
}
sort(begin(pairs), end(pairs), compare);
for(int i = 0; i < p.size(); i++) {
res = max(res, pSum + pairs[i].first + pairs[i].second);
pSum += pairs[i].first;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/25/PS/LeetCode/earliest-possible-day-of-full-bloom/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.