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 bccb294

Browse files
feat: add solutions to lc problem: No.1465 (doocs#1888)
1 parent b09e870 commit bccb294

File tree

7 files changed

+196
-52
lines changed

7 files changed

+196
-52
lines changed

‎solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README.md‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@
6363

6464
**方法一:排序**
6565

66-
先分别对 `horizontalCuts``verticalCuts` 排序,然后遍历数组,计算相邻两个元素的差值,取最大值的乘积即可
66+
我们先分别对 `horizontalCuts``verticalCuts` 排序,然后分别遍历两个数组,计算相邻两个元素的最大差值,分别记为 $x$ 和 $y,ドル最后返回 $x \times y$ 即可
6767

68-
时间复杂度 $O(m\log m \times n\log n)$。其中 $m$ 和 $n$ 分别为 `horizontalCuts``verticalCuts` 的长度。
68+
注意要考虑边界情况,即 `horizontalCuts``verticalCuts` 的首尾元素。
69+
70+
时间复杂度 $O(m\log m + n\log n),ドル空间复杂度 $(\log m + \log n)$。其中 $m$ 和 $n$ 分别为 `horizontalCuts``verticalCuts` 的长度。
6971

7072
<!-- tabs:start -->
7173

@@ -93,9 +95,8 @@ class Solution:
9395

9496
```java
9597
class Solution {
96-
private static final int MOD = (int) 1e9 + 7;
97-
9898
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
99+
final int mod = (int) 1e9 + 7;
99100
Arrays.sort(horizontalCuts);
100101
Arrays.sort(verticalCuts);
101102
int m = horizontalCuts.length;
@@ -108,7 +109,7 @@ class Solution {
108109
for (int i = 1; i < n; ++i) {
109110
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
110111
}
111-
return (int) ((x * y) % MOD);
112+
return (int) ((x * y) % mod);
112113
}
113114
}
114115
```
@@ -132,8 +133,8 @@ public:
132133
for (int i = 1; i < verticalCuts.size(); ++i) {
133134
y = max(y, verticalCuts[i] - verticalCuts[i - 1]);
134135
}
135-
int mod = 1e9 + 7;
136-
return (int) ((1ll * x * y) % mod);
136+
const int mod = 1e9 + 7;
137+
return (1ll * x * y) % mod;
137138
}
138139
};
139140
```
@@ -147,7 +148,7 @@ func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int {
147148
sort.Ints(horizontalCuts)
148149
sort.Ints(verticalCuts)
149150
x, y := 0, 0
150-
mod := int(1e9) + 7
151+
const mod int = 1e9 + 7
151152
for i := 1; i < len(horizontalCuts); i++ {
152153
x = max(x, horizontalCuts[i]-horizontalCuts[i-1])
153154
}
@@ -165,6 +166,55 @@ func max(a, b int) int {
165166
}
166167
```
167168

169+
### **TypeScript**
170+
171+
```ts
172+
function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: number[]): number {
173+
const mod = 1e9 + 7;
174+
horizontalCuts.push(0, h);
175+
verticalCuts.push(0, w);
176+
horizontalCuts.sort((a, b) => a - b);
177+
verticalCuts.sort((a, b) => a - b);
178+
let [x, y] = [0, 0];
179+
for (let i = 1; i < horizontalCuts.length; i++) {
180+
x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
181+
}
182+
for (let i = 1; i < verticalCuts.length; i++) {
183+
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
184+
}
185+
return Number((BigInt(x) * BigInt(y)) % BigInt(mod));
186+
}
187+
```
188+
189+
### **Rust**
190+
191+
```rust
192+
impl Solution {
193+
pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec<i32>, mut vertical_cuts: Vec<i32>) -> i32 {
194+
const MOD: i64 = 1_000_000_007;
195+
196+
horizontal_cuts.sort();
197+
vertical_cuts.sort();
198+
199+
let m = horizontal_cuts.len();
200+
let n = vertical_cuts.len();
201+
202+
let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64);
203+
let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64);
204+
205+
for i in 1..m {
206+
x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64);
207+
}
208+
209+
for i in 1..n {
210+
y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64);
211+
}
212+
213+
((x * y) % MOD) as i32
214+
}
215+
}
216+
```
217+
168218
### **...**
169219

170220
```

‎solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/README_EN.md‎

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252

5353
## Solutions
5454

55+
**Solution 1: Sorting**
56+
57+
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$.
58+
59+
Note that we need to consider the boundary cases, i.e., the first and last elements of `horizontalCuts` and `verticalCuts`.
60+
61+
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)$.
62+
5563
<!-- tabs:start -->
5664

5765
### **Python3**
@@ -74,9 +82,8 @@ class Solution:
7482

7583
```java
7684
class Solution {
77-
private static final int MOD = (int) 1e9 + 7;
78-
7985
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
86+
final int mod = (int) 1e9 + 7;
8087
Arrays.sort(horizontalCuts);
8188
Arrays.sort(verticalCuts);
8289
int m = horizontalCuts.length;
@@ -89,7 +96,7 @@ class Solution {
8996
for (int i = 1; i < n; ++i) {
9097
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
9198
}
92-
return (int) ((x * y) % MOD);
99+
return (int) ((x * y) % mod);
93100
}
94101
}
95102
```
@@ -113,8 +120,8 @@ public:
113120
for (int i = 1; i < verticalCuts.size(); ++i) {
114121
y = max(y, verticalCuts[i] - verticalCuts[i - 1]);
115122
}
116-
int mod = 1e9 + 7;
117-
return (int) ((1ll * x * y) % mod);
123+
const int mod = 1e9 + 7;
124+
return (1ll * x * y) % mod;
118125
}
119126
};
120127
```
@@ -128,7 +135,7 @@ func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int {
128135
sort.Ints(horizontalCuts)
129136
sort.Ints(verticalCuts)
130137
x, y := 0, 0
131-
mod := int(1e9) + 7
138+
const mod int = 1e9 + 7
132139
for i := 1; i < len(horizontalCuts); i++ {
133140
x = max(x, horizontalCuts[i]-horizontalCuts[i-1])
134141
}
@@ -146,6 +153,55 @@ func max(a, b int) int {
146153
}
147154
```
148155

156+
### **TypeScript**
157+
158+
```ts
159+
function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: number[]): number {
160+
const mod = 1e9 + 7;
161+
horizontalCuts.push(0, h);
162+
verticalCuts.push(0, w);
163+
horizontalCuts.sort((a, b) => a - b);
164+
verticalCuts.sort((a, b) => a - b);
165+
let [x, y] = [0, 0];
166+
for (let i = 1; i < horizontalCuts.length; i++) {
167+
x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
168+
}
169+
for (let i = 1; i < verticalCuts.length; i++) {
170+
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
171+
}
172+
return Number((BigInt(x) * BigInt(y)) % BigInt(mod));
173+
}
174+
```
175+
176+
### **Rust**
177+
178+
```rust
179+
impl Solution {
180+
pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec<i32>, mut vertical_cuts: Vec<i32>) -> i32 {
181+
const MOD: i64 = 1_000_000_007;
182+
183+
horizontal_cuts.sort();
184+
vertical_cuts.sort();
185+
186+
let m = horizontal_cuts.len();
187+
let n = vertical_cuts.len();
188+
189+
let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64);
190+
let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64);
191+
192+
for i in 1..m {
193+
x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64);
194+
}
195+
196+
for i in 1..n {
197+
y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64);
198+
}
199+
200+
((x * y) % MOD) as i32
201+
}
202+
}
203+
```
204+
149205
### **...**
150206

151207
```
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
class Solution {
2-
public:
3-
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
4-
horizontalCuts.push_back(0);
5-
horizontalCuts.push_back(h);
6-
verticalCuts.push_back(0);
7-
verticalCuts.push_back(w);
8-
sort(horizontalCuts.begin(), horizontalCuts.end());
9-
sort(verticalCuts.begin(), verticalCuts.end());
10-
int x = 0, y = 0;
11-
for (int i = 1; i < horizontalCuts.size(); ++i) {
12-
x = max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
13-
}
14-
for (int i = 1; i < verticalCuts.size(); ++i) {
15-
y = max(y, verticalCuts[i] - verticalCuts[i - 1]);
16-
}
17-
int mod = 1e9 + 7;
18-
return (int) ((1ll * x * y) % mod);
19-
}
1+
class Solution {
2+
public:
3+
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {
4+
horizontalCuts.push_back(0);
5+
horizontalCuts.push_back(h);
6+
verticalCuts.push_back(0);
7+
verticalCuts.push_back(w);
8+
sort(horizontalCuts.begin(), horizontalCuts.end());
9+
sort(verticalCuts.begin(), verticalCuts.end());
10+
int x = 0, y = 0;
11+
for (int i = 1; i < horizontalCuts.size(); ++i) {
12+
x = max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
13+
}
14+
for (int i = 1; i < verticalCuts.size(); ++i) {
15+
y = max(y, verticalCuts[i] - verticalCuts[i - 1]);
16+
}
17+
constint mod = 1e9 + 7;
18+
return (1ll * x * y) % mod;
19+
}
2020
};

‎solution/1400-1499/1465.Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts/Solution.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ func maxArea(h int, w int, horizontalCuts []int, verticalCuts []int) int {
44
sort.Ints(horizontalCuts)
55
sort.Ints(verticalCuts)
66
x, y := 0, 0
7-
mod :=int(1e9) + 7
7+
constmod int=1e9 + 7
88
for i := 1; i < len(horizontalCuts); i++ {
99
x = max(x, horizontalCuts[i]-horizontalCuts[i-1])
1010
}
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
class Solution {
2-
private static final int MOD = (int) 1e9 + 7;
3-
4-
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
5-
Arrays.sort(horizontalCuts);
6-
Arrays.sort(verticalCuts);
7-
int m = horizontalCuts.length;
8-
int n = verticalCuts.length;
9-
long x = Math.max(horizontalCuts[0], h - horizontalCuts[m - 1]);
10-
long y = Math.max(verticalCuts[0], w - verticalCuts[n - 1]);
11-
for (int i = 1; i < m; ++i) {
12-
x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
13-
}
14-
for (int i = 1; i < n; ++i) {
15-
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
16-
}
17-
return (int) ((x * y) % MOD);
18-
}
1+
class Solution {
2+
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) {
3+
final int mod = (int) 1e9 + 7;
4+
Arrays.sort(horizontalCuts);
5+
Arrays.sort(verticalCuts);
6+
int m = horizontalCuts.length;
7+
int n = verticalCuts.length;
8+
long x = Math.max(horizontalCuts[0], h - horizontalCuts[m - 1]);
9+
long y = Math.max(verticalCuts[0], w - verticalCuts[n - 1]);
10+
for (int i = 1; i < m; ++i) {
11+
x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
12+
}
13+
for (int i = 1; i < n; ++i) {
14+
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
15+
}
16+
return (int) ((x * y) % mod);
17+
}
1918
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec<i32>, mut vertical_cuts: Vec<i32>) -> i32 {
3+
const MOD: i64 = 1_000_000_007;
4+
5+
horizontal_cuts.sort();
6+
vertical_cuts.sort();
7+
8+
let m = horizontal_cuts.len();
9+
let n = vertical_cuts.len();
10+
11+
let mut x = i64::max(horizontal_cuts[0] as i64, h as i64 - horizontal_cuts[m - 1] as i64);
12+
let mut y = i64::max(vertical_cuts[0] as i64, w as i64 - vertical_cuts[n - 1] as i64);
13+
14+
for i in 1..m {
15+
x = i64::max(x, horizontal_cuts[i] as i64 - horizontal_cuts[i - 1] as i64);
16+
}
17+
18+
for i in 1..n {
19+
y = i64::max(y, vertical_cuts[i] as i64 - vertical_cuts[i - 1] as i64);
20+
}
21+
22+
((x * y) % MOD) as i32
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maxArea(h: number, w: number, horizontalCuts: number[], verticalCuts: number[]): number {
2+
const mod = 1e9 + 7;
3+
horizontalCuts.push(0, h);
4+
verticalCuts.push(0, w);
5+
horizontalCuts.sort((a, b) => a - b);
6+
verticalCuts.sort((a, b) => a - b);
7+
let [x, y] = [0, 0];
8+
for (let i = 1; i < horizontalCuts.length; i++) {
9+
x = Math.max(x, horizontalCuts[i] - horizontalCuts[i - 1]);
10+
}
11+
for (let i = 1; i < verticalCuts.length; i++) {
12+
y = Math.max(y, verticalCuts[i] - verticalCuts[i - 1]);
13+
}
14+
return Number((BigInt(x) * BigInt(y)) % BigInt(mod));
15+
}

0 commit comments

Comments
(0)

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