Skip to content

Commit ef39e0c

Browse files
committed
Added tasks 3427-3430
1 parent efe2320 commit ef39e0c

File tree

12 files changed

+541
-0
lines changed

12 files changed

+541
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3401_3500.s3427_sum_of_variable_length_subarrays;
2+
3+
// #Easy #Array #Prefix_Sum #2025_01_22_Time_0_(100.00%)_Space_43.77_(58.41%)
4+
5+
public class Solution {
6+
public int subarraySum(int[] nums) {
7+
int res = nums[0];
8+
for (int i = 1; i < nums.length; i++) {
9+
int j = i - nums[i] - 1;
10+
nums[i] += nums[i - 1];
11+
res += nums[i] - (j < 0 ? 0 : nums[j]);
12+
}
13+
return res;
14+
}
15+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
3427\. Sum of Variable Length Subarrays
2+
3+
Easy
4+
5+
You are given an integer array `nums` of size `n`. For **each** index `i` where `0 <= i < n`, define a **non-empty subarrays** `nums[start ... i]` where `start = max(0, i - nums[i])`.
6+
7+
Return the total sum of all elements from the subarray defined for each index in the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,3,1]
12+
13+
**Output:** 11
14+
15+
**Explanation:**
16+
17+
i
18+
19+
Subarray
20+
21+
Sum
22+
23+
0
24+
25+
`nums[0] = [2]`
26+
27+
2
28+
29+
1
30+
31+
`nums[0 ... 1] = [2, 3]`
32+
33+
5
34+
35+
2
36+
37+
`nums[1 ... 2] = [3, 1]`
38+
39+
4
40+
41+
**Total Sum**
42+
43+
11
44+
45+
The total sum is 11. Hence, 11 is the output.
46+
47+
**Example 2:**
48+
49+
**Input:** nums = [3,1,1,2]
50+
51+
**Output:** 13
52+
53+
**Explanation:**
54+
55+
i
56+
57+
Subarray
58+
59+
Sum
60+
61+
0
62+
63+
`nums[0] = [3]`
64+
65+
3
66+
67+
1
68+
69+
`nums[0 ... 1] = [3, 1]`
70+
71+
4
72+
73+
2
74+
75+
`nums[1 ... 2] = [1, 1]`
76+
77+
2
78+
79+
3
80+
81+
`nums[1 ... 3] = [1, 1, 2]`
82+
83+
4
84+
85+
**Total Sum**
86+
87+
13
88+
89+
The total sum is 13. Hence, 13 is the output.
90+
91+
**Constraints:**
92+
93+
* `1 <= n == nums.length <= 100`
94+
* `1 <= nums[i] <= 1000`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g3401_3500.s3428_maximum_and_minimum_sums_of_at_most_size_k_subsequences;
2+
3+
// #Medium #Array #Dynamic_Programming #Math #Sorting #Combinatorics
4+
// #2025_01_22_Time_28_(99.74%)_Space_65.01_(35.71%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
private static final int MOD = (int) 1e9 + 7;
10+
private static long[] fact, invFact;
11+
12+
public int minMaxSums(int[] nums, int k) {
13+
int n = nums.length;
14+
Arrays.sort(nums);
15+
if (fact == null || fact.length < n + 1) {
16+
buildFactorials(n);
17+
}
18+
long[] sum = new long[n + 1];
19+
sum[0] = 1;
20+
for (int i = 0; i < n; i++) {
21+
long val = (2L * sum[i]) % MOD;
22+
if (i + 1 >= k) {
23+
val = (val - comb(i, k - 1) + MOD) % MOD;
24+
}
25+
sum[i + 1] = val;
26+
}
27+
long res = 0;
28+
for (int i = 0; i < n; i++) {
29+
long add = (sum[i] + sum[n - 1 - i]) % MOD;
30+
res = (res + (nums[i] % MOD) * add) % MOD;
31+
}
32+
return (int) res;
33+
}
34+
35+
private void buildFactorials(int n) {
36+
fact = new long[n + 1];
37+
invFact = new long[n + 1];
38+
fact[0] = 1;
39+
for (int i = 1; i <= n; i++) {
40+
fact[i] = (fact[i - 1] * i) % MOD;
41+
}
42+
invFact[n] = pow(fact[n], MOD - 2);
43+
for (int i = n - 1; i >= 0; i--) {
44+
invFact[i] = (invFact[i + 1] * (i + 1)) % MOD;
45+
}
46+
}
47+
48+
private long comb(int n, int r) {
49+
if (r < 0 || r > n) return 0;
50+
return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;
51+
}
52+
53+
private long pow(long base, int exp) {
54+
long ans = 1L;
55+
while (exp > 0) {
56+
if ((exp & 1) == 1) ans = (ans * base) % MOD;
57+
base = (base * base) % MOD;
58+
exp >>= 1;
59+
}
60+
return ans;
61+
}
62+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
3428\. Maximum and Minimum Sums of at Most Size K Subsequences
2+
3+
Medium
4+
5+
You are given an integer array `nums` and a positive integer `k`. Return the sum of the **maximum** and **minimum** elements of all
6+
7+
**subsequences**
8+
9+
of `nums` with **at most** `k` elements.
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3], k = 2
16+
17+
**Output:** 24
18+
19+
**Explanation:**
20+
21+
The subsequences of `nums` with at most 2 elements are:
22+
23+
**Subsequence**
24+
25+
Minimum
26+
27+
Maximum
28+
29+
Sum
30+
31+
`[1]`
32+
33+
1
34+
35+
1
36+
37+
2
38+
39+
`[2]`
40+
41+
2
42+
43+
2
44+
45+
4
46+
47+
`[3]`
48+
49+
3
50+
51+
3
52+
53+
6
54+
55+
`[1, 2]`
56+
57+
1
58+
59+
2
60+
61+
3
62+
63+
`[1, 3]`
64+
65+
1
66+
67+
3
68+
69+
4
70+
71+
`[2, 3]`
72+
73+
2
74+
75+
3
76+
77+
5
78+
79+
**Final Total**
80+
81+
24
82+
83+
The output would be 24.
84+
85+
**Example 2:**
86+
87+
**Input:** nums = [5,0,6], k = 1
88+
89+
**Output:** 22
90+
91+
**Explanation:**
92+
93+
For subsequences with exactly 1 element, the minimum and maximum values are the element itself. Therefore, the total is `5 + 5 + 0 + 0 + 6 + 6 = 22`.
94+
95+
**Example 3:**
96+
97+
**Input:** nums = [1,1,1], k = 2
98+
99+
**Output:** 12
100+
101+
**Explanation:**
102+
103+
The subsequences `[1, 1]` and `[1]` each appear 3 times. For all of them, the minimum and maximum are both 1. Thus, the total is 12.
104+
105+
**Constraints:**
106+
107+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
108+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
109+
* `1 <= k <= min(70, nums.length)`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3401_3500.s3429_paint_house_iv;
2+
3+
// #Medium #Array #Dynamic_Programming #2025_01_22_Time_5_(100.00%)_Space_106.29_(78.64%)
4+
5+
public class Solution {
6+
public long minCost(int n, int[][] cost) {
7+
long dp0 = 0;
8+
long dp1 = 0;
9+
long dp2 = 0;
10+
long dp3 = 0;
11+
long dp4 = 0;
12+
long dp5 = 0;
13+
for (int i = 0; i < n / 2; ++i) {
14+
long nextdp0 = Math.min(Math.min(dp2, dp3), dp4) + cost[i][0] + cost[n - i - 1][1];
15+
long nextdp1 = Math.min(Math.min(dp2, dp4), dp5) + cost[i][0] + cost[n - i - 1][2];
16+
long nextdp2 = Math.min(Math.min(dp0, dp1), dp5) + cost[i][1] + cost[n - i - 1][0];
17+
long nextdp3 = Math.min(Math.min(dp0, dp4), dp5) + cost[i][1] + cost[n - i - 1][2];
18+
long nextdp4 = Math.min(Math.min(dp0, dp1), dp3) + cost[i][2] + cost[n - i - 1][0];
19+
long nextdp5 = Math.min(Math.min(dp1, dp2), dp3) + cost[i][2] + cost[n - i - 1][1];
20+
dp0 = nextdp0;
21+
dp1 = nextdp1;
22+
dp2 = nextdp2;
23+
dp3 = nextdp3;
24+
dp4 = nextdp4;
25+
dp5 = nextdp5;
26+
}
27+
long ans = Long.MAX_VALUE;
28+
for (long x : new long[] {dp0, dp1, dp2, dp3, dp4, dp5}) {
29+
ans = Math.min(ans, x);
30+
}
31+
return ans;
32+
}
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3429\. Paint House IV
2+
3+
Medium
4+
5+
You are given an **even** integer `n` representing the number of houses arranged in a straight line, and a 2D array `cost` of size `n x 3`, where `cost[i][j]` represents the cost of painting house `i` with color `j + 1`.
6+
7+
The houses will look **beautiful** if they satisfy the following conditions:
8+
9+
* No **two** adjacent houses are painted the same color.
10+
* Houses **equidistant** from the ends of the row are **not** painted the same color. For example, if `n = 6`, houses at positions `(0, 5)`, `(1, 4)`, and `(2, 3)` are considered equidistant.
11+
12+
Return the **minimum** cost to paint the houses such that they look **beautiful**.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]]
17+
18+
**Output:** 9
19+
20+
**Explanation:**
21+
22+
The optimal painting sequence is `[1, 2, 3, 2]` with corresponding costs `[3, 2, 1, 3]`. This satisfies the following conditions:
23+
24+
* No adjacent houses have the same color.
25+
* Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color `(1 != 2)`.
26+
* Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color `(2 != 3)`.
27+
28+
The minimum cost to paint the houses so that they look beautiful is `3 + 2 + 1 + 3 = 9`.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]]
33+
34+
**Output:** 18
35+
36+
**Explanation:**
37+
38+
The optimal painting sequence is `[1, 3, 2, 3, 1, 2]` with corresponding costs `[2, 8, 1, 2, 3, 2]`. This satisfies the following conditions:
39+
40+
* No adjacent houses have the same color.
41+
* Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color `(1 != 2)`.
42+
* Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color `(3 != 1)`.
43+
* Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color `(2 != 3)`.
44+
45+
The minimum cost to paint the houses so that they look beautiful is `2 + 8 + 1 + 2 + 3 + 2 = 18`.
46+
47+
**Constraints:**
48+
49+
* <code>2 <= n <= 10<sup>5</sup></code>
50+
* `n` is even.
51+
* `cost.length == n`
52+
* `cost[i].length == 3`
53+
* <code>0 <= cost[i]\[j] <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)