Skip to content

Commit d98a9da

Browse files
committed
Added tasks 3427-3430
1 parent 7fa8820 commit d98a9da

File tree

12 files changed

+528
-0
lines changed

12 files changed

+528
-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+
class Solution {
6+
fun subarraySum(nums: IntArray): Int {
7+
var res = nums[0]
8+
for (i in 1..<nums.size) {
9+
val j = i - nums[i] - 1
10+
nums[i] += nums[i - 1]
11+
res += nums[i] - (if (j < 0) 0 else 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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_167_(77.78%)_Space_75.34_(66.67%)
5+
6+
class Solution {
7+
private lateinit var fact: LongArray
8+
private lateinit var inv: LongArray
9+
10+
private fun precomputeFactorials(n: Int) {
11+
fact = LongArray(n + 1)
12+
inv = LongArray(n + 1)
13+
fact[0] = 1
14+
for (i in 1..n) {
15+
fact[i] = (fact[i - 1] * i) % MOD
16+
}
17+
inv[n] = power(fact[n], (MOD - 2).toLong(), MOD)
18+
for (i in n - 1 downTo 0) {
19+
inv[i] = (inv[i + 1] * (i + 1)) % MOD
20+
}
21+
}
22+
23+
private fun power(a: Long, b: Long, m: Int): Long {
24+
var a = a
25+
var b = b
26+
var `val`: Long = 1
27+
a = a % m
28+
while (b > 0) {
29+
if ((b and 1L) == 1L) `val` = (`val` * a) % m
30+
b = b shr 1
31+
a = (a * a) % m
32+
}
33+
return `val`
34+
}
35+
36+
private fun nCr(n: Int, r: Int): Long {
37+
if (r < 0 || r > n) return 0
38+
return (fact[n] * inv[r] % MOD * inv[n - r]) % MOD
39+
}
40+
41+
fun minMaxSums(nums: IntArray, k: Int): Int {
42+
val n = nums.size
43+
nums.sort()
44+
precomputeFactorials(n)
45+
var sum: Long = 0
46+
for (i in 0..<n) {
47+
var `val`: Long = 0
48+
val a = i
49+
val b = n - 1 - i
50+
run {
51+
var j = 0
52+
while (j < k && j <= a) {
53+
`val` = (`val` + nCr(a, j)) % MOD
54+
j++
55+
}
56+
}
57+
sum = (sum + (`val` * nums[i]) % MOD) % MOD
58+
`val` = 0
59+
var j = 0
60+
while (j < k && j <= b) {
61+
`val` = (`val` + nCr(b, j)) % MOD
62+
j++
63+
}
64+
sum = (sum + (`val` * nums[i]) % MOD) % MOD
65+
}
66+
return sum.toInt()
67+
}
68+
69+
companion object {
70+
private const val MOD = 1e9.toInt() + 7
71+
}
72+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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** | Minimum | Maximum | Sum |
24+
|-----------------|---------|---------|------|
25+
| `[1]` | 1 | 1 | 2 |
26+
| `[2]` | 2 | 2 | 4 |
27+
| `[3]` | 3 | 3 | 6 |
28+
| `[1, 2]` | 1 | 2 | 3 |
29+
| `[1, 3]` | 1 | 3 | 4 |
30+
| `[2, 3]` | 2 | 3 | 5 |
31+
| **Final Total** | | | 24 |
32+
33+
The output would be 24.
34+
35+
**Example 2:**
36+
37+
**Input:** nums = [5,0,6], k = 1
38+
39+
**Output:** 22
40+
41+
**Explanation:**
42+
43+
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`.
44+
45+
**Example 3:**
46+
47+
**Input:** nums = [1,1,1], k = 2
48+
49+
**Output:** 12
50+
51+
**Explanation:**
52+
53+
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.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
58+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
59+
* `1 <= k <= min(70, nums.length)`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3401_3500.s3429_paint_house_iv
2+
3+
// #Medium #Array #Dynamic_Programming #2025_01_22_Time_10_(100.00%)_Space_119.77_(84.62%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minCost(n: Int, cost: Array<IntArray>): Long {
9+
var dp0: Long = 0
10+
var dp1: Long = 0
11+
var dp2: Long = 0
12+
var dp3: Long = 0
13+
var dp4: Long = 0
14+
var dp5: Long = 0
15+
for (i in 0..<n / 2) {
16+
val nextdp0: Long =
17+
min(min(dp2, dp3), dp4) + cost[i][0] + cost[n - i - 1][1]
18+
val nextdp1: Long =
19+
min(min(dp2, dp4), dp5) + cost[i][0] + cost[n - i - 1][2]
20+
val nextdp2: Long =
21+
min(min(dp0, dp1), dp5) + cost[i][1] + cost[n - i - 1][0]
22+
val nextdp3: Long =
23+
min(min(dp0, dp4), dp5) + cost[i][1] + cost[n - i - 1][2]
24+
val nextdp4: Long =
25+
min(min(dp0, dp1), dp3) + cost[i][2] + cost[n - i - 1][0]
26+
val nextdp5: Long =
27+
min(min(dp1, dp2), dp3) + cost[i][2] + cost[n - i - 1][1]
28+
dp0 = nextdp0
29+
dp1 = nextdp1
30+
dp2 = nextdp2
31+
dp3 = nextdp3
32+
dp4 = nextdp4
33+
dp5 = nextdp5
34+
}
35+
var ans = Long.Companion.MAX_VALUE
36+
for (x in longArrayOf(dp0, dp1, dp2, dp3, dp4, dp5)) {
37+
ans = min(ans, x).toLong()
38+
}
39+
return ans
40+
}
41+
}
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>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3401_3500.s3430_maximum_and_minimum_sums_of_at_most_size_k_subarrays
2+
3+
// #Hard #Array #Math #Stack #Monotonic_Stack #2025_01_22_Time_31_(100.00%)_Space_74.84_(24.24%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minMaxSubarraySum(nums: IntArray, k: Int): Long {
9+
val sum = solve(nums, k)
10+
for (i in nums.indices) {
11+
nums[i] = -nums[i]
12+
}
13+
return sum - solve(nums, k)
14+
}
15+
16+
private fun solve(nums: IntArray, k: Int): Long {
17+
val n = nums.size
18+
val left = IntArray(n)
19+
val right = IntArray(n)
20+
val st = IntArray(n)
21+
var top = -1
22+
for (i in 0..<n) {
23+
val num = nums[i]
24+
while (top != -1 && num < nums[st[top]]) {
25+
right[st[top--]] = i
26+
}
27+
left[i] = if (top == -1) -1 else st[top]
28+
st[++top] = i
29+
}
30+
while (0 <= top) {
31+
right[st[top--]] = n
32+
}
33+
var ans: Long = 0
34+
for (i in 0..<n) {
35+
val num = nums[i]
36+
val l = min((i - left[i]), k)
37+
val r = min((right[i] - i), k)
38+
if (l + r - 1 <= k) {
39+
ans += num.toLong() * l * r
40+
} else {
41+
val cnt = (k - r + 1).toLong() * r + (l + r - k - 1).toLong() * (r + k - l) / 2
42+
ans += num * cnt
43+
}
44+
}
45+
return ans
46+
}
47+
}

0 commit comments

Comments
 (0)