Skip to content
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

feat: add solutions to lc problems: No.2943,2944 #2022

Merged
merged 2 commits into from
Nov 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
133 changes: 130 additions & 3 deletions solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,161 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:排序**

题目实际上要我们找出数组中最长的连续递增子序列的长度,然后再加上 $1$。

我们定义一个函数 $f(nums)$,表示数组 $nums$ 中最长的连续递增子序列的长度。

对于数组 $nums$,我们先对其进行排序,然后遍历数组,如果当前元素 $nums[i]$ 等于前一个元素 $nums[i - 1]$ 加 $1$,则说明当前元素可以加入到连续递增子序列中,否则,说明当前元素不能加入到连续递增子序列中,我们需要重新开始计算连续递增子序列的长度。最后,我们返回连续递增子序列的长度加 $1$。

我们在求出 $hBars$ 和 $vBars$ 中最长的连续递增子序列的长度之后,我们取两者中的最小值作为正方形的边长,然后再求出正方形的面积即可。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $hBars$ 或 $vBars$ 的长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def maximizeSquareHoleArea(
self, n: int, m: int, hBars: List[int], vBars: List[int]
) -> int:
def f(nums: List[int]) -> int:
nums.sort()
ans = cnt = 1
for i in range(1, len(nums)):
if nums[i] == nums[i - 1] + 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 1
return ans + 1

return min(f(hBars), f(vBars)) ** 2
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
int x = Math.min(f(hBars), f(vBars));
return x * x;
}

private int f(int[] nums) {
Arrays.sort(nums);
int ans = 1, cnt = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] == nums[i - 1] + 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
}
}
```

### **C++**

```cpp

class Solution {
public:
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
auto f = [](vector<int>& nums) {
int ans = 1, cnt = 1;
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1] + 1) {
ans = max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
};
int x = min(f(hBars), f(vBars));
return x * x;
}
};
```

### **Go**

```go
func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int {
f := func(nums []int) int {
sort.Ints(nums)
ans, cnt := 1, 1
for i, x := range nums[1:] {
if x == nums[i]+1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 1
}
}
return ans + 1
}
x := min(f(hBars), f(vBars))
return x * x
}
```

### **TypeScript**

```ts
function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number {
const f = (nums: number[]): number => {
nums.sort((a, b) => a - b);
let [ans, cnt] = [1, 1];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] === nums[i - 1] + 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
};
return Math.min(f(hBars), f(vBars)) ** 2;
}
```

### **Rust**

```rust
impl Solution {
pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec<i32>, v_bars: Vec<i32>) -> i32 {
let f = |nums: &mut Vec<i32>| -> i32 {
let mut ans = 1;
let mut cnt = 1;
nums.sort();
for i in 1..nums.len() {
if nums[i] == nums[i - 1] + 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 1;
}
}
ans + 1
};

let mut h_bars = h_bars;
let mut v_bars = v_bars;
let x = f(&mut h_bars).min(f(&mut v_bars));
x * x
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,157 @@ Hence, the answer is 9.

## Solutions

**Solution 1: Sorting**

The problem essentially asks us to find the length of the longest consecutive increasing subsequence in the array, and then add 1 to it.

We define a function $f(nums)$, which represents the length of the longest consecutive increasing subsequence in the array $nums$.

For the array $nums$, we first sort it, then traverse the array. If the current element $nums[i]$ equals the previous element $nums[i - 1]$ plus 1, it means that the current element can be added to the consecutive increasing subsequence. Otherwise, it means that the current element cannot be added to the consecutive increasing subsequence, and we need to start calculating the length of the consecutive increasing subsequence again. Finally, we return the length of the consecutive increasing subsequence plus 1.

After finding the length of the longest consecutive increasing subsequence in $hBars$ and $vBars$, we take the minimum of the two as the side length of the square, and then calculate the area of the square.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $hBars$ or $vBars$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def maximizeSquareHoleArea(
self, n: int, m: int, hBars: List[int], vBars: List[int]
) -> int:
def f(nums: List[int]) -> int:
nums.sort()
ans = cnt = 1
for i in range(1, len(nums)):
if nums[i] == nums[i - 1] + 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 1
return ans + 1

return min(f(hBars), f(vBars)) ** 2
```

### **Java**

```java

class Solution {
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
int x = Math.min(f(hBars), f(vBars));
return x * x;
}

private int f(int[] nums) {
Arrays.sort(nums);
int ans = 1, cnt = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] == nums[i - 1] + 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
}
}
```

### **C++**

```cpp

class Solution {
public:
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
auto f = [](vector<int>& nums) {
int ans = 1, cnt = 1;
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1] + 1) {
ans = max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
};
int x = min(f(hBars), f(vBars));
return x * x;
}
};
```

### **Go**

```go
func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int {
f := func(nums []int) int {
sort.Ints(nums)
ans, cnt := 1, 1
for i, x := range nums[1:] {
if x == nums[i]+1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 1
}
}
return ans + 1
}
x := min(f(hBars), f(vBars))
return x * x
}
```

### **TypeScript**

```ts
function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number {
const f = (nums: number[]): number => {
nums.sort((a, b) => a - b);
let [ans, cnt] = [1, 1];
for (let i = 1; i < nums.length; ++i) {
if (nums[i] === nums[i - 1] + 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
};
return Math.min(f(hBars), f(vBars)) ** 2;
}
```

### **Rust**

```rust
impl Solution {
pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec<i32>, v_bars: Vec<i32>) -> i32 {
let f = |nums: &mut Vec<i32>| -> i32 {
let mut ans = 1;
let mut cnt = 1;
nums.sort();
for i in 1..nums.len() {
if nums[i] == nums[i - 1] + 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
cnt = 1;
}
}
ans + 1
};

let mut h_bars = h_bars;
let mut v_bars = v_bars;
let x = f(&mut h_bars).min(f(&mut v_bars));
x * x
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
auto f = [](vector<int>& nums) {
int ans = 1, cnt = 1;
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] == nums[i - 1] + 1) {
ans = max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
};
int x = min(f(hBars), f(vBars));
return x * x;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int {
f := func(nums []int) int {
sort.Ints(nums)
ans, cnt := 1, 1
for i, x := range nums[1:] {
if x == nums[i]+1 {
cnt++
ans = max(ans, cnt)
} else {
cnt = 1
}
}
return ans + 1
}
x := min(f(hBars), f(vBars))
return x * x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
int x = Math.min(f(hBars), f(vBars));
return x * x;
}

private int f(int[] nums) {
Arrays.sort(nums);
int ans = 1, cnt = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] == nums[i - 1] + 1) {
ans = Math.max(ans, ++cnt);
} else {
cnt = 1;
}
}
return ans + 1;
}
}
Loading