Skip to content

Commit c7e16ce

Browse files
committed
Added tasks 3487-3490
1 parent 28663e0 commit c7e16ce

File tree

12 files changed

+497
-0
lines changed

12 files changed

+497
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3401_3500.s3487_maximum_unique_subarray_sum_after_deletion
2+
3+
// #Easy #2025_03_16_Time_4_ms_(100.00%)_Space_43.27_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxSum(nums: IntArray): Int {
7+
var sum = 0
8+
val st = mutableSetOf<Int>()
9+
var mxNeg = Int.MIN_VALUE
10+
for (num in nums) {
11+
if (num > 0) {
12+
st.add(num)
13+
} else {
14+
mxNeg = maxOf(mxNeg, num)
15+
}
16+
}
17+
for (value in st) {
18+
sum += value
19+
}
20+
return if (st.isNotEmpty()) sum else mxNeg
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3487\. Maximum Unique Subarray Sum After Deletion
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
You are allowed to delete any number of elements from `nums` without making it **empty**. After performing the deletions, select a non-empty subarrays of `nums` such that:
8+
9+
1. All elements in the subarray are **unique**.
10+
2. The sum of the elements in the subarray is **maximized**.
11+
12+
Return the **maximum sum** of such a subarray.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4,5]
17+
18+
**Output:** 15
19+
20+
**Explanation:**
21+
22+
Select the entire array without deleting any element to obtain the maximum sum.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,1,0,1,1]
27+
28+
**Output:** 1
29+
30+
**Explanation:**
31+
32+
Delete the element `nums[0] == 1`, `nums[1] == 1`, `nums[2] == 0`, and `nums[3] == 1`. Select the entire array `[1]` to obtain the maximum sum.
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [1,2,-1,-2,1,0,-1]
37+
38+
**Output:** 3
39+
40+
**Explanation:**
41+
42+
Delete the elements `nums[2] == -1` and `nums[3] == -2`, and select the subarray `[2, 1]` from `[1, 2, 1, 0, -1]` to obtain the maximum sum.
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 100`
47+
* `-100 <= nums[i] <= 100`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3401_3500.s3488_closest_equal_element_queries
2+
3+
// #Medium #2025_03_16_Time_93_ms_(100.00%)_Space_99.42_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun solveQueries(nums: IntArray, queries: IntArray): List<Int> {
10+
val sz = nums.size
11+
val indices: MutableMap<Int, MutableList<Int>> = HashMap<Int, MutableList<Int>>()
12+
for (i in 0..<sz) {
13+
indices.computeIfAbsent(nums[i]) { _: Int -> ArrayList<Int>() }.add(i)
14+
}
15+
for (arr in indices.values) {
16+
val m = arr.size
17+
if (m == 1) {
18+
nums[arr[0]] = -1
19+
continue
20+
}
21+
for (i in 0..<m) {
22+
val j: Int = arr[i]
23+
val f: Int = arr[(i + 1) % m]
24+
val b: Int = arr[(i - 1 + m) % m]
25+
val forward = min(((sz - j - 1) + f + 1), abs((j - f)))
26+
val backward = min(abs((b - j)), (j + (sz - b)))
27+
nums[j] = min(backward, forward)
28+
}
29+
}
30+
val res: MutableList<Int> = ArrayList<Int>()
31+
for (q in queries) {
32+
res.add(nums[q])
33+
}
34+
return res
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3488\. Closest Equal Element Queries
2+
3+
Medium
4+
5+
You are given a **circular** array `nums` and an array `queries`.
6+
7+
For each query `i`, you have to find the following:
8+
9+
* The **minimum** distance between the element at index `queries[i]` and **any** other index `j` in the **circular** array, where `nums[j] == nums[queries[i]]`. If no such index exists, the answer for that query should be -1.
10+
11+
Return an array `answer` of the **same** size as `queries`, where `answer[i]` represents the result for query `i`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,3,1,4,1,3,2], queries = [0,3,5]
16+
17+
**Output:** [2,-1,3]
18+
19+
**Explanation:**
20+
21+
* Query 0: The element at `queries[0] = 0` is `nums[0] = 1`. The nearest index with the same value is 2, and the distance between them is 2.
22+
* Query 1: The element at `queries[1] = 3` is `nums[3] = 4`. No other index contains 4, so the result is -1.
23+
* Query 2: The element at `queries[2] = 5` is `nums[5] = 3`. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: `5 -> 6 -> 0 -> 1`).
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,2,3,4], queries = [0,1,2,3]
28+
29+
**Output:** [-1,-1,-1,-1]
30+
31+
**Explanation:**
32+
33+
Each value in `nums` is unique, so no index shares the same value as the queried element. This results in -1 for all queries.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= queries.length <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
39+
* `0 <= queries[i] < nums.length`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3401_3500.s3489_zero_array_transformation_iv
2+
3+
// #Medium #2025_03_16_Time_104_ms_(100.00%)_Space_73.10_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
private fun solve(q: Array<IntArray>, i: Int, target: Int, k: Int, dp: Array<IntArray>): Int {
10+
// we found a valid sum equal to target so return current index of query.
11+
if (target == 0) {
12+
return k
13+
}
14+
// return a larger number to invalidate this flow
15+
if (k >= q.size || target < 0) {
16+
return q.size + 1
17+
}
18+
if (dp[target][k] != -1) {
19+
return dp[target][k]
20+
}
21+
// skip current query val
22+
var res = solve(q, i, target, k + 1, dp)
23+
// pick the val if its range is in the range of target index
24+
if (q[k][0] <= i && i <= q[k][1]) {
25+
res = min(res, solve(q, i, target - q[k][2], k + 1, dp))
26+
}
27+
dp[target][k] = res
28+
return res
29+
}
30+
31+
fun minZeroArray(nums: IntArray, queries: Array<IntArray>): Int {
32+
var ans = -1
33+
for (i in nums.indices) {
34+
val dp = Array<IntArray>(nums[i] + 1) { IntArray(queries.size) }
35+
dp.forEach { row: IntArray -> row.fill(-1) }
36+
ans = max(ans, solve(queries, i, nums[i], 0, dp))
37+
}
38+
return if (ans > queries.size) -1 else ans
39+
}
40+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
3489\. Zero Array Transformation IV
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n` and a 2D array `queries`, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code>.
6+
7+
Each `queries[i]` represents the following action on `nums`:
8+
9+
* Select a **subset** of indices in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> from `nums`.
10+
* Decrement the value at each selected index by **exactly** <code>val<sub>i</sub></code>.
11+
12+
A **Zero Array** is an array with all its elements equal to 0.
13+
14+
Return the **minimum** possible **non-negative** value of `k`, such that after processing the first `k` queries in **sequence**, `nums` becomes a **Zero Array**. If no such `k` exists, return -1.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
* **For query 0 (l = 0, r = 2, val = 1):**
25+
* Decrement the values at indices `[0, 2]` by 1.
26+
* The array will become `[1, 0, 1]`.
27+
* **For query 1 (l = 0, r = 2, val = 1):**
28+
* Decrement the values at indices `[0, 2]` by 1.
29+
* The array will become `[0, 0, 0]`, which is a Zero Array. Therefore, the minimum value of `k` is 2.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]
34+
35+
**Output:** \-1
36+
37+
**Explanation:**
38+
39+
It is impossible to make nums a Zero Array even after all the queries.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]]
44+
45+
**Output:** 4
46+
47+
**Explanation:**
48+
49+
* **For query 0 (l = 0, r = 1, val = 1):**
50+
* Decrement the values at indices `[0, 1]` by `1`.
51+
* The array will become `[0, 1, 3, 2, 1]`.
52+
* **For query 1 (l = 1, r = 2, val = 1):**
53+
* Decrement the values at indices `[1, 2]` by 1.
54+
* The array will become `[0, 0, 2, 2, 1]`.
55+
* **For query 2 (l = 2, r = 3, val = 2):**
56+
* Decrement the values at indices `[2, 3]` by 2.
57+
* The array will become `[0, 0, 0, 0, 1]`.
58+
* **For query 3 (l = 3, r = 4, val = 1):**
59+
* Decrement the value at index 4 by 1.
60+
* The array will become `[0, 0, 0, 0, 0]`. Therefore, the minimum value of `k` is 4.
61+
62+
**Example 4:**
63+
64+
**Input:** nums = [1,2,3,2,6], queries = [[0,1,1],[0,2,1],[1,4,2],[4,4,4],[3,4,1],[4,4,5]]
65+
66+
**Output:** 4
67+
68+
**Constraints:**
69+
70+
* `1 <= nums.length <= 10`
71+
* `0 <= nums[i] <= 1000`
72+
* `1 <= queries.length <= 1000`
73+
* <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code>
74+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
75+
* <code>1 <= val<sub>i</sub> <= 10</code>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g3401_3500.s3490_count_beautiful_numbers
2+
3+
// #Hard #2025_03_16_Time_246_ms_(100.00%)_Space_61.00_MB_(100.00%)
4+
5+
class Solution {
6+
fun beautifulNumbers(l: Int, r: Int): Int {
7+
return countBeautiful(r) - countBeautiful(l - 1)
8+
}
9+
10+
private fun countBeautiful(x: Int): Int {
11+
val digits = getCharArray(x)
12+
val dp = HashMap<String?, Int?>()
13+
return solve(0, 1, 0, 1, digits, dp)
14+
}
15+
16+
private fun getCharArray(x: Int): CharArray {
17+
val str = x.toString()
18+
return str.toCharArray()
19+
}
20+
21+
private fun solve(
22+
i: Int,
23+
tight: Int,
24+
sum: Int,
25+
prod: Int,
26+
digits: CharArray,
27+
dp: HashMap<String?, Int?>,
28+
): Int {
29+
if (i == digits.size) {
30+
return if (sum > 0 && prod % sum == 0) {
31+
1
32+
} else {
33+
0
34+
}
35+
}
36+
val str = "$i - $tight - $sum - $prod"
37+
if (dp.containsKey(str)) {
38+
return dp.get(str)!!
39+
}
40+
val limit: Int = if (tight == 1) {
41+
digits[i].code - '0'.code
42+
} else {
43+
9
44+
}
45+
var count = 0
46+
var j = 0
47+
while (j <= limit) {
48+
var newTight = 0
49+
if (tight == 1 && j == limit) {
50+
newTight = 1
51+
}
52+
val newSum = sum + j
53+
val newProd: Int = if (j == 0 && sum == 0) {
54+
1
55+
} else {
56+
prod * j
57+
}
58+
count += solve(i + 1, newTight, newSum, newProd, digits, dp)
59+
j++
60+
}
61+
dp.put(str, count)
62+
return count
63+
}
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
3490\. Count Beautiful Numbers
2+
3+
Hard
4+
5+
You are given two positive integers, `l` and `r`. A positive integer is called **beautiful** if the product of its digits is divisible by the sum of its digits.
6+
7+
Return the count of **beautiful** numbers between `l` and `r`, inclusive.
8+
9+
**Example 1:**
10+
11+
**Input:** l = 10, r = 20
12+
13+
**Output:** 2
14+
15+
**Explanation:**
16+
17+
The beautiful numbers in the range are 10 and 20.
18+
19+
**Example 2:**
20+
21+
**Input:** l = 1, r = 15
22+
23+
**Output:** 10
24+
25+
**Explanation:**
26+
27+
The beautiful numbers in the range are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= l <= r < 10<sup>9</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3401_3500.s3487_maximum_unique_subarray_sum_after_deletion
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxSum() {
10+
assertThat<Int>(Solution().maxSum(intArrayOf(1, 2, 3, 4, 5)), equalTo<Int>(15))
11+
}
12+
13+
@Test
14+
fun maxSum2() {
15+
assertThat<Int>(Solution().maxSum(intArrayOf(1, 1, 0, 1, 1)), equalTo<Int>(1))
16+
}
17+
18+
@Test
19+
fun maxSum3() {
20+
assertThat<Int>(
21+
Solution().maxSum(intArrayOf(1, 2, -1, -2, 1, 0, -1)),
22+
equalTo<Int>(3),
23+
)
24+
}
25+
26+
@Test
27+
fun maxSum4() {
28+
assertThat<Int>(Solution().maxSum(intArrayOf(-100)), equalTo<Int>(-100))
29+
}
30+
}

0 commit comments

Comments
 (0)