Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d9dbaba

Browse files
committedMar 16, 2025·
Added tasks 3487-3490
1 parent 569d288 commit d9dbaba

File tree

12 files changed

+474
-0
lines changed

12 files changed

+474
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3401_3500.s3487_maximum_unique_subarray_sum_after_deletion;
2+
3+
// #Easy #2025_03_16_Time_2_ms_(100.00%)_Space_42.42_MB_(100.00%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public int maxSum(int[] nums) {
10+
int sum = 0;
11+
Set<Integer> st = new HashSet<>();
12+
int mxNeg = Integer.MIN_VALUE;
13+
for (int num : nums) {
14+
if (num > 0) {
15+
st.add(num);
16+
} else {
17+
mxNeg = Math.max(mxNeg, num);
18+
}
19+
}
20+
for (int val : st) {
21+
sum += val;
22+
}
23+
if (!st.isEmpty()) {
24+
return sum;
25+
} else {
26+
return mxNeg;
27+
}
28+
}
29+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3401_3500.s3488_closest_equal_element_queries;
2+
3+
// #Medium #2025_03_16_Time_50_ms_(100.00%)_Space_78.29_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class Solution {
11+
public List<Integer> solveQueries(int[] nums, int[] queries) {
12+
int sz = nums.length;
13+
Map<Integer, List<Integer>> indices = new HashMap<>();
14+
for (int i = 0; i < sz; i++) {
15+
indices.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
16+
}
17+
for (List<Integer> arr : indices.values()) {
18+
int m = arr.size();
19+
if (m == 1) {
20+
nums[arr.get(0)] = -1;
21+
continue;
22+
}
23+
for (int i = 0; i < m; i++) {
24+
int j = arr.get(i);
25+
int f = arr.get((i + 1) % m);
26+
int b = arr.get((i - 1 + m) % m);
27+
int forward = Math.min((sz - j - 1) + f + 1, Math.abs(j - f));
28+
int backward = Math.min(Math.abs(b - j), j + (sz - b));
29+
nums[j] = Math.min(backward, forward);
30+
}
31+
}
32+
List<Integer> res = new ArrayList<>();
33+
for (int q : queries) {
34+
res.add(nums[q]);
35+
}
36+
return res;
37+
}
38+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3401_3500.s3489_zero_array_transformation_iv;
2+
3+
// #Medium #2025_03_16_Time_136_ms_(100.00%)_Space_55.84_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private int solve(int[][] q, int i, int target, int k, int[][] dp) {
9+
// we found a valid sum equal to target so return current index of query.
10+
if (target == 0) {
11+
return k;
12+
}
13+
// return a larger number to invalidate this flow
14+
if (k >= q.length || target < 0) {
15+
return q.length + 1;
16+
}
17+
if (dp[target][k] != -1) {
18+
return dp[target][k];
19+
}
20+
// skip current query val
21+
int res = solve(q, i, target, k + 1, dp);
22+
// pick the val if its range is in the range of target index
23+
if (q[k][0] <= i && i <= q[k][1]) {
24+
res = Math.min(res, solve(q, i, target - q[k][2], k + 1, dp));
25+
}
26+
return dp[target][k] = res;
27+
}
28+
29+
public int minZeroArray(int[] nums, int[][] queries) {
30+
int ans = -1;
31+
for (int i = 0; i < nums.length; ++i) {
32+
int[][] dp = new int[nums[i] + 1][queries.length];
33+
Arrays.stream(dp).forEach(row -> Arrays.fill(row, -1));
34+
ans = Math.max(ans, solve(queries, i, nums[i], 0, dp));
35+
}
36+
return (ans > queries.length) ? -1 : ans;
37+
}
38+
}
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>
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_512_ms_(100.00%)_Space_55.59_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
7+
public class Solution {
8+
public int beautifulNumbers(int l, int r) {
9+
return countBeautiful(r) - countBeautiful(l - 1);
10+
}
11+
12+
private int countBeautiful(int x) {
13+
if (x < 0) {
14+
return 0;
15+
}
16+
char[] digits = getCharArray(x);
17+
HashMap<String, Integer> dp = new HashMap<>();
18+
return solve(0, 1, 0, 1, digits, dp);
19+
}
20+
21+
private char[] getCharArray(int x) {
22+
String str = String.valueOf(x);
23+
return str.toCharArray();
24+
}
25+
26+
private int solve(
27+
int i, int tight, int sum, int prod, char[] digits, HashMap<String, Integer> dp) {
28+
if (i == digits.length) {
29+
if (sum > 0 && prod % sum == 0) {
30+
return 1;
31+
} else {
32+
return 0;
33+
}
34+
}
35+
String str = i + " - " + tight + " - " + sum + " - " + prod;
36+
if (dp.containsKey(str)) {
37+
return dp.get(str);
38+
}
39+
int limit;
40+
if (tight == 1) {
41+
limit = digits[i] - '0';
42+
} else {
43+
limit = 9;
44+
}
45+
int count = 0, j = 0;
46+
while (j <= limit) {
47+
int newTight = 0;
48+
if (tight == 1 && j == limit) {
49+
newTight = 1;
50+
}
51+
int newSum = sum + j;
52+
int newProd;
53+
if (j == 0 && sum == 0) {
54+
newProd = 1;
55+
} else {
56+
newProd = 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+
}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3401_3500.s3487_maximum_unique_subarray_sum_after_deletion;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxSum() {
11+
assertThat(new Solution().maxSum(new int[] {1, 2, 3, 4, 5}), equalTo(15));
12+
}
13+
14+
@Test
15+
void maxSum2() {
16+
assertThat(new Solution().maxSum(new int[] {1, 1, 0, 1, 1}), equalTo(1));
17+
}
18+
19+
@Test
20+
void maxSum3() {
21+
assertThat(new Solution().maxSum(new int[] {1, 2, -1, -2, 1, 0, -1}), equalTo(3));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3401_3500.s3488_closest_equal_element_queries;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void solveQueries() {
12+
assertThat(
13+
new Solution().solveQueries(new int[] {1, 3, 1, 4, 1, 3, 2}, new int[] {0, 3, 5}),
14+
equalTo(List.of(2, -1, 3)));
15+
}
16+
17+
@Test
18+
void solveQueries2() {
19+
assertThat(
20+
new Solution().solveQueries(new int[] {1, 2, 3, 4}, new int[] {0, 1, 2, 3}),
21+
equalTo(List.of(-1, -1, -1, -1)));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3401_3500.s3489_zero_array_transformation_iv;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minZeroArray() {
11+
assertThat(
12+
new Solution()
13+
.minZeroArray(
14+
new int[] {2, 0, 2}, new int[][] {{0, 2, 1}, {0, 2, 1}, {1, 1, 3}}),
15+
equalTo(2));
16+
}
17+
18+
@Test
19+
void minZeroArray2() {
20+
assertThat(
21+
new Solution()
22+
.minZeroArray(new int[] {4, 3, 2, 1}, new int[][] {{1, 3, 2}, {0, 2, 1}}),
23+
equalTo(-1));
24+
}
25+
26+
@Test
27+
void minZeroArray3() {
28+
assertThat(
29+
new Solution()
30+
.minZeroArray(
31+
new int[] {1, 2, 3, 2, 1},
32+
new int[][] {
33+
{0, 1, 1}, {1, 2, 1}, {2, 3, 2}, {3, 4, 1}, {4, 4, 1}
34+
}),
35+
equalTo(4));
36+
}
37+
38+
@Test
39+
void minZeroArray4() {
40+
assertThat(
41+
new Solution()
42+
.minZeroArray(
43+
new int[] {1, 2, 3, 2, 6},
44+
new int[][] {
45+
{0, 1, 1}, {0, 2, 1}, {1, 4, 2}, {4, 4, 4}, {3, 4, 1}, {4, 4, 5}
46+
}),
47+
equalTo(4));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3401_3500.s3490_count_beautiful_numbers;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void beautifulNumbers() {
11+
assertThat(new Solution().beautifulNumbers(10, 20), equalTo(2));
12+
}
13+
14+
@Test
15+
void beautifulNumbers2() {
16+
assertThat(new Solution().beautifulNumbers(1, 15), equalTo(10));
17+
}
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.