From a3b96ebff18838f1bb0c590cf7850be832c6cb3a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 22 Dec 2025 07:31:05 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.0962~0964 --- .../0962.Maximum Width Ramp/README.md | 50 ++---------------- .../0962.Maximum Width Ramp/README_EN.md | 52 ++++--------------- .../0962.Maximum Width Ramp/Solution2.js | 11 ---- .../0962.Maximum Width Ramp/Solution2.ts | 11 ---- .../0963.Minimum Area Rectangle II/README.md | 2 +- .../README_EN.md | 8 ++- .../README_EN.md | 17 +++++- 7 files changed, 37 insertions(+), 114 deletions(-) delete mode 100644 solution/0900-0999/0962.Maximum Width Ramp/Solution2.js delete mode 100644 solution/0900-0999/0962.Maximum Width Ramp/Solution2.ts diff --git a/solution/0900-0999/0962.Maximum Width Ramp/README.md b/solution/0900-0999/0962.Maximum Width Ramp/README.md index 4ebfbb68b9ccc..913a9417937be 100644 --- a/solution/0900-0999/0962.Maximum Width Ramp/README.md +++ b/solution/0900-0999/0962.Maximum Width Ramp/README.md @@ -60,13 +60,13 @@ tags: ### 方法一:单调栈 -根据题意,我们可以发现,所有可能的 $nums[i]$ 所构成的子序列一定是单调递减的。为什么呢?我们不妨用反证法证明一下。 +根据题意,我们可以发现,所有可能的 $\textit{nums}[i]$ 所构成的子序列一定是单调递减的。为什么呢?我们不妨用反证法证明一下。 -假设存在 $i_1 @@ -215,46 +215,4 @@ function maxWidthRamp(nums) { - - -### 方法二:排序 - - - -#### TypeScript - -```ts -function maxWidthRamp(nums: number[]): number { - const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b); - let [ans, j] = [0, nums.length]; - - for (const [_, i] of idx) { - ans = Math.max(ans, i - j); - j = Math.min(j, i); - } - - return ans; -} -``` - -#### JavaScript - -```js -function maxWidthRamp(nums) { - const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b); - let [ans, j] = [0, nums.length]; - - for (const [_, i] of idx) { - ans = Math.max(ans, i - j); - j = Math.min(j, i); - } - - return ans; -} -``` - - - - - diff --git a/solution/0900-0999/0962.Maximum Width Ramp/README_EN.md b/solution/0900-0999/0962.Maximum Width Ramp/README_EN.md index 3293b93f9dc8e..ede22099609b2 100644 --- a/solution/0900-0999/0962.Maximum Width Ramp/README_EN.md +++ b/solution/0900-0999/0962.Maximum Width Ramp/README_EN.md @@ -54,7 +54,15 @@ tags: -### Solution 1: Monotonic stack +### Solution 1: Monotonic Stack + +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. + +Suppose there exist $i_1 @@ -203,46 +211,4 @@ function maxWidthRamp(nums) { - - -### Solution 2: Sorting - - - -#### TypeScript - -```ts -function maxWidthRamp(nums: number[]): number { - const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b); - let [ans, j] = [0, nums.length]; - - for (const [_, i] of idx) { - ans = Math.max(ans, i - j); - j = Math.min(j, i); - } - - return ans; -} -``` - -#### JavaScript - -```js -function maxWidthRamp(nums) { - const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b); - let [ans, j] = [0, nums.length]; - - for (const [_, i] of idx) { - ans = Math.max(ans, i - j); - j = Math.min(j, i); - } - - return ans; -} -``` - - - - - diff --git a/solution/0900-0999/0962.Maximum Width Ramp/Solution2.js b/solution/0900-0999/0962.Maximum Width Ramp/Solution2.js deleted file mode 100644 index dfb3998e8f9a6..0000000000000 --- a/solution/0900-0999/0962.Maximum Width Ramp/Solution2.js +++ /dev/null @@ -1,11 +0,0 @@ -function maxWidthRamp(nums) { - const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b); - let [ans, j] = [0, nums.length]; - - for (const [_, i] of idx) { - ans = Math.max(ans, i - j); - j = Math.min(j, i); - } - - return ans; -} diff --git a/solution/0900-0999/0962.Maximum Width Ramp/Solution2.ts b/solution/0900-0999/0962.Maximum Width Ramp/Solution2.ts deleted file mode 100644 index ee30f48ad0ac5..0000000000000 --- a/solution/0900-0999/0962.Maximum Width Ramp/Solution2.ts +++ /dev/null @@ -1,11 +0,0 @@ -function maxWidthRamp(nums: number[]): number { - const idx = nums.map((x, i) => [x, i]).sort(([a], [b]) => a - b); - let [ans, j] = [0, nums.length]; - - for (const [_, i] of idx) { - ans = Math.max(ans, i - j); - j = Math.min(j, i); - } - - return ans; -} diff --git a/solution/0900-0999/0963.Minimum Area Rectangle II/README.md b/solution/0900-0999/0963.Minimum Area Rectangle II/README.md index 95985de7da993..73497a53bbfd1 100644 --- a/solution/0900-0999/0963.Minimum Area Rectangle II/README.md +++ b/solution/0900-0999/0963.Minimum Area Rectangle II/README.md @@ -74,7 +74,7 @@ tags: 最后,如果找到满足条件的矩形,返回其中面积的最小值。否则,返回 $0$。 -时间复杂度 $O(n^3)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $points$ 的长度。 +时间复杂度 $O(n^3)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{points}$ 的长度。 diff --git a/solution/0900-0999/0963.Minimum Area Rectangle II/README_EN.md b/solution/0900-0999/0963.Minimum Area Rectangle II/README_EN.md index ddcb75d6ec6bb..aa39c3230f377 100644 --- a/solution/0900-0999/0963.Minimum Area Rectangle II/README_EN.md +++ b/solution/0900-0999/0963.Minimum Area Rectangle II/README_EN.md @@ -66,7 +66,13 @@ tags: -### Solution 1 +### Solution 1: Hash Table + Enumeration + +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. + +Finally, if a rectangle that satisfies the conditions is found, return the minimum area among them. Otherwise, return $0$. + +The time complexity is $O(n^3)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{points}$. diff --git a/solution/0900-0999/0964.Least Operators to Express Number/README_EN.md b/solution/0900-0999/0964.Least Operators to Express Number/README_EN.md index 425102dc6d09d..6008632b7c282 100644 --- a/solution/0900-0999/0964.Least Operators to Express Number/README_EN.md +++ b/solution/0900-0999/0964.Least Operators to Express Number/README_EN.md @@ -73,7 +73,22 @@ The expression contains 3 operations. -### Solution 1 +### Solution 1: Memoization Search + +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)$. + +The execution logic of function $dfs(v)$ is as follows: + +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. + +Otherwise, we enumerate $x^k$ starting from $k=2$ to find the first $k$ where $x^k \geq v$: + +- 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})$; +- 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. + +To avoid redundant calculations, we implement the $dfs$ function using memoization search. + +The time complexity is $O(\log_{x}{target})$ and the space complexity is $O(\log_{x}{target})$.