From 39076b0125304074f1be5dbe3104161d75b914f0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: 2023年9月27日 19:43:50 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2136 No.2136.Earliest Possible Day of Full Bloom --- .../README.md | 96 ++++++++++++------- .../README_EN.md | 90 +++++++++++------ .../Solution.cpp | 29 +++--- .../Solution.go | 24 ++--- .../Solution.java | 17 ++++ .../Solution.py | 14 +-- .../Solution.rs | 13 +++ .../Solution.ts | 12 +++ 8 files changed, 199 insertions(+), 96 deletions(-) create mode 100644 solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.java create mode 100644 solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.rs create mode 100644 solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.ts 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..5cb75d8581cd5 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,16 @@ 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) { + int pt = plantTime[i], gt = growTime[i]; + t += pt; + ans = Math.max(ans, t + gt); } return ans; } @@ -117,13 +119,14 @@ 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) { + int pt = plantTime[i], gt = growTime[i]; + t += pt; + ans = max(ans, t + gt); } return ans; } @@ -133,20 +136,20 @@ 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 { + pt, gt := plantTime[i], growTime[i] + t += pt + ans = max(ans, t+gt) } - return ans + return } func max(a, b int) int { @@ -159,10 +162,37 @@ 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) { + const [pt, gt] = [plantTime[i], growTime[i]]; + t += pt; + ans = Math.max(ans, t + gt); + } + 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..ff8be482c464c 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,16 @@ 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) { + int pt = plantTime[i], gt = growTime[i]; + t += pt; + ans = Math.max(ans, t + gt); } return ans; } @@ -106,13 +106,14 @@ 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) { + int pt = plantTime[i], gt = growTime[i]; + t += pt; + ans = max(ans, t + gt); } return ans; } @@ -122,20 +123,20 @@ 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 { + pt, gt := plantTime[i], growTime[i] + t += pt + ans = max(ans, t+gt) } - return ans + return } func max(a, b int) int { @@ -149,7 +150,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) { + const [pt, gt] = [plantTime[i], growTime[i]]; + t += pt; + ans = Math.max(ans, t + gt); + } + 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..ea176885504b4 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,16 @@ -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) { + int pt = plantTime[i], gt = growTime[i]; + t += pt; + ans = max(ans, t + gt); + } + 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..e9a8fb6e52a07 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,17 @@ -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 { + pt, gt := plantTime[i], growTime[i] + t += pt + ans = max(ans, t+gt) } - 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..c867512abd860 --- /dev/null +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.java @@ -0,0 +1,17 @@ +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) { + int pt = plantTime[i], gt = growTime[i]; + t += pt; + ans = Math.max(ans, t + gt); + } + 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..4f6c78bfe95f1 --- /dev/null +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.ts @@ -0,0 +1,12 @@ +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) { + const [pt, gt] = [plantTime[i], growTime[i]]; + t += pt; + ans = Math.max(ans, t + gt); + } + return ans; +} From 361d7146747ba0b575bbf2f9ef1a17dfcc9057f8 Mon Sep 17 00:00:00 2001 From: yanglbme Date: 2023年9月27日 19:50:12 +0800 Subject: [PATCH 2/2] feat: update --- .../README.md | 20 ++++++++----------- .../README_EN.md | 20 ++++++++----------- .../Solution.cpp | 5 ++--- .../Solution.go | 5 ++--- .../Solution.java | 5 ++--- .../Solution.ts | 5 ++--- 6 files changed, 24 insertions(+), 36 deletions(-) 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 5cb75d8581cd5..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 @@ -103,9 +103,8 @@ class Solution { Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]); int ans = 0, t = 0; for (int i : idx) { - int pt = plantTime[i], gt = growTime[i]; - t += pt; - ans = Math.max(ans, t + gt); + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return ans; } @@ -124,9 +123,8 @@ public: sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; }); int ans = 0, t = 0; for (int i : idx) { - int pt = plantTime[i], gt = growTime[i]; - t += pt; - ans = max(ans, t + gt); + t += plantTime[i]; + ans = max(ans, t + growTime[i]); } return ans; } @@ -145,9 +143,8 @@ func earliestFullBloom(plantTime []int, growTime []int) (ans int) { sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] }) t := 0 for _, i := range idx { - pt, gt := plantTime[i], growTime[i] - t += pt - ans = max(ans, t+gt) + t += plantTime[i] + ans = max(ans, t+growTime[i]) } return } @@ -169,9 +166,8 @@ function earliestFullBloom(plantTime: number[], growTime: number[]): number { idx.sort((i, j) => growTime[j] - growTime[i]); let [ans, t] = [0, 0]; for (const i of idx) { - const [pt, gt] = [plantTime[i], growTime[i]]; - t += pt; - ans = Math.max(ans, t + gt); + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return 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 ff8be482c464c..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 @@ -90,9 +90,8 @@ class Solution { Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]); int ans = 0, t = 0; for (int i : idx) { - int pt = plantTime[i], gt = growTime[i]; - t += pt; - ans = Math.max(ans, t + gt); + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return ans; } @@ -111,9 +110,8 @@ public: sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; }); int ans = 0, t = 0; for (int i : idx) { - int pt = plantTime[i], gt = growTime[i]; - t += pt; - ans = max(ans, t + gt); + t += plantTime[i]; + ans = max(ans, t + growTime[i]); } return ans; } @@ -132,9 +130,8 @@ func earliestFullBloom(plantTime []int, growTime []int) (ans int) { sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] }) t := 0 for _, i := range idx { - pt, gt := plantTime[i], growTime[i] - t += pt - ans = max(ans, t+gt) + t += plantTime[i] + ans = max(ans, t+growTime[i]) } return } @@ -156,9 +153,8 @@ function earliestFullBloom(plantTime: number[], growTime: number[]): number { idx.sort((i, j) => growTime[j] - growTime[i]); let [ans, t] = [0, 0]; for (const i of idx) { - const [pt, gt] = [plantTime[i], growTime[i]]; - t += pt; - ans = Math.max(ans, t + gt); + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return 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 ea176885504b4..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 @@ -7,9 +7,8 @@ class Solution { sort(idx.begin(), idx.end(), [&](int i, int j) { return growTime[j] < growTime[i]; }); int ans = 0, t = 0; for (int i : idx) { - int pt = plantTime[i], gt = growTime[i]; - t += pt; - ans = max(ans, t + gt); + t += plantTime[i]; + ans = max(ans, t + growTime[i]); } return ans; } 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 e9a8fb6e52a07..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 @@ -7,9 +7,8 @@ func earliestFullBloom(plantTime []int, growTime []int) (ans int) { sort.Slice(idx, func(i, j int) bool { return growTime[idx[j]] < growTime[idx[i]] }) t := 0 for _, i := range idx { - pt, gt := plantTime[i], growTime[i] - t += pt - ans = max(ans, t+gt) + t += plantTime[i] + ans = max(ans, t+growTime[i]) } return } 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 index c867512abd860..da7cba16a5f1f 100644 --- 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 @@ -8,9 +8,8 @@ public int earliestFullBloom(int[] plantTime, int[] growTime) { Arrays.sort(idx, (i, j) -> growTime[j] - growTime[i]); int ans = 0, t = 0; for (int i : idx) { - int pt = plantTime[i], gt = growTime[i]; - t += pt; - ans = Math.max(ans, t + gt); + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return ans; } 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 index 4f6c78bfe95f1..25afbebc1b2d3 100644 --- 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 @@ -4,9 +4,8 @@ function earliestFullBloom(plantTime: number[], growTime: number[]): number { idx.sort((i, j) => growTime[j] - growTime[i]); let [ans, t] = [0, 0]; for (const i of idx) { - const [pt, gt] = [plantTime[i], growTime[i]]; - t += pt; - ans = Math.max(ans, t + gt); + t += plantTime[i]; + ans = Math.max(ans, t + growTime[i]); } return ans; }

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