You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2900-2999/2943.Maximize Area of Square Hole in Grid/README_EN.md
+130-3
Original file line number
Diff line number
Diff line change
@@ -93,30 +93,157 @@ Hence, the answer is 9.
93
93
94
94
## Solutions
95
95
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$.
0 commit comments