From 0fcbacace4dfa1f8347ae8b72fc2a6b7cf5441a2 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 14 Sep 2025 13:20:46 +0300 Subject: [PATCH 1/6] Added tasks 3678-3686 --- .../Solution.java | 29 ++++++++ .../readme.md | 47 +++++++++++++ .../Solution.java | 32 +++++++++ .../readme.md | 54 +++++++++++++++ .../s3680_generate_schedule/Solution.java | 66 +++++++++++++++++++ .../s3680_generate_schedule/readme.md | 41 ++++++++++++ .../Solution.java | 29 ++++++++ .../readme.md | 53 +++++++++++++++ .../Solution.java | 29 ++++++++ .../readme.md | 35 ++++++++++ .../Solution.java | 30 +++++++++ .../readme.md | 45 +++++++++++++ .../Solution.java | 41 ++++++++++++ .../readme.md | 40 +++++++++++ .../Solution.java | 29 ++++++++ .../readme.md | 38 +++++++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 21 ++++++ .../s3680_generate_schedule/SolutionTest.java | 25 +++++++ .../SolutionTest.java | 18 +++++ .../SolutionTest.java | 20 ++++++ .../SolutionTest.java | 29 ++++++++ .../SolutionTest.java | 22 +++++++ .../SolutionTest.java | 18 +++++ 24 files changed, 814 insertions(+) create mode 100644 src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java create mode 100644 src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/readme.md create mode 100644 src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java create mode 100644 src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/readme.md create mode 100644 src/main/java/g3601_3700/s3680_generate_schedule/Solution.java create mode 100644 src/main/java/g3601_3700/s3680_generate_schedule/readme.md create mode 100644 src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java create mode 100644 src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/readme.md create mode 100644 src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java create mode 100644 src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/readme.md create mode 100644 src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java create mode 100644 src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/readme.md create mode 100644 src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java create mode 100644 src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/readme.md create mode 100644 src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java create mode 100644 src/main/java/g3601_3700/s3686_number_of_stable_subsequences/readme.md create mode 100644 src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3681_maximum_xor_of_subsequences/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3686_number_of_stable_subsequences/SolutionTest.java diff --git a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java new file mode 100644 index 000000000..ebeabe051 --- /dev/null +++ b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java @@ -0,0 +1,29 @@ +package g3601_3700.s3678_smallest_absent_positive_greater_than_average; + +// #Easy #Biweekly_Contest_165 #2025_09_14_Time_10_ms_(100.00%)_Space_45.27_MB_(100.00%) + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Solution { + public int smallestAbsent(int[] nums) { + double avg; + double sum = 0; + Set set = new HashSet<>(); + for (int num : nums) { + sum += num; + set.add(num); + } + int n = nums.length; + Arrays.sort(nums); + avg = sum / n; + double j = avg < 0 ? 1 : Math.ceil(avg) == avg ? avg + 1 : Math.ceil(avg); + for (int i = (int) j; i <= 100; i++) { + if (!set.contains(i)) { + return i; + } + } + return nums[n - 1] + 1; + } +} diff --git a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/readme.md b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/readme.md new file mode 100644 index 000000000..ddcb9ae29 --- /dev/null +++ b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/readme.md @@ -0,0 +1,47 @@ +3678\. Smallest Absent Positive Greater Than Average + +Easy + +You are given an integer array `nums`. + +Return the **smallest absent positive** integer in `nums` such that it is **strictly greater** than the **average** of all elements in `nums`. + +The **average** of an array is defined as the sum of all its elements divided by the number of elements. + +**Example 1:** + +**Input:** nums = [3,5] + +**Output:** 6 + +**Explanation:** + +* The average of `nums` is `(3 + 5) / 2 = 8 / 2 = 4`. +* The smallest absent positive integer greater than 4 is 6. + +**Example 2:** + +**Input:** nums = [-1,1,2] + +**Output:** 3 + +**Explanation:** + +* The average of `nums` is `(-1 + 1 + 2) / 3 = 2 / 3 = 0.667`. +* The smallest absent positive integer greater than 0.667 is 3. + +**Example 3:** + +**Input:** nums = [4,-1] + +**Output:** 2 + +**Explanation:** + +* The average of `nums` is `(4 + (-1)) / 2 = 3 / 2 = 1.50`. +* The smallest absent positive integer greater than 1.50 is 2. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java b/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java new file mode 100644 index 000000000..6d8d55dfb --- /dev/null +++ b/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java @@ -0,0 +1,32 @@ +package g3601_3700.s3679_minimum_discards_to_balance_inventory; + +// #Medium #Biweekly_Contest_165 #2025_09_14_Time_30_ms_(100.00%)_Space_61.57_MB_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minArrivalsToDiscard(int[] arrivals, int w, int m) { + int n = arrivals.length; + if (n == 0) { + return 0; + } + Map map = new HashMap<>(); + int[] kept = new int[n]; + int dis = 0; + for (int i = 0; i < n; i++) { + int idx = i - w; + if (idx >= 0 && kept[idx] == 1) { + map.put(arrivals[idx], map.get(arrivals[idx]) - 1); + } + int t = arrivals[i]; + if (map.getOrDefault(t, 0) < m) { + kept[i] = 1; + map.put(t, map.getOrDefault(t, 0) + 1); + } else { + dis++; + } + } + return dis; + } +} diff --git a/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/readme.md b/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/readme.md new file mode 100644 index 000000000..9030dfd89 --- /dev/null +++ b/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/readme.md @@ -0,0 +1,54 @@ +3679\. Minimum Discards to Balance Inventory + +Medium + +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**). + +Items are managed according to the following rules: + +* Each arrival may be **kept** or **discarded**; an item may only be discarded on its arrival day. +* For each day `i`, consider the window of days `[max(1, i - w + 1), i]` (the `w` most recent days up to day `i`): + * For **any** such window, each item type may appear **at most** `m` times among kept arrivals whose arrival day lies in that window. + * 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. + +Return the **minimum** number of arrivals to be discarded so that every `w`\-day window contains at most `m` occurrences of each type. + +**Example 1:** + +**Input:** arrivals = [1,2,1,3,1], w = 4, m = 2 + +**Output:** 0 + +**Explanation:** + +* On day 1, Item 1 arrives; the window contains no more than `m` occurrences of this type, so we keep it. +* On day 2, Item 2 arrives; the window of days 1 - 2 is fine. +* On day 3, Item 1 arrives, window `[1, 2, 1]` has item 1 twice, within limit. +* On day 4, Item 3 arrives, window `[1, 2, 1, 3]` has item 1 twice, allowed. +* On day 5, Item 1 arrives, window `[2, 1, 3, 1]` has item 1 twice, still valid. + +There are no discarded items, so return 0. + +**Example 2:** + +**Input:** arrivals = [1,2,3,3,3,4], w = 3, m = 2 + +**Output:** 1 + +**Explanation:** + +* On day 1, Item 1 arrives. We keep it. +* On day 2, Item 2 arrives, window `[1, 2]` is fine. +* On day 3, Item 3 arrives, window `[1, 2, 3]` has item 3 once. +* On day 4, Item 3 arrives, window `[2, 3, 3]` has item 3 twice, allowed. +* On day 5, Item 3 arrives, window `[3, 3, 3]` has item 3 three times, exceeds limit, so the arrival must be discarded. +* On day 6, Item 4 arrives, window `[3, 4]` is fine. + +Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1. + +**Constraints:** + +* 1 <= arrivals.length <= 105 +* 1 <= arrivals[i] <= 105 +* `1 <= w <= arrivals.length` +* `1 <= m <= w` \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java b/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java new file mode 100644 index 000000000..b58e6ff43 --- /dev/null +++ b/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java @@ -0,0 +1,66 @@ +package g3601_3700.s3680_generate_schedule; + +// #Medium #Biweekly_Contest_165 #2025_09_14_Time_67_ms_(100.00%)_Space_45.58_MB_(100.00%) + +import java.util.Arrays; +import java.util.Comparator; + +public class Solution { + public int[][] generateSchedule(int n) { + int[][] res = new int[n * (n - 1)][2]; + boolean[][] matches = new boolean[n][n]; + res[0] = new int[] {0, 1}; + matches[0][1] = true; + int[] matchesCount = new int[n]; + matchesCount[0] = 1; + matchesCount[1] = 1; + if (backtrack(n, matches, res, 1, matchesCount)) { + return res; + } + return new int[][] {}; + } + + private boolean backtrack( + int n, boolean[][] matches, int[][] result, int cur, int[] matchesCount) { + if (cur == result.length) { + return true; + } + Integer[] teams = new Integer[n]; + for (int i = 0; i < n; i++) { + teams[i] = i; + } + Arrays.sort(teams, Comparator.comparingInt(a -> matchesCount[a])); + for (int i = 0; i < n; i++) { + if (result[cur - 1][0] != teams[i] && result[cur - 1][1] != teams[i]) { + int team1 = -1; + int team2 = -1; + for (int j = 0; j < n; j++) { + if (i != j + && !matches[teams[i]][teams[j]] + && result[cur - 1][0] != teams[j] + && result[cur - 1][1] != teams[j]) { + team1 = teams[i]; + team2 = teams[j]; + break; + } + } + if (team1 != -1) { + result[cur] = new int[] {team1, team2}; + matches[team1][team2] = true; + matchesCount[team1]++; + matchesCount[team2]++; + boolean found = backtrack(n, matches, result, cur + 1, matchesCount); + if (found) { + return true; + } else { + matches[team1][team2] = false; + result[cur] = new int[] {0, 0}; + matchesCount[team1]--; + matchesCount[team2]--; + } + } + } + } + return false; + } +} diff --git a/src/main/java/g3601_3700/s3680_generate_schedule/readme.md b/src/main/java/g3601_3700/s3680_generate_schedule/readme.md new file mode 100644 index 000000000..71a7cc8c2 --- /dev/null +++ b/src/main/java/g3601_3700/s3680_generate_schedule/readme.md @@ -0,0 +1,41 @@ +3680\. Generate Schedule + +Medium + +You are given an integer `n` representing `n` teams. You are asked to generate a schedule such that: + +* Each team plays every other team **exactly twice**: once at home and once away. +* There is **exactly one** match per day; the schedule is a list of **consecutive** days and `schedule[i]` is the match on day `i`. +* No team plays on **consecutive** days. + +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. + +If no schedule exists that meets the conditions, return an empty array. + +**Example 1:** + +**Input:** n = 3 + +**Output:** [] + +**Explanation:** + +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]`. + +It's not possible to create a schedule without at least one team playing consecutive days. + +**Example 2:** + +**Input:** n = 5 + +**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]] + +**Explanation:** + +Since each team plays every other team exactly twice, a total of 20 matches need to be played. + +The output shows one of the schedules that meet the conditions. No team plays on consecutive days. + +**Constraints:** + +* `2 <= n <= 50` \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java b/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java new file mode 100644 index 000000000..56b085fb2 --- /dev/null +++ b/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java @@ -0,0 +1,29 @@ +package g3601_3700.s3681_maximum_xor_of_subsequences; + +// #Hard #Biweekly_Contest_165 #2025_09_14_Time_29_ms_(100.00%)_Space_58.02_MB_(100.00%) + +public class Solution { + public int maxXorSubsequences(int[] nums) { + int n = nums.length; + if (n == 0) { + return 0; + } + int x = 0; + while (true) { + int y = 0; + for (int v : nums) { + if (v > y) { + y = v; + } + } + if (y == 0) { + return x; + } + x = Math.max(x, x ^ y); + for (int i = 0; i < n; i++) { + int v = nums[i]; + nums[i] = Math.min(v, v ^ y); + } + } + } +} diff --git a/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/readme.md b/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/readme.md new file mode 100644 index 000000000..9d5d32c6e --- /dev/null +++ b/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/readme.md @@ -0,0 +1,53 @@ +3681\. Maximum XOR of Subsequences + +Hard + +You are given an integer array `nums` of length `n` where each element is a non-negative integer. + +Select **two** **subsequences** of `nums` (they may be empty and are **allowed** to **overlap**), each preserving the original order of elements, and let: + +* `X` be the bitwise XOR of all elements in the first subsequence. +* `Y` be the bitwise XOR of all elements in the second subsequence. + +Return the **maximum** possible value of `X XOR Y`. + +**Note:** The XOR of an **empty** subsequence is 0. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 3 + +**Explanation:** + +Choose subsequences: + +* First subsequence `[2]`, whose XOR is 2. +* Second subsequence `[2,3]`, whose XOR is 1. + +Then, XOR of both subsequences = `2 XOR 1 = 3`. + +This is the maximum XOR value achievable from any two subsequences. + +**Example 2:** + +**Input:** nums = [5,2] + +**Output:** 7 + +**Explanation:** + +Choose subsequences: + +* First subsequence `[5]`, whose XOR is 5. +* Second subsequence `[2]`, whose XOR is 2. + +Then, XOR of both subsequences = `5 XOR 2 = 7`. + +This is the maximum XOR value achievable from any two subsequences. + +**Constraints:** + +* 2 <= nums.length <= 105 +* 0 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java b/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java new file mode 100644 index 000000000..06282544a --- /dev/null +++ b/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java @@ -0,0 +1,29 @@ +package g3601_3700.s3683_earliest_time_to_finish_one_task; + +// #Easy #Weekly_Contest_467 #2025_09_14_Time_1_ms_(100.00%)_Space_44.90_MB_(100.00%) + +public class Solution { + public int earliestTime(int[][] tasks) { + if (tasks.length == 1) { + int sum = 0; + for (int t : tasks[0]) { + sum += t; + } + return sum; + } + int minTask = 0; + for (int t : tasks[0]) { + minTask += t; + } + for (int i = 1; i < tasks.length; i++) { + int sum = 0; + for (int t : tasks[i]) { + sum += t; + } + if (sum < minTask) { + minTask = sum; + } + } + return minTask; + } +} diff --git a/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/readme.md b/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/readme.md new file mode 100644 index 000000000..5402f26de --- /dev/null +++ b/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/readme.md @@ -0,0 +1,35 @@ +3683\. Earliest Time to Finish One Task + +Easy + +You are given a 2D integer array `tasks` where tasks[i] = [si, ti]. + +Each [si, ti] in `tasks` represents a task with start time si that takes ti units of time to finish. + +Return the earliest time at which at least one task is finished. + +**Example 1:** + +**Input:** tasks = [[1,6],[2,3]] + +**Output:** 5 + +**Explanation:** + +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. + +**Example 2:** + +**Input:** tasks = [[100,100],[100,100],[100,100]] + +**Output:** 200 + +**Explanation:** + +All three tasks finish at time `100 + 100 = 200`. + +**Constraints:** + +* `1 <= tasks.length <= 100` +* tasks[i] = [si, ti] +* 1 <= si, ti <= 100 \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java new file mode 100644 index 000000000..6f86b35f2 --- /dev/null +++ b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java @@ -0,0 +1,30 @@ +package g3601_3700.s3684_maximize_sum_of_at_most_k_distinct_elements; + +// #Easy #Weekly_Contest_467 #2025_09_14_Time_8_ms_(100.00%)_Space_46.01_MB_(_%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + public int[] maxKDistinct(int[] nums, int k) { + Arrays.sort(nums); + for (int i = 0; i < nums.length / 2; i++) { + int temp = nums[i]; + nums[i] = nums[nums.length - 1 - i]; + nums[nums.length - 1 - i] = temp; + } + List res = new ArrayList<>(); + res.add(nums[0]); + k--; + int i = 1; + while (k > 0 && i < nums.length) { + if (nums[i] != nums[i - 1]) { + res.add(nums[i]); + k--; + } + i++; + } + return res.stream().mapToInt(Integer::intValue).toArray(); + } +} diff --git a/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/readme.md b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/readme.md new file mode 100644 index 000000000..d05c016a7 --- /dev/null +++ b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/readme.md @@ -0,0 +1,45 @@ +3684\. Maximize Sum of At Most K Distinct Elements + +Easy + +You are given a **positive** integer array `nums` and an integer `k`. + +Choose at most `k` elements from `nums` so that their sum is maximized. However, the chosen numbers must be **distinct**. + +Return an array containing the chosen numbers in **strictly descending** order. + +**Example 1:** + +**Input:** nums = [84,93,100,77,90], k = 3 + +**Output:** [100,93,90] + +**Explanation:** + +The maximum sum is 283, which is attained by choosing 93, 100 and 90. We rearrange them in strictly descending order as `[100, 93, 90]`. + +**Example 2:** + +**Input:** nums = [84,93,100,77,93], k = 3 + +**Output:** [100,93,84] + +**Explanation:** + +The maximum sum is 277, which is attained by choosing 84, 93 and 100. We rearrange them in strictly descending order as `[100, 93, 84]`. We cannot choose 93, 100 and 93 because the chosen numbers must be distinct. + +**Example 3:** + +**Input:** nums = [1,1,1,2,2,2], k = 6 + +**Output:** [2,1] + +**Explanation:** + +The maximum sum is 3, which is attained by choosing 1 and 2. We rearrange them in strictly descending order as `[2, 1]`. + +**Constraints:** + +* `1 <= nums.length <= 100` +* 1 <= nums[i] <= 109 +* `1 <= k <= nums.length` \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java new file mode 100644 index 000000000..65e5e1b50 --- /dev/null +++ b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java @@ -0,0 +1,41 @@ +package g3601_3700.s3685_subsequence_sum_after_capping_elements; + +// #Medium #Weekly_Contest_467 #2025_09_14_Time_107_ms_(100.00%)_Space_45.61_MB_(100.00%) + +import java.util.Arrays; + +public class Solution { + private static final int MAX_K = 4001; + private final boolean[] dp = new boolean[MAX_K]; + + public boolean[] subsequenceSumAfterCapping(int[] nums, int k) { + Arrays.sort(nums); + int n = nums.length; + Arrays.fill(dp, false); + dp[0] = true; + int p = 0; + boolean[] ans = new boolean[n]; + for (int i = 1; i <= n; i++) { + while (p < n && nums[p] < i) { + for (int j = k; j >= nums[p]; j--) { + dp[j] |= dp[j - nums[p]]; + } + p++; + } + int cnt = n - p; + for (int j = 0; j <= cnt; j++) { + int weight = i * j; + if (k < weight) { + break; + } + // We can form dp[k - weight], so we can form dp[k] + // by choosing j knapsacks (each has weight of i) + if (dp[k - weight]) { + ans[i - 1] = true; + break; + } + } + } + return ans; + } +} diff --git a/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/readme.md b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/readme.md new file mode 100644 index 000000000..ca0ee673f --- /dev/null +++ b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/readme.md @@ -0,0 +1,40 @@ +3685\. Subsequence Sum After Capping Elements + +Medium + +You are given an integer array `nums` of size `n` and a positive integer `k`. + +An array **capped** by value `x` is obtained by replacing every element `nums[i]` with `min(nums[i], x)`. + +For each integer `x` from 1 to `n`, determine whether it is possible to choose a **subsequence** from the array capped by `x` such that the sum of the chosen elements is **exactly** `k`. + +Return a **0-indexed** boolean array `answer` of size `n`, where `answer[i]` is `true` if it is possible when using `x = i + 1`, and `false` otherwise. + +**Example 1:** + +**Input:** nums = [4,3,2,4], k = 5 + +**Output:** [false,false,true,true] + +**Explanation:** + +* For `x = 1`, the capped array is `[1, 1, 1, 1]`. Possible sums are `1, 2, 3, 4`, so it is impossible to form a sum of `5`. +* For `x = 2`, the capped array is `[2, 2, 2, 2]`. Possible sums are `2, 4, 6, 8`, so it is impossible to form a sum of `5`. +* For `x = 3`, the capped array is `[3, 3, 2, 3]`. A subsequence `[2, 3]` sums to `5`, so it is possible. +* For `x = 4`, the capped array is `[4, 3, 2, 4]`. A subsequence `[3, 2]` sums to `5`, so it is possible. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5], k = 3 + +**Output:** [true,true,true,true,true] + +**Explanation:** + +For every value of `x`, it is always possible to select a subsequence from the capped array that sums exactly to `3`. + +**Constraints:** + +* `1 <= n == nums.length <= 4000` +* `1 <= nums[i] <= n` +* `1 <= k <= 4000` \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java b/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java new file mode 100644 index 000000000..2e250c284 --- /dev/null +++ b/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java @@ -0,0 +1,29 @@ +package g3601_3700.s3686_number_of_stable_subsequences; + +// #Hard #Weekly_Contest_467 #2025_09_14_Time_10_ms_(100.00%)_Space_60.00_MB_(100.00%) + +public class Solution { + private static final long MOD = 1000000007L; + + public int countStableSubsequences(int[] nums) { + long e1 = 0; + long e2 = 0; + long o1 = 0; + long o2 = 0; + for (int x : nums) { + if ((x & 1) == 0) { + long ne1 = (e1 + (o1 + o2 + 1)) % MOD; + long ne2 = (e2 + e1) % MOD; + e1 = ne1; + e2 = ne2; + } else { + long no1 = (o1 + (e1 + e2 + 1)) % MOD; + long no2 = (o2 + o1) % MOD; + o1 = no1; + o2 = no2; + } + } + long ans = (e1 + e2 + o1 + o2) % MOD; + return (int) ans; + } +} diff --git a/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/readme.md b/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/readme.md new file mode 100644 index 000000000..571c59690 --- /dev/null +++ b/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/readme.md @@ -0,0 +1,38 @@ +3686\. Number of Stable Subsequences + +Hard + +You are given an integer array `nums`. + +A **subsequence** is **stable** if it does not contain **three consecutive** elements with the **same** parity when the subsequence is read **in order** (i.e., consecutive **inside the subsequence**). + +Return the number of stable subsequences. + +Since the answer may be too large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** nums = [1,3,5] + +**Output:** 6 + +**Explanation:** + +* Stable subsequences are `[1]`, `[3]`, `[5]`, `[1, 3]`, `[1, 5]`, and `[3, 5]`. +* Subsequence `[1, 3, 5]` is not stable because it contains three consecutive odd numbers. Thus, the answer is 6. + +**Example 2:** + +**Input:** nums = [2,3,4,2] + +**Output:** 14 + +**Explanation:** + +* The only subsequence that is not stable is `[2, 4, 2]`, which contains three consecutive even numbers. +* All other subsequences are stable. Thus, the answer is 14. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java b/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java new file mode 100644 index 000000000..354dd129e --- /dev/null +++ b/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java @@ -0,0 +1,23 @@ +package g3601_3700.s3678_smallest_absent_positive_greater_than_average; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void smallestAbsent() { + assertThat(new Solution().smallestAbsent(new int[] {3, 5}), equalTo(6)); + } + + @Test + void smallestAbsent2() { + assertThat(new Solution().smallestAbsent(new int[] {-1, 1, 2}), equalTo(3)); + } + + @Test + void smallestAbsent3() { + assertThat(new Solution().smallestAbsent(new int[] {4, -1}), equalTo(2)); + } +} diff --git a/src/test/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/SolutionTest.java b/src/test/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/SolutionTest.java new file mode 100644 index 000000000..3e58da0b8 --- /dev/null +++ b/src/test/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/SolutionTest.java @@ -0,0 +1,21 @@ +package g3601_3700.s3679_minimum_discards_to_balance_inventory; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minArrivalsToDiscard() { + assertThat( + new Solution().minArrivalsToDiscard(new int[] {1, 2, 1, 3, 1}, 4, 2), equalTo(0)); + } + + @Test + void minArrivalsToDiscard2() { + assertThat( + new Solution().minArrivalsToDiscard(new int[] {1, 2, 3, 3, 3, 4}, 3, 2), + equalTo(1)); + } +} diff --git a/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java b/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java new file mode 100644 index 000000000..66836e86e --- /dev/null +++ b/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java @@ -0,0 +1,25 @@ +package g3601_3700.s3680_generate_schedule; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void generateSchedule() { + assertThat(new Solution().generateSchedule(3), equalTo(new int[] {})); + } + + @Test + void generateSchedule2() { + assertThat( + new Solution().generateSchedule(5), + equalTo( + new int[][] { + {0, 1}, {2, 3}, {4, 0}, {1, 2}, {3, 4}, {0, 2}, {1, 3}, {4, 2}, {0, 3}, + {1, 4}, {2, 0}, {3, 1}, {0, 4}, {2, 1}, {4, 3}, {1, 0}, {2, 4}, {3, 0}, + {4, 1}, {3, 2} + })); + } +} diff --git a/src/test/java/g3601_3700/s3681_maximum_xor_of_subsequences/SolutionTest.java b/src/test/java/g3601_3700/s3681_maximum_xor_of_subsequences/SolutionTest.java new file mode 100644 index 000000000..77b47075d --- /dev/null +++ b/src/test/java/g3601_3700/s3681_maximum_xor_of_subsequences/SolutionTest.java @@ -0,0 +1,18 @@ +package g3601_3700.s3681_maximum_xor_of_subsequences; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxXorSubsequences() { + assertThat(new Solution().maxXorSubsequences(new int[] {1, 2, 3}), equalTo(3)); + } + + @Test + void maxXorSubsequences2() { + assertThat(new Solution().maxXorSubsequences(new int[] {5, 2}), equalTo(7)); + } +} diff --git a/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java b/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java new file mode 100644 index 000000000..3d8a5e389 --- /dev/null +++ b/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java @@ -0,0 +1,20 @@ +package g3601_3700.s3683_earliest_time_to_finish_one_task; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void earliestTime() { + assertThat(new Solution().earliestTime(new int[][] {{1, 6}, {2, 3}}), equalTo(5)); + } + + @Test + void earliestTime2() { + assertThat( + new Solution().earliestTime(new int[][] {{100, 100}, {100, 100}, {100, 100}}), + equalTo(200)); + } +} diff --git a/src/test/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/SolutionTest.java b/src/test/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/SolutionTest.java new file mode 100644 index 000000000..ce5c5b1f8 --- /dev/null +++ b/src/test/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/SolutionTest.java @@ -0,0 +1,29 @@ +package g3601_3700.s3684_maximize_sum_of_at_most_k_distinct_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxKDistinct() { + assertThat( + new Solution().maxKDistinct(new int[] {84, 93, 100, 77, 90}, 3), + equalTo(new int[] {100, 93, 90})); + } + + @Test + void maxKDistinct2() { + assertThat( + new Solution().maxKDistinct(new int[] {84, 93, 100, 77, 93}, 3), + equalTo(new int[] {100, 93, 84})); + } + + @Test + void maxKDistinct3() { + assertThat( + new Solution().maxKDistinct(new int[] {1, 1, 1, 2, 2, 2}, 6), + equalTo(new int[] {2, 1})); + } +} diff --git a/src/test/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/SolutionTest.java b/src/test/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/SolutionTest.java new file mode 100644 index 000000000..baf549897 --- /dev/null +++ b/src/test/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/SolutionTest.java @@ -0,0 +1,22 @@ +package g3601_3700.s3685_subsequence_sum_after_capping_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void subsequenceSumAfterCapping() { + assertThat( + new Solution().subsequenceSumAfterCapping(new int[] {4, 3, 2, 4}, 5), + equalTo(new boolean[] {false, false, true, true})); + } + + @Test + void subsequenceSumAfterCapping2() { + assertThat( + new Solution().subsequenceSumAfterCapping(new int[] {1, 2, 3, 4, 5}, 3), + equalTo(new boolean[] {true, true, true, true, true})); + } +} diff --git a/src/test/java/g3601_3700/s3686_number_of_stable_subsequences/SolutionTest.java b/src/test/java/g3601_3700/s3686_number_of_stable_subsequences/SolutionTest.java new file mode 100644 index 000000000..8c76e2197 --- /dev/null +++ b/src/test/java/g3601_3700/s3686_number_of_stable_subsequences/SolutionTest.java @@ -0,0 +1,18 @@ +package g3601_3700.s3686_number_of_stable_subsequences; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countStableSubsequences() { + assertThat(new Solution().countStableSubsequences(new int[] {1, 3, 5}), equalTo(6)); + } + + @Test + void countStableSubsequences2() { + assertThat(new Solution().countStableSubsequences(new int[] {2, 3, 4, 2}), equalTo(14)); + } +} From 5cb27b3e74d2f6de84940544f570d6c89869c57d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 18 Sep 2025 20:08:40 +0300 Subject: [PATCH 2/6] Fixed sonar --- .../Solution.java | 9 ++++++++- .../Solution.java | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java index ebeabe051..2d8f0b662 100644 --- a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java +++ b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java @@ -18,7 +18,14 @@ public int smallestAbsent(int[] nums) { int n = nums.length; Arrays.sort(nums); avg = sum / n; - double j = avg < 0 ? 1 : Math.ceil(avg) == avg ? avg + 1 : Math.ceil(avg); + double j; + if (avg < 0) { + j = 1; + } else if (Math.ceil(avg) == avg) { + j = avg + 1; + } else { + j = Math.ceil(avg); + } for (int i = (int) j; i <= 100; i++) { if (!set.contains(i)) { return i; diff --git a/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java index 65e5e1b50..37778f353 100644 --- a/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java +++ b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java @@ -4,6 +4,7 @@ import java.util.Arrays; +@SuppressWarnings("java:S135") public class Solution { private static final int MAX_K = 4001; private final boolean[] dp = new boolean[MAX_K]; From 940d179401c86b3667deaaa08db7a6417045754e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 18 Sep 2025 21:51:43 +0300 Subject: [PATCH 3/6] Added tests --- .../SolutionTest.java | 5 +++++ .../s3683_earliest_time_to_finish_one_task/SolutionTest.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java b/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java index 354dd129e..2ae782229 100644 --- a/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java +++ b/src/test/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/SolutionTest.java @@ -20,4 +20,9 @@ void smallestAbsent2() { void smallestAbsent3() { assertThat(new Solution().smallestAbsent(new int[] {4, -1}), equalTo(2)); } + + @Test + void smallestAbsent4() { + assertThat(new Solution().smallestAbsent(new int[] {-2, -1}), equalTo(1)); + } } diff --git a/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java b/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java index 3d8a5e389..beb1e4765 100644 --- a/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java +++ b/src/test/java/g3601_3700/s3683_earliest_time_to_finish_one_task/SolutionTest.java @@ -17,4 +17,9 @@ void earliestTime2() { new Solution().earliestTime(new int[][] {{100, 100}, {100, 100}, {100, 100}}), equalTo(200)); } + + @Test + void earliestTime3() { + assertThat(new Solution().earliestTime(new int[][] {{1, 6}}), equalTo(7)); + } } From f95aa3832326a8345f5a76476fcd9dbb9699375c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 20 Sep 2025 04:06:30 +0300 Subject: [PATCH 4/6] Improved solutions --- .../Solution.java | 44 ++++++----- .../Solution.java | 33 ++++----- .../s3680_generate_schedule/Solution.java | 67 +++-------------- .../Solution.java | 2 +- .../Solution.java | 28 ++----- .../Solution.java | 39 +++++----- .../Solution.java | 73 ++++++++++++------- .../Solution.java | 2 +- 8 files changed, 125 insertions(+), 163 deletions(-) diff --git a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java index 2d8f0b662..ee32a6b92 100644 --- a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java +++ b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java @@ -1,36 +1,34 @@ package g3601_3700.s3678_smallest_absent_positive_greater_than_average; -// #Easy #Biweekly_Contest_165 #2025_09_14_Time_10_ms_(100.00%)_Space_45.27_MB_(100.00%) - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; +// #Easy #Biweekly_Contest_165 #2025_09_20_Time_2_ms_(100.00%)_Space_45.28_MB_(53.71%) public class Solution { public int smallestAbsent(int[] nums) { - double avg; - double sum = 0; - Set set = new HashSet<>(); - for (int num : nums) { - sum += num; - set.add(num); + int sum = 0; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; } - int n = nums.length; - Arrays.sort(nums); - avg = sum / n; - double j; + double avg = (double) sum / nums.length; + int num; if (avg < 0) { - j = 1; - } else if (Math.ceil(avg) == avg) { - j = avg + 1; + num = 1; } else { - j = Math.ceil(avg); + num = (int) avg + 1; } - for (int i = (int) j; i <= 100; i++) { - if (!set.contains(i)) { - return i; + while (true) { + boolean flag = false; + for (int i = 0; i < nums.length; i++) { + if (num == nums[i]) { + flag = true; + break; + } + } + if (!flag) { + if (num > avg) { + return num; + } } + num++; } - return nums[n - 1] + 1; } } diff --git a/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java b/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java index 6d8d55dfb..0f1dc347e 100644 --- a/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java +++ b/src/main/java/g3601_3700/s3679_minimum_discards_to_balance_inventory/Solution.java @@ -1,30 +1,29 @@ package g3601_3700.s3679_minimum_discards_to_balance_inventory; -// #Medium #Biweekly_Contest_165 #2025_09_14_Time_30_ms_(100.00%)_Space_61.57_MB_(100.00%) - -import java.util.HashMap; -import java.util.Map; +// #Medium #Biweekly_Contest_165 #2025_09_20_Time_2_ms_(100.00%)_Space_60.72_MB_(75.43%) public class Solution { public int minArrivalsToDiscard(int[] arrivals, int w, int m) { int n = arrivals.length; - if (n == 0) { - return 0; - } - Map map = new HashMap<>(); - int[] kept = new int[n]; int dis = 0; + boolean[] removed = new boolean[n]; + int maxVal = 0; + for (int v : arrivals) { + maxVal = Math.max(maxVal, v); + } + int[] freq = new int[maxVal + 1]; for (int i = 0; i < n; i++) { - int idx = i - w; - if (idx >= 0 && kept[idx] == 1) { - map.put(arrivals[idx], map.get(arrivals[idx]) - 1); + int outIdx = i - w; + if (outIdx >= 0 && !removed[outIdx]) { + int oldVal = arrivals[outIdx]; + freq[oldVal]--; } - int t = arrivals[i]; - if (map.getOrDefault(t, 0) < m) { - kept[i] = 1; - map.put(t, map.getOrDefault(t, 0) + 1); - } else { + int val = arrivals[i]; + if (freq[val] >= m) { dis++; + removed[i] = true; + } else { + freq[val]++; } } return dis; diff --git a/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java b/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java index b58e6ff43..4392cec4d 100644 --- a/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java +++ b/src/main/java/g3601_3700/s3680_generate_schedule/Solution.java @@ -1,66 +1,23 @@ package g3601_3700.s3680_generate_schedule; -// #Medium #Biweekly_Contest_165 #2025_09_14_Time_67_ms_(100.00%)_Space_45.58_MB_(100.00%) - -import java.util.Arrays; -import java.util.Comparator; +// #Medium #Biweekly_Contest_165 #2025_09_20_Time_2_ms_(100.00%)_Space_45.33_MB_(59.29%) public class Solution { public int[][] generateSchedule(int n) { - int[][] res = new int[n * (n - 1)][2]; - boolean[][] matches = new boolean[n][n]; - res[0] = new int[] {0, 1}; - matches[0][1] = true; - int[] matchesCount = new int[n]; - matchesCount[0] = 1; - matchesCount[1] = 1; - if (backtrack(n, matches, res, 1, matchesCount)) { - return res; - } - return new int[][] {}; - } - - private boolean backtrack( - int n, boolean[][] matches, int[][] result, int cur, int[] matchesCount) { - if (cur == result.length) { - return true; + if (n < 5) { + return new int[0][]; } - Integer[] teams = new Integer[n]; - for (int i = 0; i < n; i++) { - teams[i] = i; + int[][] res = new int[n * (n - 1)][]; + int idx = 0; + for (int i = 2; i < n - 1; i++) { + for (int j = 0; j < n; j++) { + res[idx++] = new int[] {j, (j + i) % n}; + } } - Arrays.sort(teams, Comparator.comparingInt(a -> matchesCount[a])); for (int i = 0; i < n; i++) { - if (result[cur - 1][0] != teams[i] && result[cur - 1][1] != teams[i]) { - int team1 = -1; - int team2 = -1; - for (int j = 0; j < n; j++) { - if (i != j - && !matches[teams[i]][teams[j]] - && result[cur - 1][0] != teams[j] - && result[cur - 1][1] != teams[j]) { - team1 = teams[i]; - team2 = teams[j]; - break; - } - } - if (team1 != -1) { - result[cur] = new int[] {team1, team2}; - matches[team1][team2] = true; - matchesCount[team1]++; - matchesCount[team2]++; - boolean found = backtrack(n, matches, result, cur + 1, matchesCount); - if (found) { - return true; - } else { - matches[team1][team2] = false; - result[cur] = new int[] {0, 0}; - matchesCount[team1]--; - matchesCount[team2]--; - } - } - } + res[idx++] = new int[] {i, (i + 1) % n}; + res[idx++] = new int[] {(i + 4) % n, (i + 3) % n}; } - return false; + return res; } } diff --git a/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java b/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java index 56b085fb2..628667fd2 100644 --- a/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java +++ b/src/main/java/g3601_3700/s3681_maximum_xor_of_subsequences/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3681_maximum_xor_of_subsequences; -// #Hard #Biweekly_Contest_165 #2025_09_14_Time_29_ms_(100.00%)_Space_58.02_MB_(100.00%) +// #Hard #Biweekly_Contest_165 #2025_09_20_Time_27_ms_(99.73%)_Space_58.35_MB_(30.31%) public class Solution { public int maxXorSubsequences(int[] nums) { diff --git a/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java b/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java index 06282544a..908327c82 100644 --- a/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java +++ b/src/main/java/g3601_3700/s3683_earliest_time_to_finish_one_task/Solution.java @@ -1,29 +1,15 @@ package g3601_3700.s3683_earliest_time_to_finish_one_task; -// #Easy #Weekly_Contest_467 #2025_09_14_Time_1_ms_(100.00%)_Space_44.90_MB_(100.00%) +// #Easy #Weekly_Contest_467 #2025_09_20_Time_0_ms_(100.00%)_Space_45.29_MB_(39.62%) public class Solution { public int earliestTime(int[][] tasks) { - if (tasks.length == 1) { - int sum = 0; - for (int t : tasks[0]) { - sum += t; - } - return sum; + int ans = 1000; + for (int i = 0; i < tasks.length; i++) { + int st = tasks[i][0]; + int tm = tasks[i][1]; + ans = Math.min(ans, st + tm); } - int minTask = 0; - for (int t : tasks[0]) { - minTask += t; - } - for (int i = 1; i < tasks.length; i++) { - int sum = 0; - for (int t : tasks[i]) { - sum += t; - } - if (sum < minTask) { - minTask = sum; - } - } - return minTask; + return ans; } } diff --git a/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java index 6f86b35f2..e5fc3dfca 100644 --- a/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java +++ b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java @@ -1,30 +1,35 @@ package g3601_3700.s3684_maximize_sum_of_at_most_k_distinct_elements; -// #Easy #Weekly_Contest_467 #2025_09_14_Time_8_ms_(100.00%)_Space_46.01_MB_(_%) +// #Easy #Weekly_Contest_467 #2025_09_20_Time_2_ms_(100.00%)_Space_45.58_MB_(68.55%) -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; public class Solution { public int[] maxKDistinct(int[] nums, int k) { Arrays.sort(nums); - for (int i = 0; i < nums.length / 2; i++) { - int temp = nums[i]; - nums[i] = nums[nums.length - 1 - i]; - nums[nums.length - 1 - i] = temp; + int[] arr = new int[k]; + int j = 1; + arr[0] = nums[nums.length - 1]; + if (nums.length > 1) { + for (int i = nums.length - 2; j < k && i >= 0; i--) { + if (i < nums.length - 1 && nums[i] != nums[i + 1]) { + arr[j] = nums[i]; + j++; + } + } } - List res = new ArrayList<>(); - res.add(nums[0]); - k--; - int i = 1; - while (k > 0 && i < nums.length) { - if (nums[i] != nums[i - 1]) { - res.add(nums[i]); - k--; + int cnt = 0; + int n = 0; + while (n < arr.length) { + if (arr[n] != 0) { + cnt++; } - i++; + n++; + } + int[] finl = new int[cnt]; + for (int i = 0; i < cnt; i++) { + finl[i] = arr[i]; } - return res.stream().mapToInt(Integer::intValue).toArray(); + return finl; } } diff --git a/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java index 37778f353..869820f65 100644 --- a/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java +++ b/src/main/java/g3601_3700/s3685_subsequence_sum_after_capping_elements/Solution.java @@ -1,42 +1,59 @@ package g3601_3700.s3685_subsequence_sum_after_capping_elements; -// #Medium #Weekly_Contest_467 #2025_09_14_Time_107_ms_(100.00%)_Space_45.61_MB_(100.00%) +// #Medium #Weekly_Contest_467 #2025_09_20_Time_24_ms_(96.07%)_Space_45.39_MB_(97.38%) -import java.util.Arrays; - -@SuppressWarnings("java:S135") public class Solution { - private static final int MAX_K = 4001; - private final boolean[] dp = new boolean[MAX_K]; - public boolean[] subsequenceSumAfterCapping(int[] nums, int k) { - Arrays.sort(nums); - int n = nums.length; - Arrays.fill(dp, false); + int[] zolvarinte = nums; + int n = zolvarinte.length; + boolean[] answer = new boolean[n]; + int maxV = n; + int[] freq = new int[maxV + 2]; + for (int v : zolvarinte) { + if (v <= maxV) { + freq[v]++; + } + } + int[] cntGe = new int[maxV + 2]; + cntGe[maxV] = freq[maxV]; + for (int x = maxV - 1; x >= 1; x--) { + cntGe[x] = cntGe[x + 1] + freq[x]; + } + boolean[] dp = new boolean[k + 1]; dp[0] = true; - int p = 0; - boolean[] ans = new boolean[n]; - for (int i = 1; i <= n; i++) { - while (p < n && nums[p] < i) { - for (int j = k; j >= nums[p]; j--) { - dp[j] |= dp[j - nums[p]]; - } - p++; + for (int x = 1; x <= n; x++) { + int cnt = cntGe[x]; + boolean ok = false; + int maxM = cnt; + int limit = k / x; + if (maxM > limit) { + maxM = limit; } - int cnt = n - p; - for (int j = 0; j <= cnt; j++) { - int weight = i * j; - if (k < weight) { + for (int m = 0; m <= maxM; m++) { + int rem = k - m * x; + if (rem >= 0 && dp[rem]) { + ok = true; break; } - // We can form dp[k - weight], so we can form dp[k] - // by choosing j knapsacks (each has weight of i) - if (dp[k - weight]) { - ans[i - 1] = true; - break; + } + answer[x - 1] = ok; + int c = freq[x]; + if (c == 0) { + continue; + } + int power = 1; + while (c > 0) { + int take = Math.min(power, c); + int weight = take * x; + for (int s = k; s >= weight; s--) { + if (!dp[s] && dp[s - weight]) { + dp[s] = true; + } } + c -= take; + power <<= 1; } } - return ans; + return answer; } } diff --git a/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java b/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java index 2e250c284..d1c372902 100644 --- a/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java +++ b/src/main/java/g3601_3700/s3686_number_of_stable_subsequences/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3686_number_of_stable_subsequences; -// #Hard #Weekly_Contest_467 #2025_09_14_Time_10_ms_(100.00%)_Space_60.00_MB_(100.00%) +// #Hard #Weekly_Contest_467 #2025_09_20_Time_9_ms_(99.94%)_Space_59.73_MB_(89.70%) public class Solution { private static final long MOD = 1000000007L; From c54da60b827eea95a5147c69ebc63b0fa9c2f4ea Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 20 Sep 2025 04:12:49 +0300 Subject: [PATCH 5/6] Fixed test --- .../g3601_3700/s3680_generate_schedule/SolutionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java b/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java index 66836e86e..9d595847a 100644 --- a/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java +++ b/src/test/java/g3601_3700/s3680_generate_schedule/SolutionTest.java @@ -17,9 +17,9 @@ void generateSchedule2() { new Solution().generateSchedule(5), equalTo( new int[][] { - {0, 1}, {2, 3}, {4, 0}, {1, 2}, {3, 4}, {0, 2}, {1, 3}, {4, 2}, {0, 3}, - {1, 4}, {2, 0}, {3, 1}, {0, 4}, {2, 1}, {4, 3}, {1, 0}, {2, 4}, {3, 0}, - {4, 1}, {3, 2} + {0, 2}, {1, 3}, {2, 4}, {3, 0}, {4, 1}, {0, 3}, {1, 4}, {2, 0}, {3, 1}, + {4, 2}, {0, 1}, {4, 3}, {1, 2}, {0, 4}, {2, 3}, {1, 0}, {3, 4}, {2, 1}, + {4, 0}, {3, 2} })); } } From af6507519eee9bb645ac6bb1fbebf755ef6e5363 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 20 Sep 2025 04:25:05 +0300 Subject: [PATCH 6/6] Fixed sonar --- .../Solution.java | 14 ++++++-------- .../Solution.java | 4 +--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java index ee32a6b92..4be70b86f 100644 --- a/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java +++ b/src/main/java/g3601_3700/s3678_smallest_absent_positive_greater_than_average/Solution.java @@ -5,8 +5,8 @@ public class Solution { public int smallestAbsent(int[] nums) { int sum = 0; - for (int i = 0; i < nums.length; i++) { - sum += nums[i]; + for (int j : nums) { + sum += j; } double avg = (double) sum / nums.length; int num; @@ -17,16 +17,14 @@ public int smallestAbsent(int[] nums) { } while (true) { boolean flag = false; - for (int i = 0; i < nums.length; i++) { - if (num == nums[i]) { + for (int j : nums) { + if (num == j) { flag = true; break; } } - if (!flag) { - if (num > avg) { - return num; - } + if (!flag && num > avg) { + return num; } num++; } diff --git a/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java index e5fc3dfca..a12e778ea 100644 --- a/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java +++ b/src/main/java/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements/Solution.java @@ -27,9 +27,7 @@ public int[] maxKDistinct(int[] nums, int k) { n++; } int[] finl = new int[cnt]; - for (int i = 0; i < cnt; i++) { - finl[i] = arr[i]; - } + System.arraycopy(arr, 0, finl, 0, cnt); return finl; } }