Skip to content

Commit 9ea4b3c

Browse files
authored
feat: add solutions to lc problems: No.2943,2944 (doocs#2022)
* No.2943.Maximize Area of Square Hole in Grid * No.2944.Minimum Number of Coins for Fruits
1 parent d72b849 commit 9ea4b3c

File tree

15 files changed

+670
-12
lines changed

15 files changed

+670
-12
lines changed

solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README.md

+130-3
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,161 @@
100100

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

103+
**方法一:排序**
104+
105+
题目实际上要我们找出数组中最长的连续递增子序列的长度,然后再加上 $1$。
106+
107+
我们定义一个函数 $f(nums)$,表示数组 $nums$ 中最长的连续递增子序列的长度。
108+
109+
对于数组 $nums$,我们先对其进行排序,然后遍历数组,如果当前元素 $nums[i]$ 等于前一个元素 $nums[i - 1]$ 加 $1$,则说明当前元素可以加入到连续递增子序列中,否则,说明当前元素不能加入到连续递增子序列中,我们需要重新开始计算连续递增子序列的长度。最后,我们返回连续递增子序列的长度加 $1$。
110+
111+
我们在求出 $hBars$ 和 $vBars$ 中最长的连续递增子序列的长度之后,我们取两者中的最小值作为正方形的边长,然后再求出正方形的面积即可。
112+
113+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $hBars$ 或 $vBars$ 的长度。
114+
103115
<!-- tabs:start -->
104116

105117
### **Python3**
106118

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

109121
```python
110-
122+
class Solution:
123+
def maximizeSquareHoleArea(
124+
self, n: int, m: int, hBars: List[int], vBars: List[int]
125+
) -> int:
126+
def f(nums: List[int]) -> int:
127+
nums.sort()
128+
ans = cnt = 1
129+
for i in range(1, len(nums)):
130+
if nums[i] == nums[i - 1] + 1:
131+
cnt += 1
132+
ans = max(ans, cnt)
133+
else:
134+
cnt = 1
135+
return ans + 1
136+
137+
return min(f(hBars), f(vBars)) ** 2
111138
```
112139

113140
### **Java**
114141

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

117144
```java
118-
145+
class Solution {
146+
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
147+
int x = Math.min(f(hBars), f(vBars));
148+
return x * x;
149+
}
150+
151+
private int f(int[] nums) {
152+
Arrays.sort(nums);
153+
int ans = 1, cnt = 1;
154+
for (int i = 1; i < nums.length; ++i) {
155+
if (nums[i] == nums[i - 1] + 1) {
156+
ans = Math.max(ans, ++cnt);
157+
} else {
158+
cnt = 1;
159+
}
160+
}
161+
return ans + 1;
162+
}
163+
}
119164
```
120165

121166
### **C++**
122167

123168
```cpp
124-
169+
class Solution {
170+
public:
171+
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
172+
auto f = [](vector<int>& nums) {
173+
int ans = 1, cnt = 1;
174+
sort(nums.begin(), nums.end());
175+
for (int i = 1; i < nums.size(); ++i) {
176+
if (nums[i] == nums[i - 1] + 1) {
177+
ans = max(ans, ++cnt);
178+
} else {
179+
cnt = 1;
180+
}
181+
}
182+
return ans + 1;
183+
};
184+
int x = min(f(hBars), f(vBars));
185+
return x * x;
186+
}
187+
};
125188
```
126189
127190
### **Go**
128191
129192
```go
193+
func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int {
194+
f := func(nums []int) int {
195+
sort.Ints(nums)
196+
ans, cnt := 1, 1
197+
for i, x := range nums[1:] {
198+
if x == nums[i]+1 {
199+
cnt++
200+
ans = max(ans, cnt)
201+
} else {
202+
cnt = 1
203+
}
204+
}
205+
return ans + 1
206+
}
207+
x := min(f(hBars), f(vBars))
208+
return x * x
209+
}
210+
```
211+
212+
### **TypeScript**
213+
214+
```ts
215+
function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number {
216+
const f = (nums: number[]): number => {
217+
nums.sort((a, b) => a - b);
218+
let [ans, cnt] = [1, 1];
219+
for (let i = 1; i < nums.length; ++i) {
220+
if (nums[i] === nums[i - 1] + 1) {
221+
ans = Math.max(ans, ++cnt);
222+
} else {
223+
cnt = 1;
224+
}
225+
}
226+
return ans + 1;
227+
};
228+
return Math.min(f(hBars), f(vBars)) ** 2;
229+
}
230+
```
130231

232+
### **Rust**
233+
234+
```rust
235+
impl Solution {
236+
pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec<i32>, v_bars: Vec<i32>) -> i32 {
237+
let f = |nums: &mut Vec<i32>| -> i32 {
238+
let mut ans = 1;
239+
let mut cnt = 1;
240+
nums.sort();
241+
for i in 1..nums.len() {
242+
if nums[i] == nums[i - 1] + 1 {
243+
cnt += 1;
244+
ans = ans.max(cnt);
245+
} else {
246+
cnt = 1;
247+
}
248+
}
249+
ans + 1
250+
};
251+
252+
let mut h_bars = h_bars;
253+
let mut v_bars = v_bars;
254+
let x = f(&mut h_bars).min(f(&mut v_bars));
255+
x * x
256+
}
257+
}
131258
```
132259

133260
### **...**

solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README_EN.md

+130-3
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,157 @@ Hence, the answer is 9.
9393

9494
## Solutions
9595

96+
**Solution 1: Sorting**
97+
98+
The problem essentially asks us to find the length of the longest consecutive increasing subsequence in the array, and then add 1 to it.
99+
100+
We define a function $f(nums)$, which represents the length of the longest consecutive increasing subsequence in the array $nums$.
101+
102+
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.
103+
104+
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.
105+
106+
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$.
107+
96108
<!-- tabs:start -->
97109

98110
### **Python3**
99111

100112
```python
101-
113+
class Solution:
114+
def maximizeSquareHoleArea(
115+
self, n: int, m: int, hBars: List[int], vBars: List[int]
116+
) -> int:
117+
def f(nums: List[int]) -> int:
118+
nums.sort()
119+
ans = cnt = 1
120+
for i in range(1, len(nums)):
121+
if nums[i] == nums[i - 1] + 1:
122+
cnt += 1
123+
ans = max(ans, cnt)
124+
else:
125+
cnt = 1
126+
return ans + 1
127+
128+
return min(f(hBars), f(vBars)) ** 2
102129
```
103130

104131
### **Java**
105132

106133
```java
107-
134+
class Solution {
135+
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
136+
int x = Math.min(f(hBars), f(vBars));
137+
return x * x;
138+
}
139+
140+
private int f(int[] nums) {
141+
Arrays.sort(nums);
142+
int ans = 1, cnt = 1;
143+
for (int i = 1; i < nums.length; ++i) {
144+
if (nums[i] == nums[i - 1] + 1) {
145+
ans = Math.max(ans, ++cnt);
146+
} else {
147+
cnt = 1;
148+
}
149+
}
150+
return ans + 1;
151+
}
152+
}
108153
```
109154

110155
### **C++**
111156

112157
```cpp
113-
158+
class Solution {
159+
public:
160+
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
161+
auto f = [](vector<int>& nums) {
162+
int ans = 1, cnt = 1;
163+
sort(nums.begin(), nums.end());
164+
for (int i = 1; i < nums.size(); ++i) {
165+
if (nums[i] == nums[i - 1] + 1) {
166+
ans = max(ans, ++cnt);
167+
} else {
168+
cnt = 1;
169+
}
170+
}
171+
return ans + 1;
172+
};
173+
int x = min(f(hBars), f(vBars));
174+
return x * x;
175+
}
176+
};
114177
```
115178
116179
### **Go**
117180
118181
```go
182+
func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int {
183+
f := func(nums []int) int {
184+
sort.Ints(nums)
185+
ans, cnt := 1, 1
186+
for i, x := range nums[1:] {
187+
if x == nums[i]+1 {
188+
cnt++
189+
ans = max(ans, cnt)
190+
} else {
191+
cnt = 1
192+
}
193+
}
194+
return ans + 1
195+
}
196+
x := min(f(hBars), f(vBars))
197+
return x * x
198+
}
199+
```
200+
201+
### **TypeScript**
202+
203+
```ts
204+
function maximizeSquareHoleArea(n: number, m: number, hBars: number[], vBars: number[]): number {
205+
const f = (nums: number[]): number => {
206+
nums.sort((a, b) => a - b);
207+
let [ans, cnt] = [1, 1];
208+
for (let i = 1; i < nums.length; ++i) {
209+
if (nums[i] === nums[i - 1] + 1) {
210+
ans = Math.max(ans, ++cnt);
211+
} else {
212+
cnt = 1;
213+
}
214+
}
215+
return ans + 1;
216+
};
217+
return Math.min(f(hBars), f(vBars)) ** 2;
218+
}
219+
```
119220

221+
### **Rust**
222+
223+
```rust
224+
impl Solution {
225+
pub fn maximize_square_hole_area(n: i32, m: i32, h_bars: Vec<i32>, v_bars: Vec<i32>) -> i32 {
226+
let f = |nums: &mut Vec<i32>| -> i32 {
227+
let mut ans = 1;
228+
let mut cnt = 1;
229+
nums.sort();
230+
for i in 1..nums.len() {
231+
if nums[i] == nums[i - 1] + 1 {
232+
cnt += 1;
233+
ans = ans.max(cnt);
234+
} else {
235+
cnt = 1;
236+
}
237+
}
238+
ans + 1
239+
};
240+
241+
let mut h_bars = h_bars;
242+
let mut v_bars = v_bars;
243+
let x = f(&mut h_bars).min(f(&mut v_bars));
244+
x * x
245+
}
246+
}
120247
```
121248

122249
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maximizeSquareHoleArea(int n, int m, vector<int>& hBars, vector<int>& vBars) {
4+
auto f = [](vector<int>& nums) {
5+
int ans = 1, cnt = 1;
6+
sort(nums.begin(), nums.end());
7+
for (int i = 1; i < nums.size(); ++i) {
8+
if (nums[i] == nums[i - 1] + 1) {
9+
ans = max(ans, ++cnt);
10+
} else {
11+
cnt = 1;
12+
}
13+
}
14+
return ans + 1;
15+
};
16+
int x = min(f(hBars), f(vBars));
17+
return x * x;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maximizeSquareHoleArea(n int, m int, hBars []int, vBars []int) int {
2+
f := func(nums []int) int {
3+
sort.Ints(nums)
4+
ans, cnt := 1, 1
5+
for i, x := range nums[1:] {
6+
if x == nums[i]+1 {
7+
cnt++
8+
ans = max(ans, cnt)
9+
} else {
10+
cnt = 1
11+
}
12+
}
13+
return ans + 1
14+
}
15+
x := min(f(hBars), f(vBars))
16+
return x * x
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
3+
int x = Math.min(f(hBars), f(vBars));
4+
return x * x;
5+
}
6+
7+
private int f(int[] nums) {
8+
Arrays.sort(nums);
9+
int ans = 1, cnt = 1;
10+
for (int i = 1; i < nums.length; ++i) {
11+
if (nums[i] == nums[i - 1] + 1) {
12+
ans = Math.max(ans, ++cnt);
13+
} else {
14+
cnt = 1;
15+
}
16+
}
17+
return ans + 1;
18+
}
19+
}

0 commit comments

Comments
 (0)