Skip to content

feat: add solutions to lc problem: No.1465 #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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` 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
```
Expand All @@ -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;
}
};
```
Expand All @@ -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])
}
Expand All @@ -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<i32>, mut vertical_cuts: Vec<i32>) -> 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
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -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;
Expand All @@ -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);
}
}
```
Expand All @@ -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;
}
};
```
Expand All @@ -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])
}
Expand All @@ -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<i32>, mut vertical_cuts: Vec<i32>) -> 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
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Solution {
public:
int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& 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<int>& horizontalCuts, vector<int>& 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;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
impl Solution {
pub fn max_area(h: i32, w: i32, mut horizontal_cuts: Vec<i32>, mut vertical_cuts: Vec<i32>) -> 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
}
}
Original file line number Diff line number Diff line change
@@ -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));
}