|
35 | 35 | <pre> |
36 | 36 | <strong>Input:</strong> target = [9,3,5] |
37 | 37 | <strong>Output:</strong> true |
38 | | -<strong>Explanation:</strong> Start with arr = [1, 1, 1] |
| 38 | +<strong>Explanation:</strong> Start with arr = [1, 1, 1] |
39 | 39 | [1, 1, 1], sum = 3 choose index 1 |
40 | 40 | [1, 3, 1], sum = 5 choose index 2 |
41 | 41 | [1, 3, 5], sum = 9 choose index 0 |
@@ -74,11 +74,11 @@ tags: |
74 | 74 |
|
75 | 75 | ### Solution 1: Reverse Construction + Priority Queue (Max Heap) |
76 | 76 |
|
77 | | -We find that if we start from the array $arr$ and construct the target array $target$ forward, it is not easy to determine which index $i$ to choose each time, and the problem is relatively complex. However, if we start from the array $target$ and construct it in reverse, each construction must choose the largest element in the current array, which can ensure that each construction is unique, and the problem is relatively simple. |
| 77 | +We observe that if we start constructing the target array $\textit{target}$ from the array $\textit{arr}$ in a forward manner, it is difficult to determine which index $i$ to choose each time, making the problem quite complex. However, if we construct in reverse starting from the array $\textit{target}$, each construction step must select the largest element in the current array, which ensures that each construction is unique, making the problem relatively simple. |
78 | 78 |
|
79 | | -Therefore, we can use a priority queue (max heap) to store the elements in the array $target$, and use a variable $s$ to record the sum of all elements in the array $target$. Each time we take out the largest element $mx$ from the priority queue, calculate the sum $t$ of all elements in the current array except $mx$. If $t < 1$ or $mx - t < 1$, it means that the target array $target$ cannot be constructed, and we return `false`. Otherwise, we calculate $mx \bmod t$. If $mx \bmod t = 0$, let $x = t$, otherwise let $x = mx \bmod t$, add $x$ to the priority queue, and update the value of $s$, repeat the above operations until all elements in the priority queue become $1$, then return `true`. |
| 79 | +Therefore, we can use a priority queue (max heap) to store the elements of array $\textit{target}$, and use a variable $s$ to record the sum of all elements in array $\textit{target}$. Each time we extract the maximum element $mx$ from the priority queue and calculate the sum $t$ of all elements in the current array except $mx$. If $t \lt 1$ or $mx - t \lt 1$, it means the target array $\textit{target}$ cannot be constructed, and we return `false`. Otherwise, we calculate $mx \bmod t$. If $mx \bmod t = 0$, we set $x = t$; otherwise, we set $x = mx \bmod t$. We add $x$ to the priority queue and update the value of $s$. We repeat this process until all elements in the priority queue become $1$, at which point we return `true`. |
80 | 80 |
|
81 | | -The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $target$. |
| 81 | +The time complexity is $O(n \log n)$ and the space complexity is $O(n)$, where $n$ is the length of array $\textit{target}$. |
82 | 82 |
|
83 | 83 | <!-- tabs:start --> |
84 | 84 |
|
@@ -219,6 +219,34 @@ function isPossible(target: number[]): boolean { |
219 | 219 | } |
220 | 220 | ``` |
221 | 221 |
|
| 222 | +#### Rust |
| 223 | + |
| 224 | +```rust |
| 225 | +use std::collections::BinaryHeap; |
| 226 | + |
| 227 | +impl Solution { |
| 228 | + pub fn is_possible(target: Vec<i32>) -> bool { |
| 229 | + let mut pq = BinaryHeap::from(target.clone()); |
| 230 | + let mut s: i64 = target.iter().map(|&x| x as i64).sum(); |
| 231 | + |
| 232 | + while let Some(&mx) = pq.peek() { |
| 233 | + if mx == 1 { |
| 234 | + break; |
| 235 | + } |
| 236 | + let mx = pq.pop().unwrap() as i64; |
| 237 | + let t = s - mx; |
| 238 | + if t < 1 || mx - t < 1 { |
| 239 | + return false; |
| 240 | + } |
| 241 | + let x = if mx % t == 0 { t } else { mx % t }; |
| 242 | + pq.push(x as i32); |
| 243 | + s = s - mx + x; |
| 244 | + } |
| 245 | + true |
| 246 | + } |
| 247 | +} |
| 248 | +``` |
| 249 | + |
222 | 250 | <!-- tabs:end --> |
223 | 251 |
|
224 | 252 | <!-- solution:end --> |
|
0 commit comments