Skip to content

Commit ad034a8

Browse files
authored
feat: add solutions to lc problems: No.0962~0964 (#4920)
1 parent 368986b commit ad034a8

File tree

7 files changed

+37
-114
lines changed

7 files changed

+37
-114
lines changed

solution/0900-0999/0962.Maximum Width Ramp/README.md

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ tags:
6060

6161
### 方法一:单调栈
6262

63-
根据题意,我们可以发现,所有可能的 $nums[i]$ 所构成的子序列一定是单调递减的。为什么呢?我们不妨用反证法证明一下。
63+
根据题意,我们可以发现,所有可能的 $\textit{nums}[i]$ 所构成的子序列一定是单调递减的。为什么呢?我们不妨用反证法证明一下。
6464

65-
假设存在 $i_1<i_2$,并且 $nums[i_1]<=nums[i_2]$,那么实际上 $nums[i_2]$一定不可能是一个候选值,因为 $nums[i_1]$ 更靠左,会是一个更优的值。因此 $nums[i]$ 所构成的子序列一定单调递减,并且 $i$ 一定是从 0 开始。
65+
假设存在 $i_1<i_2$,并且 $\textit{nums}[i_1]\leq\textit{nums}[i_2]$,那么实际上 $\textit{nums}[i_2]$ 一定不可能是一个候选值,因为 $\textit{nums}[i_1]$ 更靠左,会是一个更优的值。因此 $\textit{nums}[i]$ 所构成的子序列一定单调递减,并且 $i$ 一定是从 0 开始。
6666

67-
我们用一个从栈底到栈顶单调递减的栈 $stk$ 来存储所有可能的 $nums[i]$,然后我们从右边界开始遍历 $j$,若遇到 $nums[stk.top()]<=nums[j]$,说明此时构成一个坡,循环弹出栈顶元素,更新 ans。
67+
我们用一个从栈底到栈顶单调递减的栈 $\textit{stk}$ 来存储所有可能的 $\textit{nums}[i]$,然后我们从右边界开始遍历 $j$,若遇到 $\textit{nums}[\textit{stk.top()}]\leq\textit{nums}[j]$,说明此时构成一个坡,循环弹出栈顶元素,更新 $\textit{ans}$
6868

69-
时间复杂度 $O(n)$,其中 $n$ 表示 $nums$ 的长度。
69+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 表示 $\textit{nums}$ 的长度。
7070

7171
<!-- tabs:start -->
7272

@@ -215,46 +215,4 @@ function maxWidthRamp(nums) {
215215

216216
<!-- solution:end -->
217217

218-
<!-- solution:start -->
219-
220-
### 方法二:排序
221-
222-
<!-- tabs:start -->
223-
224-
#### TypeScript
225-
226-
```ts
227-
function maxWidthRamp(nums: number[]): number {
228-
const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b);
229-
let [ans, j] = [0, nums.length];
230-
231-
for (const [_, i] of idx) {
232-
ans = Math.max(ans, i - j);
233-
j = Math.min(j, i);
234-
}
235-
236-
return ans;
237-
}
238-
```
239-
240-
#### JavaScript
241-
242-
```js
243-
function maxWidthRamp(nums) {
244-
const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b);
245-
let [ans, j] = [0, nums.length];
246-
247-
for (const [_, i] of idx) {
248-
ans = Math.max(ans, i - j);
249-
j = Math.min(j, i);
250-
}
251-
252-
return ans;
253-
}
254-
```
255-
256-
<!-- tabs:end -->
257-
258-
<!-- solution:end -->
259-
260218
<!-- problem:end -->

solution/0900-0999/0962.Maximum Width Ramp/README_EN.md

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ tags:
5454

5555
<!-- solution:start -->
5656

57-
### Solution 1: Monotonic stack
57+
### Solution 1: Monotonic Stack
58+
59+
According to the problem, we can find that the subsequence formed by all possible $\textit{nums}[i]$ must be monotonically decreasing. Why is that? Let's prove it by contradiction.
60+
61+
Suppose there exist $i_1<i_2$ and $\textit{nums}[i_1]\leq\textit{nums}[i_2]$, then actually $\textit{nums}[i_2]$ cannot be a candidate value, because $\textit{nums}[i_1]$ is more to the left and would be a better value. Therefore, the subsequence formed by $\textit{nums}[i]$ must be monotonically decreasing, and $i$ must start from 0.
62+
63+
We use a monotonically decreasing stack $\textit{stk}$ (from bottom to top) to store all possible $\textit{nums}[i]$. Then we traverse $j$ starting from the right boundary. If we encounter $\textit{nums}[\textit{stk.top()}]\leq\textit{nums}[j]$, it means a ramp is formed at this point. We cyclically pop the top elements from the stack and update $\textit{ans}$.
64+
65+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ represents the length of $\textit{nums}$.
5866

5967
<!-- tabs:start -->
6068

@@ -203,46 +211,4 @@ function maxWidthRamp(nums) {
203211

204212
<!-- solution:end -->
205213

206-
<!-- solution:start -->
207-
208-
### Solution 2: Sorting
209-
210-
<!-- tabs:start -->
211-
212-
#### TypeScript
213-
214-
```ts
215-
function maxWidthRamp(nums: number[]): number {
216-
const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b);
217-
let [ans, j] = [0, nums.length];
218-
219-
for (const [_, i] of idx) {
220-
ans = Math.max(ans, i - j);
221-
j = Math.min(j, i);
222-
}
223-
224-
return ans;
225-
}
226-
```
227-
228-
#### JavaScript
229-
230-
```js
231-
function maxWidthRamp(nums) {
232-
const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b);
233-
let [ans, j] = [0, nums.length];
234-
235-
for (const [_, i] of idx) {
236-
ans = Math.max(ans, i - j);
237-
j = Math.min(j, i);
238-
}
239-
240-
return ans;
241-
}
242-
```
243-
244-
<!-- tabs:end -->
245-
246-
<!-- solution:end -->
247-
248214
<!-- problem:end -->

solution/0900-0999/0962.Maximum Width Ramp/Solution2.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

solution/0900-0999/0962.Maximum Width Ramp/Solution2.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

solution/0900-0999/0963.Minimum Area Rectangle II/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ tags:
7474

7575
最后,如果找到满足条件的矩形,返回其中面积的最小值。否则,返回 $0$。
7676

77-
时间复杂度 $O(n^3)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $points$ 的长度。
77+
时间复杂度 $O(n^3)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{points}$ 的长度。
7878

7979
<!-- tabs:start -->
8080

solution/0900-0999/0963.Minimum Area Rectangle II/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Hash Table + Enumeration
70+
71+
We use a hash table to store all the points, then enumerate three points $p_1 = (x_1, y_1)$, $p_2 = (x_2, y_2)$, $p_3 = (x_3, y_3)$, where $p_2$ and $p_3$ are the two endpoints of the diagonal of the rectangle. If the line formed by $p_1$ and $p_2$ and the line formed by $p_1$ and $p_3$ are perpendicular, and the fourth point $(x_4, y_4)=(x_2 - x_1 + x_3, y_2 - y_1 + y_3)$ exists in the hash table, then we have found a rectangle. At this point, we can calculate the area of the rectangle and update the answer.
72+
73+
Finally, if a rectangle that satisfies the conditions is found, return the minimum area among them. Otherwise, return $0$.
74+
75+
The time complexity is $O(n^3)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{points}$.
7076

7177
<!-- tabs:start -->
7278

solution/0900-0999/0964.Least Operators to Express Number/README_EN.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,22 @@ The expression contains 3 operations.
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Memoization Search
77+
78+
We define a function $dfs(v)$, which represents the minimum number of operators needed to compose the number $v$ using $x$. Then the answer is $dfs(target)$.
79+
80+
The execution logic of function $dfs(v)$ is as follows:
81+
82+
If $x \geq v$, then we can obtain $v$ by adding $v$ instances of $x / x$, with the number of operators being $v \times 2 - 1$; or we can obtain $v$ by subtracting $(x - v)$ instances of $x / x$ from $x$, with the number of operators being $(x - v) \times 2$. We take the minimum of the two.
83+
84+
Otherwise, we enumerate $x^k$ starting from $k=2$ to find the first $k$ where $x^k \geq v$:
85+
86+
- If $x^k - v \geq v$ at this point, then we can only first obtain $x^{k-1}$, then recursively calculate $dfs(v - x^{k-1})$. The number of operators in this case is $k - 1 + dfs(v - x^{k-1})$;
87+
- If $x^k - v < v$ at this point, then we can obtain $v$ in the above manner, with the number of operators being $k - 1 + dfs(v - x^{k-1})$; or we can first obtain $x^k$, then recursively calculate $dfs(x^k - v)$, with the number of operators being $k + dfs(x^k - v)$. We take the minimum of the two.
88+
89+
To avoid redundant calculations, we implement the $dfs$ function using memoization search.
90+
91+
The time complexity is $O(\log_{x}{target})$ and the space complexity is $O(\log_{x}{target})$.
7792

7893
<!-- tabs:start -->
7994

0 commit comments

Comments
 (0)