|
| 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> </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> </p> |
| 113 | +<p><strong>Constraints:</strong></p> |
| 114 | + |
| 115 | +<ul> |
| 116 | + <li><code>1 <= n <= 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