Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ $$

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

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ function verticalTraversal(root: TreeNode | null): number[][] {
We perform a breadth-first search (BFS) on the tree.

Since our final answer must be returned from leftmost column to rightmost column, we maintain:

- `leftmostCol`: smallest column index currently stored.
- `rightmostCol`: largest column index currently stored.

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

- For any column index `col`, its corresponding position in `columnsValues` can be computed as:
-
$$
col - `leftmostCol`
$$
- $$
col - `leftmostCol`
$$

Comment on lines 328 to 332
This allows us to locate target column in constant time.

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

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

BFS visits every node exactly once, which takes $O(n)$ time. Sorting values is $O(n \log n)$ time in worst case.
Therefore, overall time complexity is $O(n \log n)$.

- Space Complexity: $O(n)$

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

<!-- tabs:start -->

Expand Down
182 changes: 182 additions & 0 deletions solution/3900-3999/3958.Minimum Cost to Split into Ones II/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3958.Minimum%20Cost%20to%20Split%20into%20Ones%20II/README.md
---

<!-- problem:start -->

# [3958. Minimum Cost to Split into Ones II 🔒](https://leetcode.cn/problems/minimum-cost-to-split-into-ones-ii)

[English Version](/solution/3900-3999/3958.Minimum%20Cost%20to%20Split%20into%20Ones%20II/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given an integer <code>n</code>.</p>

<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>

<p>The cost of this operation is <code>a * b</code>.</p>
Comment on lines +17 to +21

<p>Return the <strong>minimum</strong> total cost required to split the integer <code>n</code> into <code>n</code> ones.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>One optimal set of operations is:</p>

<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">
<tbody>
<tr>
<th><code>x</code></th>
<th><code>a</code></th>
<th><code>b</code></th>
<th><code>a + b</code></th>
<th><code>a * b</code></th>
<th>Cost</th>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>1</td>
<td>2</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>

<p>Thus, the minimum total cost is <code>2 + 1 = 3</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:​​​​​​​</strong></p>

<p>One optimal set of operations is:</p>

<table border="1" bordercolor="#ccc" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">
<tbody>
<tr>
<th><code>x</code></th>
<th><code>a</code></th>
<th><code>b</code></th>
<th><code>a + b</code></th>
<th><code>a * b</code></th>
<th>Cost</th>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>2</td>
<td>4</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>1</td>
<td>2</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>

<p>Thus, the minimum total cost is <code>4 + 1 + 1 = 6</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 5 * 10<sup>7</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:数学

要使得成本最小,我们应该首先将 $n$ 拆分成 $1$ 和 $n - 1$,所需成本为 $n - 1$;然后将 $n - 1$ 拆分成 $1$ 和 $n - 2$,所需成本为 $n - 2$。依此类推,得到总成本为 $1 + 2 + \dots + (n - 1) = \frac{n \times (n - 1)}{2}$。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minCost(self, n: int) -> int:
return n * (n - 1) // 2
```

#### Java

```java
class Solution {
public long minCost(int n) {
return 1L * n * (n - 1) / 2;
}
}
```

#### C++

```cpp
class Solution {
public:
long long minCost(int n) {
return 1LL * n * (n - 1) / 2;
}
};
```

#### Go

```go
func minCost(n int) int64 {
return int64(n * (n - 1) / 2)
}
```

#### TypeScript

```ts
function minCost(n: number): number {
return (n * (n - 1)) / 2;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading