You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/README_EN.md
+41-60
Original file line number
Diff line number
Diff line change
@@ -61,99 +61,80 @@ It can be proven that 8 is the minimum possible sum that a beautiful array could
61
61
62
62
## Solutions
63
63
64
-
### Solution 1: Greedy + Hash Table
64
+
### Solution 1: Greedy + Mathematics
65
65
66
-
We start from the positive integer $i=1$, and judge whether $i$ can be added to the array in turn. If it can be added, we add $i$ to the array, accumulate it to the answer, and then mark $target-i$ as visited, indicating that $target-i$ cannot be added to the array. The loop continues until the length of the array is $n$.
66
+
We can greedily construct the array `nums` starting from $x = 1$, choosing $x$ each time and excluding $target - x$.
67
67
68
-
The time complexity is $O(n + target)$, and the space complexity is $O(n + target)$. Here, $n$ is the length of the array.
68
+
If $x <= \left\lfloor \frac{target}{2} \right\rfloor$, then the numbers we can choose are $1, 2, \cdots, n$, so the sum of the array is $\left\lfloor \frac{n(n+1)}{2} \right\rfloor$.
69
+
70
+
If $x > \left\lfloor \frac{target}{2} \right\rfloor$, then the numbers we can choose are $1, 2, \cdots, \left\lfloor \frac{target}{2} \right\rfloor$, a total of $\left\lfloor \frac{target}{2} \right\rfloor$ numbers, and $n - \left\lfloor \frac{target}{2} \right\rfloor$ numbers starting from $target$, so the sum of the array is $\left\lfloor \frac{\left\lfloor \frac{target}{2} \right\rfloor \left(\left\lfloor \frac{target}{2} \right\rfloor + 1\right)}{2} \right\rfloor + \left\lfloor \frac{target + target + n - \left\lfloor \frac{target}{2} \right\rfloor - 1}{2} \right\rfloor$.
71
+
72
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
0 commit comments