Skip to content

Commit 5d76ba7

Browse files
authored
Added tasks 3678-3686
1 parent becae2c commit 5d76ba7

File tree

24 files changed

+790
-0
lines changed

24 files changed

+790
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3601_3700.s3678_smallest_absent_positive_greater_than_average;
2+
3+
// #Easy #Biweekly_Contest_165 #2025_09_20_Time_2_ms_(100.00%)_Space_45.28_MB_(53.71%)
4+
5+
public class Solution {
6+
public int smallestAbsent(int[] nums) {
7+
int sum = 0;
8+
for (int j : nums) {
9+
sum += j;
10+
}
11+
double avg = (double) sum / nums.length;
12+
int num;
13+
if (avg < 0) {
14+
num = 1;
15+
} else {
16+
num = (int) avg + 1;
17+
}
18+
while (true) {
19+
boolean flag = false;
20+
for (int j : nums) {
21+
if (num == j) {
22+
flag = true;
23+
break;
24+
}
25+
}
26+
if (!flag && num > avg) {
27+
return num;
28+
}
29+
num++;
30+
}
31+
}
32+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3601_3700.s3679_minimum_discards_to_balance_inventory;
2+
3+
// #Medium #Biweekly_Contest_165 #2025_09_20_Time_2_ms_(100.00%)_Space_60.72_MB_(75.43%)
4+
5+
public class Solution {
6+
public int minArrivalsToDiscard(int[] arrivals, int w, int m) {
7+
int n = arrivals.length;
8+
int dis = 0;
9+
boolean[] removed = new boolean[n];
10+
int maxVal = 0;
11+
for (int v : arrivals) {
12+
maxVal = Math.max(maxVal, v);
13+
}
14+
int[] freq = new int[maxVal + 1];
15+
for (int i = 0; i < n; i++) {
16+
int outIdx = i - w;
17+
if (outIdx >= 0 && !removed[outIdx]) {
18+
int oldVal = arrivals[outIdx];
19+
freq[oldVal]--;
20+
}
21+
int val = arrivals[i];
22+
if (freq[val] >= m) {
23+
dis++;
24+
removed[i] = true;
25+
} else {
26+
freq[val]++;
27+
}
28+
}
29+
return dis;
30+
}
31+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3601_3700.s3680_generate_schedule;
2+
3+
// #Medium #Biweekly_Contest_165 #2025_09_20_Time_2_ms_(100.00%)_Space_45.33_MB_(59.29%)
4+
5+
public class Solution {
6+
public int[][] generateSchedule(int n) {
7+
if (n < 5) {
8+
return new int[0][];
9+
}
10+
int[][] res = new int[n * (n - 1)][];
11+
int idx = 0;
12+
for (int i = 2; i < n - 1; i++) {
13+
for (int j = 0; j < n; j++) {
14+
res[idx++] = new int[] {j, (j + i) % n};
15+
}
16+
}
17+
for (int i = 0; i < n; i++) {
18+
res[idx++] = new int[] {i, (i + 1) % n};
19+
res[idx++] = new int[] {(i + 4) % n, (i + 3) % n};
20+
}
21+
return res;
22+
}
23+
}
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_20_Time_27_ms_(99.73%)_Space_58.35_MB_(30.31%)
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3601_3700.s3683_earliest_time_to_finish_one_task;
2+
3+
// #Easy #Weekly_Contest_467 #2025_09_20_Time_0_ms_(100.00%)_Space_45.29_MB_(39.62%)
4+
5+
public class Solution {
6+
public int earliestTime(int[][] tasks) {
7+
int ans = 1000;
8+
for (int i = 0; i < tasks.length; i++) {
9+
int st = tasks[i][0];
10+
int tm = tasks[i][1];
11+
ans = Math.min(ans, st + tm);
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3683\. Earliest Time to Finish One Task
2+
3+
Easy
4+
5+
You are given a 2D integer array `tasks` where <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>.
6+
7+
Each <code>[s<sub>i</sub>, t<sub>i</sub>]</code> in `tasks` represents a task with start time <code>s<sub>i</sub></code> that takes <code>t<sub>i</sub></code> units of time to finish.
8+
9+
Return the earliest time at which at least one task is finished.
10+
11+
**Example 1:**
12+
13+
**Input:** tasks = [[1,6],[2,3]]
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
The first task starts at time `t = 1` and finishes at time `1 + 6 = 7`. The second task finishes at time `2 + 3 = 5`. You can finish one task at time 5.
20+
21+
**Example 2:**
22+
23+
**Input:** tasks = [[100,100],[100,100],[100,100]]
24+
25+
**Output:** 200
26+
27+
**Explanation:**
28+
29+
All three tasks finish at time `100 + 100 = 200`.
30+
31+
**Constraints:**
32+
33+
* `1 <= tasks.length <= 100`
34+
* <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>
35+
* <code>1 <= s<sub>i</sub>, t<sub>i</sub> <= 100</code>

0 commit comments

Comments
 (0)