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 5cc6e3a

Browse files
committed
feat: add solutions to lc problem: No.2136
No.2136.Earliest Possible Day of Full Bloom
1 parent 3eb977e commit 5cc6e3a

File tree

8 files changed

+199
-96
lines changed

8 files changed

+199
-96
lines changed

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

Lines changed: 63 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,16 @@ 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+
int pt = plantTime[i], gt = growTime[i];
107+
t += pt;
108+
ans = Math.max(ans, t + gt);
107109
}
108110
return ans;
109111
}
@@ -117,13 +119,14 @@ class Solution {
117119
public:
118120
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
119121
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());
122+
vector<int> idx(n);
123+
iota(idx.begin(), idx.end(), 0);
124+
sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; });
123125
int ans = 0, t = 0;
124-
for (auto [a, b] : arr) {
125-
t += b;
126-
ans = max(ans, t - a);
126+
for (int i : idx) {
127+
int pt = plantTime[i], gt = growTime[i];
128+
t += pt;
129+
ans = max(ans, t + gt);
127130
}
128131
return ans;
129132
}
@@ -133,20 +136,20 @@ public:
133136
### **Go**
134137
135138
```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]})
139+
func earliestFullBloom(plantTime []int, growTime []int) (ans int) {
140+
n := len(plantTime)
141+
idx := make([]int, n)
142+
for i := range idx {
143+
idx[i] = i
140144
}
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])
145+
sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] })
146+
t := 0
147+
for _, i := range idx {
148+
pt, gt := plantTime[i], growTime[i]
149+
t += pt
150+
ans = max(ans, t+gt)
148151
}
149-
return ans
152+
return
150153
}
151154
152155
func max(a, b int) int {
@@ -159,10 +162,37 @@ func max(a, b int) int {
159162

160163
### **TypeScript**
161164

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

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

168198
### **...**

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

Lines changed: 60 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,16 @@ 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+
int pt = plantTime[i], gt = growTime[i];
94+
t += pt;
95+
ans = Math.max(ans, t + gt);
9696
}
9797
return ans;
9898
}
@@ -106,13 +106,14 @@ class Solution {
106106
public:
107107
int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {
108108
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());
109+
vector<int> idx(n);
110+
iota(idx.begin(), idx.end(), 0);
111+
sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; });
112112
int ans = 0, t = 0;
113-
for (auto [a, b] : arr) {
114-
t += b;
115-
ans = max(ans, t - a);
113+
for (int i : idx) {
114+
int pt = plantTime[i], gt = growTime[i];
115+
t += pt;
116+
ans = max(ans, t + gt);
116117
}
117118
return ans;
118119
}
@@ -122,20 +123,20 @@ public:
122123
### **Go**
123124
124125
```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]})
126+
func earliestFullBloom(plantTime []int, growTime []int) (ans int) {
127+
n := len(plantTime)
128+
idx := make([]int, n)
129+
for i := range idx {
130+
idx[i] = i
129131
}
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])
132+
sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] })
133+
t := 0
134+
for _, i := range idx {
135+
pt, gt := plantTime[i], growTime[i]
136+
t += pt
137+
ans = max(ans, t+gt)
137138
}
138-
return ans
139+
return
139140
}
140141
141142
func max(a, b int) int {
@@ -149,7 +150,36 @@ func max(a, b int) int {
149150
### **TypeScript**
150151

151152
```ts
153+
function earliestFullBloom(plantTime: number[], growTime: number[]): number {
154+
const n = plantTime.length;
155+
const idx: number[] = Array.from({ length: n }, (_, i) => i);
156+
idx.sort((i, j) => growTime[j] - growTime[i]);
157+
let [ans, t] = [0, 0];
158+
for (const i of idx) {
159+
const [pt, gt] = [plantTime[i], growTime[i]];
160+
t += pt;
161+
ans = Math.max(ans, t + gt);
162+
}
163+
return ans;
164+
}
165+
```
152166

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

155185
### **...**
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
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+
int pt = plantTime[i], gt = growTime[i];
11+
t += pt;
12+
ans = max(ans, t + gt);
13+
}
14+
return ans;
15+
}
1516
};

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
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+
pt, gt := plantTime[i], growTime[i]
11+
t += pt
12+
ans = max(ans, t+gt)
1313
}
14-
returnans
14+
return
1515
}
1616

1717
func max(a, b int) int {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
int pt = plantTime[i], gt = growTime[i];
12+
t += pt;
13+
ans = Math.max(ans, t + gt);
14+
}
15+
return ans;
16+
}
17+
}
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
const [pt, gt] = [plantTime[i], growTime[i]];
8+
t += pt;
9+
ans = Math.max(ans, t + gt);
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
(0)

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