Skip to content

Commit 0fcbaca

Browse files
committed
Added tasks 3678-3686
1 parent 77dc0ee commit 0fcbaca

File tree

24 files changed

+814
-0
lines changed

24 files changed

+814
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3601_3700.s3678_smallest_absent_positive_greater_than_average;
2+
3+
// #Easy #Biweekly_Contest_165 #2025_09_14_Time_10_ms_(100.00%)_Space_45.27_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public int smallestAbsent(int[] nums) {
11+
double avg;
12+
double sum = 0;
13+
Set<Integer> set = new HashSet<>();
14+
for (int num : nums) {
15+
sum += num;
16+
set.add(num);
17+
}
18+
int n = nums.length;
19+
Arrays.sort(nums);
20+
avg = sum / n;
21+
double j = avg < 0 ? 1 : Math.ceil(avg) == avg ? avg + 1 : Math.ceil(avg);
22+
for (int i = (int) j; i <= 100; i++) {
23+
if (!set.contains(i)) {
24+
return i;
25+
}
26+
}
27+
return nums[n - 1] + 1;
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3678\. Smallest Absent Positive Greater Than Average
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
Return the **smallest absent positive** integer in `nums` such that it is **strictly greater** than the **average** of all elements in `nums`.
8+
9+
The **average** of an array is defined as the sum of all its elements divided by the number of elements.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,5]
14+
15+
**Output:** 6
16+
17+
**Explanation:**
18+
19+
* The average of `nums` is `(3 + 5) / 2 = 8 / 2 = 4`.
20+
* The smallest absent positive integer greater than 4 is 6.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [-1,1,2]
25+
26+
**Output:** 3
27+
28+
**Explanation:**
29+
30+
* The average of `nums` is `(-1 + 1 + 2) / 3 = 2 / 3 = 0.667`.
31+
* The smallest absent positive integer greater than 0.667 is 3.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [4,-1]
36+
37+
**Output:** 2
38+
39+
**Explanation:**
40+
41+
* The average of `nums` is `(4 + (-1)) / 2 = 3 / 2 = 1.50`.
42+
* The smallest absent positive integer greater than 1.50 is 2.
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 100`
47+
* `-100 <= nums[i] <= 100`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3601_3700.s3679_minimum_discards_to_balance_inventory;
2+
3+
// #Medium #Biweekly_Contest_165 #2025_09_14_Time_30_ms_(100.00%)_Space_61.57_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
10+
int n = arrivals.length;
11+
if (n == 0) {
12+
return 0;
13+
}
14+
Map<Integer, Integer> map = new HashMap<>();
15+
int[] kept = new int[n];
16+
int dis = 0;
17+
for (int i = 0; i < n; i++) {
18+
int idx = i - w;
19+
if (idx >= 0 && kept[idx] == 1) {
20+
map.put(arrivals[idx], map.get(arrivals[idx]) - 1);
21+
}
22+
int t = arrivals[i];
23+
if (map.getOrDefault(t, 0) < m) {
24+
kept[i] = 1;
25+
map.put(t, map.getOrDefault(t, 0) + 1);
26+
} else {
27+
dis++;
28+
}
29+
}
30+
return dis;
31+
}
32+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3679\. Minimum Discards to Balance Inventory
2+
3+
Medium
4+
5+
You are given two integers `w` and `m`, and an integer array `arrivals`, where `arrivals[i]` is the type of item arriving on day `i` (days are **1-indexed**).
6+
7+
Items are managed according to the following rules:
8+
9+
* Each arrival may be **kept** or **discarded**; an item may only be discarded on its arrival day.
10+
* For each day `i`, consider the window of days `[max(1, i - w + 1), i]` (the `w` most recent days up to day `i`):
11+
* For **any** such window, each item type may appear **at most** `m` times among kept arrivals whose arrival day lies in that window.
12+
* If keeping the arrival on day `i` would cause its type to appear **more than** `m` times in the window, that arrival **must** be discarded.
13+
14+
Return the **minimum** number of arrivals to be discarded so that every `w`\-day window contains at most `m` occurrences of each type.
15+
16+
**Example 1:**
17+
18+
**Input:** arrivals = [1,2,1,3,1], w = 4, m = 2
19+
20+
**Output:** 0
21+
22+
**Explanation:**
23+
24+
* On day 1, Item 1 arrives; the window contains no more than `m` occurrences of this type, so we keep it.
25+
* On day 2, Item 2 arrives; the window of days 1 - 2 is fine.
26+
* On day 3, Item 1 arrives, window `[1, 2, 1]` has item 1 twice, within limit.
27+
* On day 4, Item 3 arrives, window `[1, 2, 1, 3]` has item 1 twice, allowed.
28+
* On day 5, Item 1 arrives, window `[2, 1, 3, 1]` has item 1 twice, still valid.
29+
30+
There are no discarded items, so return 0.
31+
32+
**Example 2:**
33+
34+
**Input:** arrivals = [1,2,3,3,3,4], w = 3, m = 2
35+
36+
**Output:** 1
37+
38+
**Explanation:**
39+
40+
* On day 1, Item 1 arrives. We keep it.
41+
* On day 2, Item 2 arrives, window `[1, 2]` is fine.
42+
* On day 3, Item 3 arrives, window `[1, 2, 3]` has item 3 once.
43+
* On day 4, Item 3 arrives, window `[2, 3, 3]` has item 3 twice, allowed.
44+
* On day 5, Item 3 arrives, window `[3, 3, 3]` has item 3 three times, exceeds limit, so the arrival must be discarded.
45+
* On day 6, Item 4 arrives, window `[3, 4]` is fine.
46+
47+
Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= arrivals.length <= 10<sup>5</sup></code>
52+
* <code>1 <= arrivals[i] <= 10<sup>5</sup></code>
53+
* `1 <= w <= arrivals.length`
54+
* `1 <= m <= w`
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g3601_3700.s3680_generate_schedule;
2+
3+
// #Medium #Biweekly_Contest_165 #2025_09_14_Time_67_ms_(100.00%)_Space_45.58_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
8+
public class Solution {
9+
public int[][] generateSchedule(int n) {
10+
int[][] res = new int[n * (n - 1)][2];
11+
boolean[][] matches = new boolean[n][n];
12+
res[0] = new int[] {0, 1};
13+
matches[0][1] = true;
14+
int[] matchesCount = new int[n];
15+
matchesCount[0] = 1;
16+
matchesCount[1] = 1;
17+
if (backtrack(n, matches, res, 1, matchesCount)) {
18+
return res;
19+
}
20+
return new int[][] {};
21+
}
22+
23+
private boolean backtrack(
24+
int n, boolean[][] matches, int[][] result, int cur, int[] matchesCount) {
25+
if (cur == result.length) {
26+
return true;
27+
}
28+
Integer[] teams = new Integer[n];
29+
for (int i = 0; i < n; i++) {
30+
teams[i] = i;
31+
}
32+
Arrays.sort(teams, Comparator.comparingInt(a -> matchesCount[a]));
33+
for (int i = 0; i < n; i++) {
34+
if (result[cur - 1][0] != teams[i] && result[cur - 1][1] != teams[i]) {
35+
int team1 = -1;
36+
int team2 = -1;
37+
for (int j = 0; j < n; j++) {
38+
if (i != j
39+
&& !matches[teams[i]][teams[j]]
40+
&& result[cur - 1][0] != teams[j]
41+
&& result[cur - 1][1] != teams[j]) {
42+
team1 = teams[i];
43+
team2 = teams[j];
44+
break;
45+
}
46+
}
47+
if (team1 != -1) {
48+
result[cur] = new int[] {team1, team2};
49+
matches[team1][team2] = true;
50+
matchesCount[team1]++;
51+
matchesCount[team2]++;
52+
boolean found = backtrack(n, matches, result, cur + 1, matchesCount);
53+
if (found) {
54+
return true;
55+
} else {
56+
matches[team1][team2] = false;
57+
result[cur] = new int[] {0, 0};
58+
matchesCount[team1]--;
59+
matchesCount[team2]--;
60+
}
61+
}
62+
}
63+
}
64+
return false;
65+
}
66+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3680\. Generate Schedule
2+
3+
Medium
4+
5+
You are given an integer `n` representing `n` teams. You are asked to generate a schedule such that:
6+
7+
* Each team plays every other team **exactly twice**: once at home and once away.
8+
* There is **exactly one** match per day; the schedule is a list of **consecutive** days and `schedule[i]` is the match on day `i`.
9+
* No team plays on **consecutive** days.
10+
11+
Return a 2D integer array `schedule`, where `schedule[i][0]` represents the home team and `schedule[i][1]` represents the away team. If multiple schedules meet the conditions, return **any** one of them.
12+
13+
If no schedule exists that meets the conditions, return an empty array.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 3
18+
19+
**Output:** []
20+
21+
**Explanation:**
22+
23+
Since each team plays every other team exactly twice, a total of 6 matches need to be played: `[0,1],[0,2],[1,2],[1,0],[2,0],[2,1]`.
24+
25+
It's not possible to create a schedule without at least one team playing consecutive days.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5
30+
31+
**Output:** [[0,1],[2,3],[0,4],[1,2],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[2,0],[3,1],[4,0],[2,1],[4,3],[1,0],[3,2],[4,1],[3,0],[4,2]]
32+
33+
**Explanation:**
34+
35+
Since each team plays every other team exactly twice, a total of 20 matches need to be played.
36+
37+
The output shows one of the schedules that meet the conditions. No team plays on consecutive days.
38+
39+
**Constraints:**
40+
41+
* `2 <= n <= 50`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3601_3700.s3681_maximum_xor_of_subsequences;
2+
3+
// #Hard #Biweekly_Contest_165 #2025_09_14_Time_29_ms_(100.00%)_Space_58.02_MB_(100.00%)
4+
5+
public class Solution {
6+
public int maxXorSubsequences(int[] nums) {
7+
int n = nums.length;
8+
if (n == 0) {
9+
return 0;
10+
}
11+
int x = 0;
12+
while (true) {
13+
int y = 0;
14+
for (int v : nums) {
15+
if (v > y) {
16+
y = v;
17+
}
18+
}
19+
if (y == 0) {
20+
return x;
21+
}
22+
x = Math.max(x, x ^ y);
23+
for (int i = 0; i < n; i++) {
24+
int v = nums[i];
25+
nums[i] = Math.min(v, v ^ y);
26+
}
27+
}
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3681\. Maximum XOR of Subsequences
2+
3+
Hard
4+
5+
You are given an integer array `nums` of length `n` where each element is a non-negative integer.
6+
7+
Select **two** **subsequences** of `nums` (they may be empty and are **allowed** to **overlap**), each preserving the original order of elements, and let:
8+
9+
* `X` be the bitwise XOR of all elements in the first subsequence.
10+
* `Y` be the bitwise XOR of all elements in the second subsequence.
11+
12+
Return the **maximum** possible value of `X XOR Y`.
13+
14+
**Note:** The XOR of an **empty** subsequence is 0.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3]
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
Choose subsequences:
25+
26+
* First subsequence `[2]`, whose XOR is 2.
27+
* Second subsequence `[2,3]`, whose XOR is 1.
28+
29+
Then, XOR of both subsequences = `2 XOR 1 = 3`.
30+
31+
This is the maximum XOR value achievable from any two subsequences.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [5,2]
36+
37+
**Output:** 7
38+
39+
**Explanation:**
40+
41+
Choose subsequences:
42+
43+
* First subsequence `[5]`, whose XOR is 5.
44+
* Second subsequence `[2]`, whose XOR is 2.
45+
46+
Then, XOR of both subsequences = `5 XOR 2 = 7`.
47+
48+
This is the maximum XOR value achievable from any two subsequences.
49+
50+
**Constraints:**
51+
52+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3601_3700.s3683_earliest_time_to_finish_one_task;
2+
3+
// #Easy #Weekly_Contest_467 #2025_09_14_Time_1_ms_(100.00%)_Space_44.90_MB_(100.00%)
4+
5+
public class Solution {
6+
public int earliestTime(int[][] tasks) {
7+
if (tasks.length == 1) {
8+
int sum = 0;
9+
for (int t : tasks[0]) {
10+
sum += t;
11+
}
12+
return sum;
13+
}
14+
int minTask = 0;
15+
for (int t : tasks[0]) {
16+
minTask += t;
17+
}
18+
for (int i = 1; i < tasks.length; i++) {
19+
int sum = 0;
20+
for (int t : tasks[i]) {
21+
sum += t;
22+
}
23+
if (sum < minTask) {
24+
minTask = sum;
25+
}
26+
}
27+
return minTask;
28+
}
29+
}

0 commit comments

Comments
 (0)