Skip to content

Commit bcc751c

Browse files
committed
Added tasks 3658-3661
1 parent ad89705 commit bcc751c

File tree

12 files changed

+535
-0
lines changed

12 files changed

+535
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g3601_3700.s3658_gcd_of_odd_and_even_sums;
2+
3+
// #Easy #Weekly_Contest_464 #2025_08_24_Time_1_ms_(100.00%)_Space_41.16_MB_(100.00%)
4+
5+
public class Solution {
6+
public int gcdOfOddEvenSums(int n) {
7+
return (n < 0) ? -n : n;
8+
}
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3658\. GCD of Odd and Even Sums
2+
3+
Easy
4+
5+
You are given an integer `n`. Your task is to compute the **GCD** (greatest common divisor) of two values:
6+
7+
* `sumOdd`: the sum of the first `n` odd numbers.
8+
9+
* `sumEven`: the sum of the first `n` even numbers.
10+
11+
12+
Return the GCD of `sumOdd` and `sumEven`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 4
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
* Sum of the first 4 odd numbers `sumOdd = 1 + 3 + 5 + 7 = 16`
23+
* Sum of the first 4 even numbers `sumEven = 2 + 4 + 6 + 8 = 20`
24+
25+
Hence, `GCD(sumOdd, sumEven) = GCD(16, 20) = 4`.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
* Sum of the first 5 odd numbers `sumOdd = 1 + 3 + 5 + 7 + 9 = 25`
36+
* Sum of the first 5 even numbers `sumEven = 2 + 4 + 6 + 8 + 10 = 30`
37+
38+
Hence, `GCD(sumOdd, sumEven) = GCD(25, 30) = 5`.
39+
40+
**Constraints:**
41+
42+
* `1 <= n <= 1000`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3601_3700.s3659_partition_array_into_k_distinct_groups;
2+
3+
// #Medium #Weekly_Contest_464 #2025_08_24_Time_81_ms_(100.00%)_Space_62.78_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
7+
public class Solution {
8+
public boolean partitionArray(int[] nums, int k) {
9+
HashMap<Integer, Integer> mpp = new HashMap<>();
10+
for (int x : nums) {
11+
mpp.put(x, mpp.getOrDefault(x, 0) + 1);
12+
}
13+
for (int count : mpp.values()) {
14+
if (count > nums.length / k) {
15+
return false;
16+
}
17+
}
18+
return nums.length % k == 0;
19+
}
20+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3659\. Partition Array Into K-Distinct Groups
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
Your task is to determine whether it is possible to partition all elements of `nums` into one or more groups such that:
8+
9+
* Each group contains **exactly** `k` **distinct** elements.
10+
* Each element in `nums` must be assigned to **exactly** one group.
11+
12+
Return `true` if such a partition is possible, otherwise return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4], k = 2
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
One possible partition is to have 2 groups:
23+
24+
* Group 1: `[1, 2]`
25+
* Group 2: `[3, 4]`
26+
27+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [3,5,2,2], k = 2
32+
33+
**Output:** true
34+
35+
**Explanation:**
36+
37+
One possible partition is to have 2 groups:
38+
39+
* Group 1: `[2, 3]`
40+
* Group 2: `[2, 5]`
41+
42+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [1,5,2,3], k = 3
47+
48+
**Output:** false
49+
50+
**Explanation:**
51+
52+
We cannot form groups of `k = 3` distinct elements using all values exactly once.
53+
54+
**Constraints:**
55+
56+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
57+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
58+
* `1 <= k <= nums.length`
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package g3601_3700.s3660_jump_game_ix;
2+
3+
// #Medium #Weekly_Contest_464 #2025_08_24_Time_248_ms_(100.00%)_Space_72.98_MB_(100.00%)
4+
5+
import java.util.ArrayDeque;
6+
import java.util.HashMap;
7+
8+
public class Solution {
9+
public int[] maxValue(int[] nums) {
10+
int n = nums.length;
11+
ArrayDeque<Integer> st = new ArrayDeque<>();
12+
UnionFind uf = new UnionFind(n);
13+
for (int i = 0; i < n; i++) {
14+
int prev = i;
15+
if (!st.isEmpty()) {
16+
prev = st.peek();
17+
}
18+
while (!st.isEmpty() && nums[i] < nums[st.peek()]) {
19+
uf.union(st.pop(), i);
20+
}
21+
if (nums[i] > nums[prev]) {
22+
st.push(i);
23+
} else {
24+
st.push(prev);
25+
}
26+
}
27+
st.clear();
28+
for (int i = n - 1; i >= 0; i--) {
29+
int prev = i;
30+
if (!st.isEmpty()) {
31+
prev = st.peek();
32+
}
33+
while (!st.isEmpty() && nums[i] > nums[st.peek()]) {
34+
uf.union(st.pop(), i);
35+
}
36+
if (nums[i] < nums[prev]) {
37+
st.push(i);
38+
} else {
39+
st.push(prev);
40+
}
41+
}
42+
HashMap<Integer, Integer> map = new HashMap<>();
43+
for (int i = 0; i < n; i++) {
44+
int root = uf.find(i);
45+
map.put(root, Math.max(map.getOrDefault(root, Integer.MIN_VALUE), nums[i]));
46+
}
47+
int[] ans = new int[n];
48+
for (int i = 0; i < n; i++) {
49+
ans[i] = map.get(uf.find(i));
50+
}
51+
return ans;
52+
}
53+
54+
private static class UnionFind {
55+
int[] par;
56+
int[] rank;
57+
58+
UnionFind(int n) {
59+
par = new int[n];
60+
rank = new int[n];
61+
for (int i = 0; i < n; i++) {
62+
par[i] = i;
63+
}
64+
}
65+
66+
int find(int x) {
67+
if (par[x] != x) {
68+
par[x] = find(par[x]);
69+
}
70+
return par[x];
71+
}
72+
73+
void union(int x, int y) {
74+
x = find(x);
75+
y = find(y);
76+
if (x == y) {
77+
return;
78+
}
79+
if (rank[x] < rank[y]) {
80+
par[x] = y;
81+
} else if (rank[x] > rank[y]) {
82+
par[y] = x;
83+
} else {
84+
par[y] = x;
85+
rank[x]++;
86+
}
87+
}
88+
}
89+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3660\. Jump Game IX
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
From any index `i`, you can jump to another index `j` under the following rules:
8+
9+
* Jump to index `j` where `j > i` is allowed only if `nums[j] < nums[i]`.
10+
* Jump to index `j` where `j < i` is allowed only if `nums[j] > nums[i]`.
11+
12+
For each index `i`, find the **maximum** **value** in `nums` that can be reached by following **any** sequence of valid jumps starting at `i`.
13+
14+
Return an array `ans` where `ans[i]` is the **maximum** **value** reachable starting from index `i`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,1,3]
19+
20+
**Output:** [2,2,3]
21+
22+
**Explanation:**
23+
24+
* For `i = 0`: No jump increases the value.
25+
* For `i = 1`: Jump to `j = 0` as `nums[j] = 2` is greater than `nums[i]`.
26+
* For `i = 2`: Since `nums[2] = 3` is the maximum value in `nums`, no jump increases the value.
27+
28+
Thus, `ans = [2, 2, 3]`.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,3,1]
33+
34+
**Output:** [3,3,3]
35+
36+
**Explanation:**
37+
38+
* For `i = 0`: Jump forward to `j = 2` as `nums[j] = 1` is less than `nums[i] = 2`, then from `i = 2` jump to `j = 1` as `nums[j] = 3` is greater than `nums[2]`.
39+
* For `i = 1`: Since `nums[1] = 3` is the maximum value in `nums`, no jump increases the value.
40+
* For `i = 2`: Jump to `j = 1` as `nums[j] = 3` is greater than `nums[2] = 1`.
41+
42+
Thus, `ans = [3, 3, 3]`.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
47+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package g3601_3700.s3661_maximum_walls_destroyed_by_robots;
2+
3+
// #Hard #Weekly_Contest_464 #2025_08_24_Time_164_ms_(100.00%)_Space_59.97_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxWalls(int[] robots, int[] distance, int[] walls) {
9+
int n = robots.length;
10+
// Pair robots with distances and sort
11+
int[][] rpair = new int[n][2];
12+
for (int i = 0; i < n; i++) {
13+
rpair[i][0] = robots[i];
14+
rpair[i][1] = distance[i];
15+
}
16+
Arrays.sort(rpair, (a, b) -> Integer.compare(a[0], b[0]));
17+
int[] r = new int[n];
18+
int[] d = new int[n];
19+
for (int i = 0; i < n; i++) {
20+
r[i] = rpair[i][0];
21+
d[i] = rpair[i][1];
22+
}
23+
Arrays.sort(walls);
24+
// Count walls at robot positions
25+
int base = 0;
26+
for (int i = 0; i < n; i++) {
27+
int idx = Arrays.binarySearch(walls, r[i]);
28+
if (idx >= 0) {
29+
base++;
30+
}
31+
}
32+
// Tail walls
33+
int leftTail = countRange(walls, r[0] - d[0], r[0] - 1);
34+
int rightTail = countRange(walls, r[n - 1] + 1, r[n - 1] + d[n - 1]);
35+
// Precompute segment ranges
36+
int segs = n - 1;
37+
int max = Math.max(0, segs);
38+
int[] a = new int[max];
39+
int[] b = new int[max];
40+
int[] c = new int[max];
41+
for (int i = 0; i < segs; i++) {
42+
int segL = r[i] + 1;
43+
int segR = r[i + 1] - 1;
44+
if (segL > segR) {
45+
a[i] = b[i] = c[i] = 0;
46+
continue;
47+
}
48+
int aHigh = Math.min(segR, r[i] + d[i]);
49+
a[i] = countRange(walls, segL, aHigh);
50+
int bLow = Math.max(segL, r[i + 1] - d[i + 1]);
51+
b[i] = countRange(walls, bLow, segR);
52+
int cLow = Math.max(segL, r[i + 1] - d[i + 1]);
53+
int cHigh = Math.min(segR, r[i] + d[i]);
54+
c[i] = countRange(walls, cLow, cHigh);
55+
}
56+
int[][] dp = new int[n][2];
57+
Arrays.fill(dp[0], Integer.MIN_VALUE / 4);
58+
// first fires left
59+
dp[0][0] = base + leftTail;
60+
// first fires right
61+
dp[0][1] = base;
62+
for (int i = 0; i < n - 1; i++) {
63+
Arrays.fill(dp[i + 1], Integer.MIN_VALUE / 4);
64+
for (int choice = 0; choice <= 1; choice++) {
65+
int cur = dp[i][choice];
66+
if (cur < 0) {
67+
continue;
68+
}
69+
int addIfNextLeft = (choice == 1) ? a[i] + b[i] - c[i] : b[i];
70+
dp[i + 1][0] = Math.max(dp[i + 1][0], cur + addIfNextLeft);
71+
int addIfNextRight = (choice == 1) ? a[i] : 0;
72+
dp[i + 1][1] = Math.max(dp[i + 1][1], cur + addIfNextRight);
73+
}
74+
}
75+
int res;
76+
if (n == 1) {
77+
res = Math.max(dp[0][0], dp[0][1] + rightTail);
78+
} else {
79+
res = Math.max(dp[n - 1][0], dp[n - 1][1] + rightTail);
80+
}
81+
return res;
82+
}
83+
84+
private int countRange(int[] arr, long l, long r) {
85+
if (l > r || arr.length == 0) {
86+
return 0;
87+
}
88+
int leftIdx = lowerBound(arr, l);
89+
int rightIdx = upperBound(arr, r);
90+
return Math.max(0, rightIdx - leftIdx);
91+
}
92+
93+
private int lowerBound(int[] a, long x) {
94+
int l = 0;
95+
int r = a.length;
96+
while (l < r) {
97+
int m = (l + r) >>> 1;
98+
if (a[m] < x) {
99+
l = m + 1;
100+
} else {
101+
r = m;
102+
}
103+
}
104+
return l;
105+
}
106+
107+
private int upperBound(int[] a, long x) {
108+
int l = 0;
109+
int r = a.length;
110+
while (l < r) {
111+
int m = (l + r) >>> 1;
112+
if (a[m] <= x) {
113+
l = m + 1;
114+
} else {
115+
r = m;
116+
}
117+
}
118+
return l;
119+
}
120+
}

0 commit comments

Comments
 (0)