diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README.md b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README.md index 10f488ca5a3fb..20fcdb4952339 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README.md +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README.md @@ -68,7 +68,9 @@ **方法一:贪心 + 排序** -生长时间越长的种子,越先播种,因此将 $growTime$ 降序排列。 +根据题目描述,我们知道,每一天只能为一枚种子进行播种,因此不管什么播种顺序,所有种子的播种时间之和总是等于 $\sum_{i=0}^{n-1} plantTime[i]$。那么,为了让尽快让所有种子开花,我们应该尽快播种生长时间最长的种子。因此,我们可以对所有种子按照生长时间从大到小进行排序,然后依次进行播种。 + +时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 是种子的数量。 @@ -80,9 +82,9 @@ class Solution: def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int: ans = t = 0 - for a, b in sorted(zip(plantTime, growTime), key=lambda x: -x[1]): - t += a - ans = max(ans, t + b) + for pt, gt in sorted(zip(plantTime, growTime), key=lambda x: -x[1]): + t += pt + ans = max(ans, t + gt) return ans ``` @@ -94,16 +96,15 @@ class Solution: class Solution { public int earliestFullBloom(int[] plantTime, int[] growTime) { int n = plantTime.length; - int[][] arr = new int[n][2]; - for (int i = 0; i < n; ++i) { - arr[i] = new int[] {plantTime[i], growTime[i]}; + Integer[] idx = new Integer[n]; + for (int i = 0; i < n; i++) { + idx[i] = i; } - Arrays.sort(arr, (a, b) -> b[1] - a[1]); - int ans = 0; - int t = 0; - for (int[] e : arr) { - t += e[0]; - ans = Math.max(ans, t + e[1]); + Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]); + int ans = 0, t = 0; + for (int i : idx) { + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return ans; } @@ -117,13 +118,13 @@ class Solution { public: int earliestFullBloom(vector& plantTime, vector& growTime) { int n = plantTime.size(); - vector> arr; - for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]}); - sort(arr.begin(), arr.end()); + vector idx(n); + iota(idx.begin(), idx.end(), 0); + sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; }); int ans = 0, t = 0; - for (auto [a, b] : arr) { - t += b; - ans = max(ans, t - a); + for (int i : idx) { + t += plantTime[i]; + ans = max(ans, t + growTime[i]); } return ans; } @@ -133,20 +134,19 @@ public: ### **Go** ```go -func earliestFullBloom(plantTime []int, growTime []int) int { - arr := [][]int{} - for i, a := range plantTime { - arr = append(arr, []int{a, growTime[i]}) +func earliestFullBloom(plantTime []int, growTime []int) (ans int) { + n := len(plantTime) + idx := make([]int, n) + for i := range idx { + idx[i] = i } - sort.Slice(arr, func(i, j int) bool { - return arr[i][1]> arr[j][1] - }) - ans, t := 0, 0 - for _, e := range arr { - t += e[0] - ans = max(ans, t+e[1]) + sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] }) + t := 0 + for _, i := range idx { + t += plantTime[i] + ans = max(ans, t+growTime[i]) } - return ans + return } func max(a, b int) int { @@ -159,10 +159,36 @@ func max(a, b int) int { ### **TypeScript** - - ```ts +function earliestFullBloom(plantTime: number[], growTime: number[]): number { + const n = plantTime.length; + const idx: number[] = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => growTime[j] - growTime[i]); + let [ans, t] = [0, 0]; + for (const i of idx) { + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); + } + return ans; +} +``` +### **Rust** + +```rust +impl Solution { + pub fn earliest_full_bloom(plant_time: Vec, grow_time: Vec) -> i32 { + let mut idx: Vec = (0..plant_time.len()).collect(); + idx.sort_by_key(|&i| -&grow_time[i]); + let mut ans = 0; + let mut t = 0; + for &i in &idx { + t += plant_time[i]; + ans = ans.max(t + grow_time[i]); + } + ans + } +} ``` ### **...** diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README_EN.md b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README_EN.md index 2a8d99fb2e3fa..2189cc89c7340 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README_EN.md +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/README_EN.md @@ -71,9 +71,9 @@ Thus, on day 2, all the seeds are blooming. class Solution: def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int: ans = t = 0 - for a, b in sorted(zip(plantTime, growTime), key=lambda x: -x[1]): - t += a - ans = max(ans, t + b) + for pt, gt in sorted(zip(plantTime, growTime), key=lambda x: -x[1]): + t += pt + ans = max(ans, t + gt) return ans ``` @@ -83,16 +83,15 @@ class Solution: class Solution { public int earliestFullBloom(int[] plantTime, int[] growTime) { int n = plantTime.length; - int[][] arr = new int[n][2]; - for (int i = 0; i < n; ++i) { - arr[i] = new int[] {plantTime[i], growTime[i]}; + Integer[] idx = new Integer[n]; + for (int i = 0; i < n; i++) { + idx[i] = i; } - Arrays.sort(arr, (a, b) -> b[1] - a[1]); - int ans = 0; - int t = 0; - for (int[] e : arr) { - t += e[0]; - ans = Math.max(ans, t + e[1]); + Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]); + int ans = 0, t = 0; + for (int i : idx) { + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return ans; } @@ -106,13 +105,13 @@ class Solution { public: int earliestFullBloom(vector& plantTime, vector& growTime) { int n = plantTime.size(); - vector> arr; - for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]}); - sort(arr.begin(), arr.end()); + vector idx(n); + iota(idx.begin(), idx.end(), 0); + sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; }); int ans = 0, t = 0; - for (auto [a, b] : arr) { - t += b; - ans = max(ans, t - a); + for (int i : idx) { + t += plantTime[i]; + ans = max(ans, t + growTime[i]); } return ans; } @@ -122,20 +121,19 @@ public: ### **Go** ```go -func earliestFullBloom(plantTime []int, growTime []int) int { - arr := [][]int{} - for i, a := range plantTime { - arr = append(arr, []int{a, growTime[i]}) +func earliestFullBloom(plantTime []int, growTime []int) (ans int) { + n := len(plantTime) + idx := make([]int, n) + for i := range idx { + idx[i] = i } - sort.Slice(arr, func(i, j int) bool { - return arr[i][1]> arr[j][1] - }) - ans, t := 0, 0 - for _, e := range arr { - t += e[0] - ans = max(ans, t+e[1]) + sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] }) + t := 0 + for _, i := range idx { + t += plantTime[i] + ans = max(ans, t+growTime[i]) } - return ans + return } func max(a, b int) int { @@ -149,7 +147,35 @@ func max(a, b int) int { ### **TypeScript** ```ts +function earliestFullBloom(plantTime: number[], growTime: number[]): number { + const n = plantTime.length; + const idx: number[] = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => growTime[j] - growTime[i]); + let [ans, t] = [0, 0]; + for (const i of idx) { + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); + } + return ans; +} +``` +### **Rust** + +```rust +impl Solution { + pub fn earliest_full_bloom(plant_time: Vec, grow_time: Vec) -> i32 { + let mut idx: Vec = (0..plant_time.len()).collect(); + idx.sort_by_key(|&i| -&grow_time[i]); + let mut ans = 0; + let mut t = 0; + for &i in &idx { + t += plant_time[i]; + ans = ans.max(t + grow_time[i]); + } + ans + } +} ``` ### **...** diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp index 210b009377625..c7c9ea1de6861 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp @@ -1,15 +1,15 @@ -class Solution { -public: - int earliestFullBloom(vector& plantTime, vector& growTime) { - int n = plantTime.size(); - vector> arr; - for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]}); - sort(arr.begin(), arr.end()); - int ans = 0, t = 0; - for (auto [a, b] : arr) { - t += b; - ans = max(ans, t - a); - } - return ans; - } +class Solution { +public: + int earliestFullBloom(vector& plantTime, vector& growTime) { + int n = plantTime.size(); + vector idx(n); + iota(idx.begin(), idx.end(), 0); + sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; }); + int ans = 0, t = 0; + for (int i : idx) { + t += plantTime[i]; + ans = max(ans, t + growTime[i]); + } + return ans; + } }; \ No newline at end of file diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.go b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.go index e13e29fdebdca..afb9d0f4a367e 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.go +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.go @@ -1,17 +1,16 @@ -func earliestFullBloom(plantTime []int, growTime []int) int { - arr := [][]int{} - for i, a := range plantTime { - arr = append(arr, []int{a, growTime[i]}) +func earliestFullBloom(plantTime []int, growTime []int) (ans int) { + n := len(plantTime) + idx := make([]int, n) + for i := range idx { + idx[i] = i } - sort.Slice(arr, func(i, j int) bool { - return arr[i][1]> arr[j][1] - }) - ans, t := 0, 0 - for _, e := range arr { - t += e[0] - ans = max(ans, t+e[1]) + sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] }) + t := 0 + for _, i := range idx { + t += plantTime[i] + ans = max(ans, t+growTime[i]) } - return ans + return } func max(a, b int) int { diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.java b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.java new file mode 100644 index 0000000000000..da7cba16a5f1f --- /dev/null +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int earliestFullBloom(int[] plantTime, int[] growTime) { + int n = plantTime.length; + Integer[] idx = new Integer[n]; + for (int i = 0; i < n; i++) { + idx[i] = i; + } + Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]); + int ans = 0, t = 0; + for (int i : idx) { + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.py b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.py index dfd5d4ca4a6b5..96e825f307eca 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.py +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.py @@ -1,7 +1,7 @@ -class Solution: - def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int: - ans = t = 0 - for a, b in sorted(zip(plantTime, growTime), key=lambda x: -x[1]): - t += a - ans = max(ans, t + b) - return ans +class Solution: + def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int: + ans = t = 0 + for pt, gt in sorted(zip(plantTime, growTime), key=lambda x: -x[1]): + t += pt + ans = max(ans, t + gt) + return ans diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs new file mode 100644 index 0000000000000..71f57a3caf484 --- /dev/null +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn earliest_full_bloom(plant_time: Vec, grow_time: Vec) -> i32 { + let mut idx: Vec = (0..plant_time.len()).collect(); + idx.sort_by_key(|&i| -&grow_time[i]); + let mut ans = 0; + let mut t = 0; + for &i in &idx { + t += plant_time[i]; + ans = ans.max(t + grow_time[i]); + } + ans + } +} \ No newline at end of file diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.ts b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.ts new file mode 100644 index 0000000000000..25afbebc1b2d3 --- /dev/null +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.ts @@ -0,0 +1,11 @@ +function earliestFullBloom(plantTime: number[], growTime: number[]): number { + const n = plantTime.length; + const idx: number[] = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => growTime[j] - growTime[i]); + let [ans, t] = [0, 0]; + for (const i of idx) { + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); + } + return ans; +}

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