Skip to content

Commit f60e9d3

Browse files
authored
feat: add solutions to lc problem: No.3958 (#5251)
1 parent 5b4ea68 commit f60e9d3

12 files changed

Lines changed: 397 additions & 11 deletions

File tree

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ $$
352352

353353
- 时间复杂度:$O(n \log n)$。
354354

355-
BFS 遍历所有节点需要 $O(n)$ 时间。各列中的节点值排序总复杂度为 $O(n \log n)$,于是有 $O(n \log n)$。
355+
BFS 遍历所有节点需要 $O(n)$ 时间。各列中的节点值排序总复杂度为 $O(n \log n)$,于是有 $O(n \log n)$。
356356

357357
- 空间复杂度:$O(n)$。
358358

359-
队列、双端队列以及结果数组最多存储所有节点,因此空间复杂度为 $O(n)$。
359+
队列、双端队列以及结果数组最多存储所有节点,因此空间复杂度为 $O(n)$。
360360

361361
<!-- tabs:start -->
362362

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ function verticalTraversal(root: TreeNode | null): number[][] {
313313
We perform a breadth-first search (BFS) on the tree.
314314

315315
Since our final answer must be returned from leftmost column to rightmost column, we maintain:
316+
316317
- `leftmostCol`: smallest column index currently stored.
317318
- `rightmostCol`: largest column index currently stored.
318319

@@ -325,10 +326,9 @@ When a newly visited node belongs to a column outside the current range:
325326
- If its column index $>$ `rightmostCol`, we append a new column container to the back of deque.
326327

327328
- For any column index `col`, its corresponding position in `columnsValues` can be computed as:
328-
-
329-
$$
330-
col - `leftmostCol`
331-
$$
329+
- $$
330+
col - `leftmostCol`
331+
$$
332332

333333
This allows us to locate target column in constant time.
334334

@@ -344,13 +344,13 @@ Finally, we output all columns from left to right.
344344
Assume binary tree contains $n$ nodes.
345345

346346
- Time Complexity: $O(n \log n)$
347-
348-
BFS visits every node exactly once, which takes $O(n)$ time. Sorting values is $O(n \log n)$ time in worst case.
349-
Therefore, overall time complexity is $O(n \log n)$.
347+
348+
BFS visits every node exactly once, which takes $O(n)$ time. Sorting values is $O(n \log n)$ time in worst case.
349+
Therefore, overall time complexity is $O(n \log n)$.
350350

351351
- Space Complexity: $O(n)$
352352

353-
BFS queue, deque structure, and result container may collectively store all nodes, resulting in $O(n)$ space.
353+
BFS queue, deque structure, and result container may collectively store all nodes, resulting in $O(n)$ space.
354354

355355
<!-- tabs:start -->
356356

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3958.Minimum%20Cost%20to%20Split%20into%20Ones%20II/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3958. Minimum Cost to Split into Ones II 🔒](https://leetcode.cn/problems/minimum-cost-to-split-into-ones-ii)
10+
11+
[English Version](/solution/3900-3999/3958.Minimum%20Cost%20to%20Split%20into%20Ones%20II/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer <code>n</code>.</p>
18+
19+
<p>In one operation, you may split an integer <code>x</code> into two positive integers <code>a</code> and <code>b</code> such that <code>a + b = x</code>.</p>
20+
21+
<p>The cost of this operation is <code>a * b</code>.</p>
22+
23+
<p>Return the <strong>minimum</strong> total cost required to split the integer <code>n</code> into <code>n</code> ones.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<p>One optimal set of operations is:</p>
36+
37+
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">
38+
<tbody>
39+
<tr>
40+
<th><code>x</code></th>
41+
<th><code>a</code></th>
42+
<th><code>b</code></th>
43+
<th><code>a + b</code></th>
44+
<th><code>a * b</code></th>
45+
<th>Cost</th>
46+
</tr>
47+
<tr>
48+
<td>3</td>
49+
<td>1</td>
50+
<td>2</td>
51+
<td>3</td>
52+
<td>2</td>
53+
<td>2</td>
54+
</tr>
55+
<tr>
56+
<td>2</td>
57+
<td>1</td>
58+
<td>1</td>
59+
<td>2</td>
60+
<td>1</td>
61+
<td>1</td>
62+
</tr>
63+
</tbody>
64+
</table>
65+
66+
<p>Thus, the minimum total cost is <code>2 + 1 = 3</code>.</p>
67+
</div>
68+
69+
<p><strong class="example">Example 2:</strong></p>
70+
71+
<div class="example-block">
72+
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
73+
74+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
75+
76+
<p><strong>Explanation:​​​​​​​</strong></p>
77+
78+
<p>One optimal set of operations is:</p>
79+
80+
<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">
81+
<tbody>
82+
<tr>
83+
<th><code>x</code></th>
84+
<th><code>a</code></th>
85+
<th><code>b</code></th>
86+
<th><code>a + b</code></th>
87+
<th><code>a * b</code></th>
88+
<th>Cost</th>
89+
</tr>
90+
<tr>
91+
<td>4</td>
92+
<td>2</td>
93+
<td>2</td>
94+
<td>4</td>
95+
<td>4</td>
96+
<td>4</td>
97+
</tr>
98+
<tr>
99+
<td>2</td>
100+
<td>1</td>
101+
<td>1</td>
102+
<td>2</td>
103+
<td>1</td>
104+
<td>1</td>
105+
</tr>
106+
</tbody>
107+
</table>
108+
109+
<p>Thus, the minimum total cost is <code>4 + 1 + 1 = 6</code>.</p>
110+
</div>
111+
112+
<p>&nbsp;</p>
113+
<p><strong>Constraints:</strong></p>
114+
115+
<ul>
116+
<li><code>1 &lt;= n &lt;= 5 * 10<sup>7</sup></code></li>
117+
</ul>
118+
119+
<!-- description:end -->
120+
121+
## 解法
122+
123+
<!-- solution:start -->
124+
125+
### 方法一:数学
126+
127+
要使得成本最小,我们应该首先将 $n$ 拆分成 $1$ 和 $n - 1$,所需成本为 $n - 1$;然后将 $n - 1$ 拆分成 $1$ 和 $n - 2$,所需成本为 $n - 2$。依此类推,得到总成本为 $1 + 2 + \dots + (n - 1) = \frac{n \times (n - 1)}{2}$。
128+
129+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
130+
131+
<!-- tabs:start -->
132+
133+
#### Python3
134+
135+
```python
136+
class Solution:
137+
def minCost(self, n: int) -> int:
138+
return n * (n - 1) // 2
139+
```
140+
141+
#### Java
142+
143+
```java
144+
class Solution {
145+
public long minCost(int n) {
146+
return 1L * n * (n - 1) / 2;
147+
}
148+
}
149+
```
150+
151+
#### C++
152+
153+
```cpp
154+
class Solution {
155+
public:
156+
long long minCost(int n) {
157+
return 1LL * n * (n - 1) / 2;
158+
}
159+
};
160+
```
161+
162+
#### Go
163+
164+
```go
165+
func minCost(n int) int64 {
166+
return int64(n * (n - 1) / 2)
167+
}
168+
```
169+
170+
#### TypeScript
171+
172+
```ts
173+
function minCost(n: number): number {
174+
return (n * (n - 1)) / 2;
175+
}
176+
```
177+
178+
<!-- tabs:end -->
179+
180+
<!-- solution:end -->
181+
182+
<!-- problem:end -->

0 commit comments

Comments
 (0)