Skip to content

Commit 63d54f7

Browse files
authored
feat: add solutions for lc No.3507 (#4969)
1 parent 21498cb commit 63d54f7

10 files changed

Lines changed: 593 additions & 17 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ tags:
119119

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

122-
我们定义一个函数 $f(nums)$,表示数组 $nums$ 中最长的连续递增子序列的长度。
122+
我们定义一个函数 $f(\textit{nums})$,表示数组 $\textit{nums}$ 中最长的连续递增子序列的长度。
123123

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

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

128-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $hBars$ 或 $vBars$ 的长度。
128+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{hBars}$ 或 $\textit{vBars}$ 的长度。
129129

130130
<!-- tabs:start -->
131131

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ tags:
9292

9393
### Solution 1: Sorting
9494

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

97-
We define a function $f(nums)$, which represents the length of the longest consecutive increasing subsequence in the array $nums$.
97+
We define a function $f(\textit{nums})$ to represent the length of the longest consecutive increasing subsequence in the array $\textit{nums}$.
9898

99-
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.
99+
For the array $\textit{nums}$, we first sort it, then iterate through the array. If the current element $\textit{nums}[i]$ equals the previous element $\textit{nums}[i - 1]$ plus $1$, it means the current element can be added to the consecutive increasing subsequence. Otherwise, the current element cannot be added to the consecutive increasing subsequence, and we need to restart counting the length of the consecutive increasing subsequence. Finally, we return the length of the consecutive increasing subsequence plus $1$.
100100

101-
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.
101+
After finding the lengths of the longest consecutive increasing subsequences in $\textit{hBars}$ and $\textit{vBars}$, we take the minimum of the two as the side length of the square, and then calculate the area of the square.
102102

103-
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$.
103+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{hBars}$ or $\textit{vBars}$.
104104

105105
<!-- tabs:start -->
106106

solution/3500-3599/3507.Minimum Pair Removal to Sort Array I/README.md

Lines changed: 200 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,228 @@ tags:
8181

8282
<!-- solution:start -->
8383

84-
### 方法一
84+
### 方法一:模拟
85+
86+
我们定义一个函数 $\text{is\_non\_decreasing}(a)$,用于判断数组 $a$ 是否为非递减数组。
87+
88+
我们使用一个循环,直到数组 $arr$ 为非递减数组为止。在每次循环中,我们找到数组 $arr$ 中相邻元素对的和的最小值,并记录该对的左边元素的下标 $k$。然后,我们将该对的和替换左边的元素,并删除右边的元素。最后,我们返回操作的次数。
89+
90+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
8591

8692
<!-- tabs:start -->
8793

8894
#### Python3
8995

9096
```python
91-
97+
class Solution:
98+
def minimumPairRemoval(self, nums: List[int]) -> int:
99+
arr = nums[:]
100+
ans = 0
101+
102+
def is_non_decreasing(a: List[int]) -> bool:
103+
for i in range(1, len(a)):
104+
if a[i] < a[i - 1]:
105+
return False
106+
return True
107+
108+
while not is_non_decreasing(arr):
109+
k = 0
110+
s = arr[0] + arr[1]
111+
for i in range(1, len(arr) - 1):
112+
t = arr[i] + arr[i + 1]
113+
if s > t:
114+
s = t
115+
k = i
116+
arr[k] = s
117+
arr.pop(k + 1)
118+
ans += 1
119+
return ans
92120
```
93121

94122
#### Java
95123

96124
```java
97-
125+
class Solution {
126+
public int minimumPairRemoval(int[] nums) {
127+
List<Integer> arr = new ArrayList<>();
128+
for (int x : nums) {
129+
arr.add(x);
130+
}
131+
int ans = 0;
132+
while (!isNonDecreasing(arr)) {
133+
int k = 0;
134+
int s = arr.get(0) + arr.get(1);
135+
for (int i = 1; i < arr.size() - 1; ++i) {
136+
int t = arr.get(i) + arr.get(i + 1);
137+
if (s > t) {
138+
s = t;
139+
k = i;
140+
}
141+
}
142+
arr.set(k, s);
143+
arr.remove(k + 1);
144+
++ans;
145+
}
146+
return ans;
147+
}
148+
149+
private boolean isNonDecreasing(List<Integer> arr) {
150+
for (int i = 1; i < arr.size(); ++i) {
151+
if (arr.get(i) < arr.get(i - 1)) {
152+
return false;
153+
}
154+
}
155+
return true;
156+
}
157+
}
98158
```
99159

100160
#### C++
101161

102162
```cpp
103-
163+
class Solution {
164+
public:
165+
int minimumPairRemoval(vector<int>& nums) {
166+
vector<int> arr = nums;
167+
int ans = 0;
168+
169+
while (!isNonDecreasing(arr)) {
170+
int k = 0;
171+
int s = arr[0] + arr[1];
172+
173+
for (int i = 1; i < arr.size() - 1; ++i) {
174+
int t = arr[i] + arr[i + 1];
175+
if (s > t) {
176+
s = t;
177+
k = i;
178+
}
179+
}
180+
181+
arr[k] = s;
182+
arr.erase(arr.begin() + (k + 1));
183+
++ans;
184+
}
185+
186+
return ans;
187+
}
188+
189+
private:
190+
bool isNonDecreasing(const vector<int>& arr) {
191+
for (int i = 1; i < (int) arr.size(); ++i) {
192+
if (arr[i] < arr[i - 1]) {
193+
return false;
194+
}
195+
}
196+
return true;
197+
}
198+
};
104199
```
105200
106201
#### Go
107202
108203
```go
204+
func minimumPairRemoval(nums []int) int {
205+
arr := append([]int(nil), nums...)
206+
ans := 0
207+
208+
isNonDecreasing := func(a []int) bool {
209+
for i := 1; i < len(a); i++ {
210+
if a[i] < a[i-1] {
211+
return false
212+
}
213+
}
214+
return true
215+
}
216+
217+
for !isNonDecreasing(arr) {
218+
k := 0
219+
s := arr[0] + arr[1]
220+
221+
for i := 1; i < len(arr)-1; i++ {
222+
t := arr[i] + arr[i+1]
223+
if s > t {
224+
s = t
225+
k = i
226+
}
227+
}
228+
229+
arr[k] = s
230+
copy(arr[k+1:], arr[k+2:])
231+
arr = arr[:len(arr)-1]
232+
ans++
233+
}
234+
235+
return ans
236+
}
237+
```
238+
239+
#### TypeScript
240+
241+
```ts
242+
function minimumPairRemoval(nums: number[]): number {
243+
const arr = nums.slice();
244+
let ans = 0;
245+
const isNonDecreasing = (a: number[]): boolean => {
246+
for (let i = 1; i < a.length; i++) {
247+
if (a[i] < a[i - 1]) {
248+
return false;
249+
}
250+
}
251+
return true;
252+
};
253+
while (!isNonDecreasing(arr)) {
254+
let k = 0;
255+
let s = arr[0] + arr[1];
256+
for (let i = 1; i < arr.length - 1; ++i) {
257+
const t = arr[i] + arr[i + 1];
258+
if (s > t) {
259+
s = t;
260+
k = i;
261+
}
262+
}
263+
arr[k] = s;
264+
arr.splice(k + 1, 1);
265+
ans++;
266+
}
267+
return ans;
268+
}
269+
```
109270

271+
#### Rust
272+
273+
```rust
274+
impl Solution {
275+
pub fn minimum_pair_removal(nums: Vec<i32>) -> i32 {
276+
let mut arr: Vec<i32> = nums.clone();
277+
let mut ans: i32 = 0;
278+
279+
fn is_non_decreasing(a: &Vec<i32>) -> bool {
280+
for i in 1..a.len() {
281+
if a[i] < a[i - 1] {
282+
return false;
283+
}
284+
}
285+
true
286+
}
287+
288+
while !is_non_decreasing(&arr) {
289+
let mut k: usize = 0;
290+
let mut s: i32 = arr[0] + arr[1];
291+
for i in 1..arr.len() - 1 {
292+
let t: i32 = arr[i] + arr[i + 1];
293+
if s > t {
294+
s = t;
295+
k = i;
296+
}
297+
}
298+
arr[k] = s;
299+
arr.remove(k + 1);
300+
ans += 1;
301+
}
302+
303+
ans
304+
}
305+
}
110306
```
111307

112308
<!-- tabs:end -->

0 commit comments

Comments
 (0)