diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md index ae0fa7022b87f..71e1392fe00f5 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md @@ -63,9 +63,11 @@ **方法一:排序** -先分别对 `horizontalCuts` 和 `verticalCuts` 排序,然后遍历数组,计算相邻两个元素的差值,取最大值的乘积即可。 +我们先分别对 `horizontalCuts` 和 `verticalCuts` 排序,然后分别遍历两个数组,计算相邻两个元素的最大差值,分别记为 $x$ 和 $y$,最后返回 $x \times y$ 即可。 -时间复杂度 $O(m\log m \times n\log n)$。其中 $m$ 和 $n$ 分别为 `horizontalCuts` 和 `verticalCuts` 的长度。 +注意要考虑边界情况,即 `horizontalCuts` 和 `verticalCuts` 的首尾元素。 + +时间复杂度 $O(m\log m + n\log n)$,空间复杂度 $(\log m + \log n)$。其中 $m$ 和 $n$ 分别为 `horizontalCuts` 和 `verticalCuts` 的长度。 @@ -93,9 +95,8 @@ class Solution: ```java class Solution { - private static final int MOD = (int) 1e9 + 7; - public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { + final int mod = (int) 1e9 + 7; Arrays.sort(horizontalCuts); Arrays.sort(verticalCuts); int m = horizontalCuts.length; @@ -108,7 +109,7 @@ class Solution { for (int i = 1; i < n; ++i) { y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); } - return (int) ((x * y) % MOD); + return (int) ((x * y) % mod); } } ``` @@ -132,8 +133,8 @@ public: for (int i = 1; i < verticalCuts.size(); ++i) { y = max(y, verticalCuts[i] - verticalCuts[i - 1]); } - int mod = 1e9 + 7; - return (int) ((1ll * x * y) % mod); + const int mod = 1e9 + 7; + return (1ll * x * y) % mod; } }; ``` @@ -147,7 +148,7 @@ func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int { sort.Ints(horizontalCuts) sort.Ints(verticalCuts) x, y := 0, 0 - mod := int(1e9) + 7 + const mod int = 1e9 + 7 for i := 1; i < len(horizontalCuts); i++ { x = max(x, horizontalCuts[i]-horizontalCuts[i-1]) } @@ -165,6 +166,55 @@ func max(a, b int) int { } ``` +### **TypeScript** + +```ts +function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: number[]): number { + const mod = 1e9 + 7; + horizontalCuts.push(0, h); + verticalCuts.push(0, w); + horizontalCuts.sort((a, b) => a - b); + verticalCuts.sort((a, b) => a - b); + let [x, y] = [0, 0]; + for (let i = 1; i < horizontalCuts.length; i++) { + x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]); + } + for (let i = 1; i < verticalCuts.length; i++) { + y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); + } + return Number((BigInt(x) * BigInt(y)) % BigInt(mod)); +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec, mut vertical_cuts: Vec) -> i32 { + const MOD: i64 = 1_000_000_007; + + horizontal_cuts.sort(); + vertical_cuts.sort(); + + let m = horizontal_cuts.len(); + let n = vertical_cuts.len(); + + let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64); + let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64); + + for i in 1..m { + x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64); + } + + for i in 1..n { + y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64); + } + + ((x * y) % MOD) as i32 + } +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md index bcfeb18492bc1..21e0e0aa5aee5 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md @@ -52,6 +52,14 @@ ## Solutions +**Solution 1: Sorting** + +We first sort `horizontalCuts` and `verticalCuts` separately, and then traverse both arrays to calculate the maximum difference between adjacent elements. We denote these maximum differences as $x$ and $y$, respectively. Finally, we return $x \times y$. + +Note that we need to consider the boundary cases, i.e., the first and last elements of `horizontalCuts` and `verticalCuts`. + +The time complexity is $O(m\log m + n\log n)$, where $m$ and $n$ are the lengths of `horizontalCuts` and `verticalCuts`, respectively. The space complexity is $O(\log m + \log n)$. + ### **Python3** @@ -74,9 +82,8 @@ class Solution: ```java class Solution { - private static final int MOD = (int) 1e9 + 7; - public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { + final int mod = (int) 1e9 + 7; Arrays.sort(horizontalCuts); Arrays.sort(verticalCuts); int m = horizontalCuts.length; @@ -89,7 +96,7 @@ class Solution { for (int i = 1; i < n; ++i) { y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); } - return (int) ((x * y) % MOD); + return (int) ((x * y) % mod); } } ``` @@ -113,8 +120,8 @@ public: for (int i = 1; i < verticalCuts.size(); ++i) { y = max(y, verticalCuts[i] - verticalCuts[i - 1]); } - int mod = 1e9 + 7; - return (int) ((1ll * x * y) % mod); + const int mod = 1e9 + 7; + return (1ll * x * y) % mod; } }; ``` @@ -128,7 +135,7 @@ func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int { sort.Ints(horizontalCuts) sort.Ints(verticalCuts) x, y := 0, 0 - mod := int(1e9) + 7 + const mod int = 1e9 + 7 for i := 1; i < len(horizontalCuts); i++ { x = max(x, horizontalCuts[i]-horizontalCuts[i-1]) } @@ -146,6 +153,55 @@ func max(a, b int) int { } ``` +### **TypeScript** + +```ts +function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: number[]): number { + const mod = 1e9 + 7; + horizontalCuts.push(0, h); + verticalCuts.push(0, w); + horizontalCuts.sort((a, b) => a - b); + verticalCuts.sort((a, b) => a - b); + let [x, y] = [0, 0]; + for (let i = 1; i < horizontalCuts.length; i++) { + x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]); + } + for (let i = 1; i < verticalCuts.length; i++) { + y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); + } + return Number((BigInt(x) * BigInt(y)) % BigInt(mod)); +} +``` + +### **Rust** + +```rust +impl Solution { + pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec, mut vertical_cuts: Vec) -> i32 { + const MOD: i64 = 1_000_000_007; + + horizontal_cuts.sort(); + vertical_cuts.sort(); + + let m = horizontal_cuts.len(); + let n = vertical_cuts.len(); + + let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64); + let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64); + + for i in 1..m { + x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64); + } + + for i in 1..n { + y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64); + } + + ((x * y) % MOD) as i32 + } +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.cpp b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.cpp index 18219bdec6bc1..5a56dc123bbf2 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.cpp +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.cpp @@ -1,20 +1,20 @@ -class Solution { -public: - int maxArea(int h, int w, vector& horizontalCuts, vector& verticalCuts) { - horizontalCuts.push_back(0); - horizontalCuts.push_back(h); - verticalCuts.push_back(0); - verticalCuts.push_back(w); - sort(horizontalCuts.begin(), horizontalCuts.end()); - sort(verticalCuts.begin(), verticalCuts.end()); - int x = 0, y = 0; - for (int i = 1; i < horizontalCuts.size(); ++i) { - x = max(x, horizontalCuts[i] - horizontalCuts[i - 1]); - } - for (int i = 1; i < verticalCuts.size(); ++i) { - y = max(y, verticalCuts[i] - verticalCuts[i - 1]); - } - int mod = 1e9 + 7; - return (int) ((1ll * x * y) % mod); - } +class Solution { +public: + int maxArea(int h, int w, vector& horizontalCuts, vector& verticalCuts) { + horizontalCuts.push_back(0); + horizontalCuts.push_back(h); + verticalCuts.push_back(0); + verticalCuts.push_back(w); + sort(horizontalCuts.begin(), horizontalCuts.end()); + sort(verticalCuts.begin(), verticalCuts.end()); + int x = 0, y = 0; + for (int i = 1; i < horizontalCuts.size(); ++i) { + x = max(x, horizontalCuts[i] - horizontalCuts[i - 1]); + } + for (int i = 1; i < verticalCuts.size(); ++i) { + y = max(y, verticalCuts[i] - verticalCuts[i - 1]); + } + const int mod = 1e9 + 7; + return (1ll * x * y) % mod; + } }; \ No newline at end of file diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.go b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.go index 32e5ee7121711..62e557980306e 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.go +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.go @@ -4,7 +4,7 @@ func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int { sort.Ints(horizontalCuts) sort.Ints(verticalCuts) x, y := 0, 0 - mod := int(1e9) + 7 + const mod int = 1e9 + 7 for i := 1; i < len(horizontalCuts); i++ { x = max(x, horizontalCuts[i]-horizontalCuts[i-1]) } diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.java b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.java index 1d1a42ca5579f..ab1a5ea3ae124 100644 --- a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.java +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.java @@ -1,19 +1,18 @@ -class Solution { - private static final int MOD = (int) 1e9 + 7; - - public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { - Arrays.sort(horizontalCuts); - Arrays.sort(verticalCuts); - int m = horizontalCuts.length; - int n = verticalCuts.length; - long x = Math.max(horizontalCuts[0], h - horizontalCuts[m - 1]); - long y = Math.max(verticalCuts[0], w - verticalCuts[n - 1]); - for (int i = 1; i < m; ++i) { - x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]); - } - for (int i = 1; i < n; ++i) { - y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); - } - return (int) ((x * y) % MOD); - } +class Solution { + public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { + final int mod = (int) 1e9 + 7; + Arrays.sort(horizontalCuts); + Arrays.sort(verticalCuts); + int m = horizontalCuts.length; + int n = verticalCuts.length; + long x = Math.max(horizontalCuts[0], h - horizontalCuts[m - 1]); + long y = Math.max(verticalCuts[0], w - verticalCuts[n - 1]); + for (int i = 1; i < m; ++i) { + x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]); + } + for (int i = 1; i < n; ++i) { + y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); + } + return (int) ((x * y) % mod); + } } \ No newline at end of file diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs new file mode 100644 index 0000000000000..89da228f09fc6 --- /dev/null +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.rs @@ -0,0 +1,24 @@ +impl Solution { + pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec, mut vertical_cuts: Vec) -> i32 { + const MOD: i64 = 1_000_000_007; + + horizontal_cuts.sort(); + vertical_cuts.sort(); + + let m = horizontal_cuts.len(); + let n = vertical_cuts.len(); + + let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64); + let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64); + + for i in 1..m { + x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64); + } + + for i in 1..n { + y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64); + } + + ((x * y) % MOD) as i32 + } +} \ No newline at end of file diff --git a/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.ts b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.ts new file mode 100644 index 0000000000000..79a812cb520ed --- /dev/null +++ b/solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.ts @@ -0,0 +1,15 @@ +function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: number[]): number { + const mod = 1e9 + 7; + horizontalCuts.push(0, h); + verticalCuts.push(0, w); + horizontalCuts.sort((a, b) => a - b); + verticalCuts.sort((a, b) => a - b); + let [x, y] = [0, 0]; + for (let i = 1; i < horizontalCuts.length; i++) { + x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]); + } + for (let i = 1; i < verticalCuts.length; i++) { + y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]); + } + return Number((BigInt(x) * BigInt(y)) % BigInt(mod)); +}