Skip to content

Commit 230050e

Browse files
authored
feat: add cs solution to lc problem: No.2834 (doocs#2415)
No.2834.Find the Minimum Possible Sum of a Beautiful Array
1 parent d56ace5 commit 230050e

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ nums = [1,3,4] 是美丽数组。
6868

6969
我们可以贪心地从 $x = 1$ 开始构造数组 $nums$,每次选择 $x$,并且排除 $target - x$。
7070

71-
如果 $x <= \left\lfloor \frac{target}{2} \right\rfloor$,那么我们可以选择的数字分别为 $1, 2, \cdots, n$,因此,数组的和为 $\left\lfloor \frac{n(n+1)}{2} \right\rfloor$。
71+
我们不妨记 $m = \left\lfloor \frac{target}{2} \right\rfloor$。
7272

73-
如果 $x > \left\lfloor \frac{target}{2} \right\rfloor$,那么我们可以选择的数字分别为 $1, 2, \cdots, \left\lfloor \frac{target}{2} \right\rfloor$,共 $\left\lfloor \frac{target}{2} \right\rfloor$ 个数,以及从 $target$ 开始的 $n - \left\lfloor \frac{target}{2} \right\rfloor$ 个数字,因此,数组的和为 $\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$。
73+
如果 $x <= m$,那么我们可以选择的数有 $1, 2, \cdots, n$,所以数组的和为 $\left\lfloor \frac{(1+n)n}{2} \right\rfloor$。
74+
75+
如果 $x > m$,那么我们可以选择的数有 $1, 2, \cdots, m$,共 $m$ 个数,以及 $n - m$ 个从 $target$ 开始的数,所以数组的和为 $\left\lfloor \frac{(1+m)m}{2} \right\rfloor + \left\lfloor \frac{(target + target + n - m - 1)(n-m)}{2} \right\rfloor$。
76+
77+
注意,我们需要对结果取模 $10^9 + 7$。
7478

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

@@ -141,6 +145,21 @@ function minimumPossibleSum(n: number, target: number): number {
141145
}
142146
```
143147

148+
```cs
149+
public class Solution {
150+
public int MinimumPossibleSum(int n, int target) {
151+
const int mod = (int) 1e9 + 7;
152+
int m = target / 2;
153+
if (n <= m) {
154+
return (int) ((1L + n) * n / 2 % mod);
155+
}
156+
long a = (1L + m) * m / 2 % mod;
157+
long b = ((1L * target + target + n - m - 1) * (n - m) / 2) % mod;
158+
return (int) ((a + b) % mod);
159+
}
160+
}
161+
```
162+
144163
<!-- tabs:end -->
145164

146165
<!-- end -->

solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/README_EN.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ It can be proven that 8 is the minimum possible sum that a beautiful array could
6565

6666
We can greedily construct the array `nums` starting from $x = 1$, choosing $x$ each time and excluding $target - x$.
6767

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$.
68+
Let's denote $m = \left\lfloor \frac{target}{2} \right\rfloor$.
6969

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$.
70+
If $x <= m$, then the numbers we can choose are $1, 2, \cdots, n$, so the sum of the array is $\left\lfloor \frac{(1+n)n}{2} \right\rfloor$.
71+
72+
If $x > m$, then the numbers we can choose are $1, 2, \cdots, m$, a total of $m$ numbers, and $n - m$ numbers starting from $target$, so the sum of the array is $\left\lfloor \frac{(1+m)m}{2} \right\rfloor + \left\lfloor \frac{(target + target + n - m - 1)(n-m)}{2} \right\rfloor$.
73+
74+
Note that we need to take the modulus of $10^9 + 7$ for the result.
7175

7276
The time complexity is $O(1)$, and the space complexity is $O(1)$.
7377

@@ -138,6 +142,21 @@ function minimumPossibleSum(n: number, target: number): number {
138142
}
139143
```
140144

145+
```cs
146+
public class Solution {
147+
public int MinimumPossibleSum(int n, int target) {
148+
const int mod = (int) 1e9 + 7;
149+
int m = target / 2;
150+
if (n <= m) {
151+
return (int) ((1L + n) * n / 2 % mod);
152+
}
153+
long a = (1L + m) * m / 2 % mod;
154+
long b = ((1L * target + target + n - m - 1) * (n - m) / 2) % mod;
155+
return (int) ((a + b) % mod);
156+
}
157+
}
158+
```
159+
141160
<!-- tabs:end -->
142161

143162
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public int MinimumPossibleSum(int n, int target) {
3+
const int mod = (int) 1e9 + 7;
4+
int m = target / 2;
5+
if (n <= m) {
6+
return (int) ((1L + n) * n / 2 % mod);
7+
}
8+
long a = (1L + m) * m / 2 % mod;
9+
long b = ((1L * target + target + n - m - 1) * (n - m) / 2) % mod;
10+
return (int) ((a + b) % mod);
11+
}
12+
}

0 commit comments

Comments
 (0)