Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ff978d

Browse files
feat: add solutions to lc problem: No.2136 (#1715)
No.2136.Earliest Possible Day of Full Bloom
1 parent 3375b53 commit 8ff978d

File tree

8 files changed

+187
-96
lines changed

8 files changed

+187
-96
lines changed

‎solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README.md‎

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@
6868

6969
**方法一:贪心 + 排序**
7070

71-
生长时间越长的种子,越先播种,因此将 $growTime$ 降序排列。
71+
根据题目描述,我们知道,每一天只能为一枚种子进行播种,因此不管什么播种顺序,所有种子的播种时间之和总是等于 $\sum_{i=0}^{n-1} plantTime[i]$。那么,为了让尽快让所有种子开花,我们应该尽快播种生长时间最长的种子。因此,我们可以对所有种子按照生长时间从大到小进行排序,然后依次进行播种。
72+
73+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 是种子的数量。
7274

7375
<!-- tabs:start -->
7476

@@ -80,9 +82,9 @@
8082
class Solution:
8183
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
8284
ans = t = 0
83-
for a, b in sorted(zip(plantTime, growTime), key=lambda x: -x[1]):
84-
t += a
85-
ans = max(ans, t + b)
85+
for pt, gt in sorted(zip(plantTime, growTime), key=lambda x: -x[1]):
86+
t += pt
87+
ans = max(ans, t + gt)
8688
return ans
8789
```
8890

@@ -94,16 +96,15 @@ class Solution:
9496
class Solution {
9597
public int earliestFullBloom(int[] plantTime, int[] growTime) {
9698
int n = plantTime.length;
97-
int[][] arr = new int[n][2];
98-
for (int i = 0; i < n; ++i) {
99-
arr[i] = newint[] {plantTime[i], growTime[i]};
99+
Integer[] idx = new Integer[n];
100+
for (int i = 0; i < n; i++) {
101+
idx[i] = i;
100102
}
101-
Arrays.sort(arr, (a, b) -> b[1] - a[1]);
102-
int ans = 0;
103-
int t = 0;
104-
for (int[] e : arr) {
105-
t += e[0];
106-
ans = Math.max(ans, t + e[1]);
103+
Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]);
104+
int ans = 0, t = 0;
105+
for (int i : idx) {
106+
t += plantTime[i];
107+
ans = Math.max(ans, t + growTime[i]);
107108
}
108109
return ans;
109110
}
@@ -117,13 +118,13 @@ class Solution {
117118
public:
118119
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
119120
int n = plantTime.size();
120-
vector<pair<int, int>> arr;
121-
for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]});
122-
sort(arr.begin(), arr.end());
121+
vector<int> idx(n);
122+
iota(idx.begin(), idx.end(), 0);
123+
sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; });
123124
int ans = 0, t = 0;
124-
for (auto [a, b] : arr) {
125-
t += b;
126-
ans = max(ans, t - a);
125+
for (int i : idx) {
126+
t += plantTime[i];
127+
ans = max(ans, t + growTime[i]);
127128
}
128129
return ans;
129130
}
@@ -133,20 +134,19 @@ public:
133134
### **Go**
134135
135136
```go
136-
func earliestFullBloom(plantTime []int, growTime []int) int {
137-
arr := [][]int{}
138-
for i, a := range plantTime {
139-
arr = append(arr, []int{a, growTime[i]})
137+
func earliestFullBloom(plantTime []int, growTime []int) (ans int) {
138+
n := len(plantTime)
139+
idx := make([]int, n)
140+
for i := range idx {
141+
idx[i] = i
140142
}
141-
sort.Slice(arr, func(i, j int) bool {
142-
return arr[i][1] > arr[j][1]
143-
})
144-
ans, t := 0, 0
145-
for _, e := range arr {
146-
t += e[0]
147-
ans = max(ans, t+e[1])
143+
sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] })
144+
t := 0
145+
for _, i := range idx {
146+
t += plantTime[i]
147+
ans = max(ans, t+growTime[i])
148148
}
149-
return ans
149+
return
150150
}
151151
152152
func max(a, b int) int {
@@ -159,10 +159,36 @@ func max(a, b int) int {
159159

160160
### **TypeScript**
161161

162-
<!-- 这里可写当前语言的特殊实现逻辑 -->
163-
164162
```ts
163+
function earliestFullBloom(plantTime: number[], growTime: number[]): number {
164+
const n = plantTime.length;
165+
const idx: number[] = Array.from({ length: n }, (_, i) => i);
166+
idx.sort((i, j) => growTime[j] - growTime[i]);
167+
let [ans, t] = [0, 0];
168+
for (const i of idx) {
169+
t += plantTime[i];
170+
ans = Math.max(ans, t + growTime[i]);
171+
}
172+
return ans;
173+
}
174+
```
165175

176+
### **Rust**
177+
178+
```rust
179+
impl Solution {
180+
pub fn earliest_full_bloom(plant_time: Vec<i32>, grow_time: Vec<i32>) -> i32 {
181+
let mut idx: Vec<usize> = (0..plant_time.len()).collect();
182+
idx.sort_by_key(|&i| -&grow_time[i]);
183+
let mut ans = 0;
184+
let mut t = 0;
185+
for &i in &idx {
186+
t += plant_time[i];
187+
ans = ans.max(t + grow_time[i]);
188+
}
189+
ans
190+
}
191+
}
166192
```
167193

168194
### **...**

‎solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README_EN.md‎

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ Thus, on day 2, all the seeds are blooming.
7171
class Solution:
7272
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
7373
ans = t = 0
74-
for a, b in sorted(zip(plantTime, growTime), key=lambda x: -x[1]):
75-
t += a
76-
ans = max(ans, t + b)
74+
for pt, gt in sorted(zip(plantTime, growTime), key=lambda x: -x[1]):
75+
t += pt
76+
ans = max(ans, t + gt)
7777
return ans
7878
```
7979

@@ -83,16 +83,15 @@ class Solution:
8383
class Solution {
8484
public int earliestFullBloom(int[] plantTime, int[] growTime) {
8585
int n = plantTime.length;
86-
int[][] arr = new int[n][2];
87-
for (int i = 0; i < n; ++i) {
88-
arr[i] = newint[] {plantTime[i], growTime[i]};
86+
Integer[] idx = new Integer[n];
87+
for (int i = 0; i < n; i++) {
88+
idx[i] = i;
8989
}
90-
Arrays.sort(arr, (a, b) -> b[1] - a[1]);
91-
int ans = 0;
92-
int t = 0;
93-
for (int[] e : arr) {
94-
t += e[0];
95-
ans = Math.max(ans, t + e[1]);
90+
Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]);
91+
int ans = 0, t = 0;
92+
for (int i : idx) {
93+
t += plantTime[i];
94+
ans = Math.max(ans, t + growTime[i]);
9695
}
9796
return ans;
9897
}
@@ -106,13 +105,13 @@ class Solution {
106105
public:
107106
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
108107
int n = plantTime.size();
109-
vector<pair<int, int>> arr;
110-
for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]});
111-
sort(arr.begin(), arr.end());
108+
vector<int> idx(n);
109+
iota(idx.begin(), idx.end(), 0);
110+
sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; });
112111
int ans = 0, t = 0;
113-
for (auto [a, b] : arr) {
114-
t += b;
115-
ans = max(ans, t - a);
112+
for (int i : idx) {
113+
t += plantTime[i];
114+
ans = max(ans, t + growTime[i]);
116115
}
117116
return ans;
118117
}
@@ -122,20 +121,19 @@ public:
122121
### **Go**
123122
124123
```go
125-
func earliestFullBloom(plantTime []int, growTime []int) int {
126-
arr := [][]int{}
127-
for i, a := range plantTime {
128-
arr = append(arr, []int{a, growTime[i]})
124+
func earliestFullBloom(plantTime []int, growTime []int) (ans int) {
125+
n := len(plantTime)
126+
idx := make([]int, n)
127+
for i := range idx {
128+
idx[i] = i
129129
}
130-
sort.Slice(arr, func(i, j int) bool {
131-
return arr[i][1] > arr[j][1]
132-
})
133-
ans, t := 0, 0
134-
for _, e := range arr {
135-
t += e[0]
136-
ans = max(ans, t+e[1])
130+
sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] })
131+
t := 0
132+
for _, i := range idx {
133+
t += plantTime[i]
134+
ans = max(ans, t+growTime[i])
137135
}
138-
return ans
136+
return
139137
}
140138
141139
func max(a, b int) int {
@@ -149,7 +147,35 @@ func max(a, b int) int {
149147
### **TypeScript**
150148

151149
```ts
150+
function earliestFullBloom(plantTime: number[], growTime: number[]): number {
151+
const n = plantTime.length;
152+
const idx: number[] = Array.from({ length: n }, (_, i) => i);
153+
idx.sort((i, j) => growTime[j] - growTime[i]);
154+
let [ans, t] = [0, 0];
155+
for (const i of idx) {
156+
t += plantTime[i];
157+
ans = Math.max(ans, t + growTime[i]);
158+
}
159+
return ans;
160+
}
161+
```
152162

163+
### **Rust**
164+
165+
```rust
166+
impl Solution {
167+
pub fn earliest_full_bloom(plant_time: Vec<i32>, grow_time: Vec<i32>) -> i32 {
168+
let mut idx: Vec<usize> = (0..plant_time.len()).collect();
169+
idx.sort_by_key(|&i| -&grow_time[i]);
170+
let mut ans = 0;
171+
let mut t = 0;
172+
for &i in &idx {
173+
t += plant_time[i];
174+
ans = ans.max(t + grow_time[i]);
175+
}
176+
ans
177+
}
178+
}
153179
```
154180

155181
### **...**
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class Solution {
2-
public:
3-
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
4-
int n = plantTime.size();
5-
vector<pair<int, int>> arr;
6-
for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]});
7-
sort(arr.begin(), arr.end());
8-
int ans = 0, t = 0;
9-
for (auto [a, b] : arr) {
10-
t += b;
11-
ans = max(ans, t - a);
12-
}
13-
return ans;
14-
}
1+
class Solution {
2+
public:
3+
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
4+
int n = plantTime.size();
5+
vector<int> idx(n);
6+
iota(idx.begin(), idx.end(), 0);
7+
sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; });
8+
int ans = 0, t = 0;
9+
for (int i : idx) {
10+
t += plantTime[i];
11+
ans = max(ans, t + growTime[i]);
12+
}
13+
return ans;
14+
}
1515
};

‎solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.go‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
func earliestFullBloom(plantTime []int, growTime []int) int {
2-
arr := [][]int{}
3-
for i, a := range plantTime {
4-
arr = append(arr, []int{a, growTime[i]})
1+
func earliestFullBloom(plantTime []int, growTime []int) (ans int) {
2+
n := len(plantTime)
3+
idx := make([]int, n)
4+
for i := range idx {
5+
idx[i] = i
56
}
6-
sort.Slice(arr, func(i, j int) bool {
7-
return arr[i][1] > arr[j][1]
8-
})
9-
ans, t := 0, 0
10-
for _, e := range arr {
11-
t += e[0]
12-
ans = max(ans, t+e[1])
7+
sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] })
8+
t := 0
9+
for _, i := range idx {
10+
t += plantTime[i]
11+
ans = max(ans, t+growTime[i])
1312
}
14-
returnans
13+
return
1514
}
1615

1716
func max(a, b int) int {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int earliestFullBloom(int[] plantTime, int[] growTime) {
3+
int n = plantTime.length;
4+
Integer[] idx = new Integer[n];
5+
for (int i = 0; i < n; i++) {
6+
idx[i] = i;
7+
}
8+
Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]);
9+
int ans = 0, t = 0;
10+
for (int i : idx) {
11+
t += plantTime[i];
12+
ans = Math.max(ans, t + growTime[i]);
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
class Solution:
2-
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
3-
ans = t = 0
4-
for a, b in sorted(zip(plantTime, growTime), key=lambda x: -x[1]):
5-
t += a
6-
ans = max(ans, t + b)
7-
return ans
1+
class Solution:
2+
def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:
3+
ans = t = 0
4+
for pt, gt in sorted(zip(plantTime, growTime), key=lambda x: -x[1]):
5+
t += pt
6+
ans = max(ans, t + gt)
7+
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn earliest_full_bloom(plant_time: Vec<i32>, grow_time: Vec<i32>) -> i32 {
3+
let mut idx: Vec<usize> = (0..plant_time.len()).collect();
4+
idx.sort_by_key(|&i| -&grow_time[i]);
5+
let mut ans = 0;
6+
let mut t = 0;
7+
for &i in &idx {
8+
t += plant_time[i];
9+
ans = ans.max(t + grow_time[i]);
10+
}
11+
ans
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function earliestFullBloom(plantTime: number[], growTime: number[]): number {
2+
const n = plantTime.length;
3+
const idx: number[] = Array.from({ length: n }, (_, i) => i);
4+
idx.sort((i, j) => growTime[j] - growTime[i]);
5+
let [ans, t] = [0, 0];
6+
for (const i of idx) {
7+
t += plantTime[i];
8+
ans = Math.max(ans, t + growTime[i]);
9+
}
10+
return ans;
11+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /