From d4989a8200ffafc58d596a491c5c83417856cd14 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Wed, 25 Nov 2026 21:48:13 +0700 Subject: [PATCH 1/9] Added tasks 4022-4044 --- .../Solution.java | 45 +++++++++ .../readme.md | 50 ++++++++++ .../s4023_elevator_requests_ii/Solution.java | 71 ++++++++++++++ .../s4023_elevator_requests_ii/readme.md | 63 ++++++++++++ .../Solution.java | 23 +++++ .../s4024_nearest_available_drone/readme.md | 55 +++++++++++ .../Solution.java | 23 +++++ .../readme.md | 76 +++++++++++++++ .../Solution.java | 35 +++++++ .../readme.md | 61 ++++++++++++ .../s4027_elevator_requests_iii/Solution.java | 50 ++++++++++ .../s4027_elevator_requests_iii/readme.md | 63 ++++++++++++ .../Solution.java | 22 +++++ .../s4030_check_ascii_palindromic/readme.md | 39 ++++++++ .../Solution.java | 34 +++++++ .../readme.md | 52 ++++++++++ .../Solution.java | 81 ++++++++++++++++ .../readme.md | 62 ++++++++++++ .../Solution.java | 95 +++++++++++++++++++ .../readme.md | 54 +++++++++++ .../Solution.java | 19 ++++ .../readme.md | 51 ++++++++++ .../Solution.java | 54 +++++++++++ .../readme.md | 81 ++++++++++++++++ .../Solution.java | 31 ++++++ .../readme.md | 50 ++++++++++ .../Solution.java | 77 +++++++++++++++ .../readme.md | 81 ++++++++++++++++ .../Solution.java | 28 ++++++ .../readme.md | 41 ++++++++ .../Solution.java | 45 +++++++++ .../s4039_sum_of_decoded_numbers/readme.md | 67 +++++++++++++ .../Solution.java | 46 +++++++++ .../readme.md | 53 +++++++++++ .../Solution.java | 49 ++++++++++ .../readme.md | 54 +++++++++++ .../Solution.java | 17 ++++ .../readme.md | 50 ++++++++++ .../Solution.java | 27 ++++++ .../readme.md | 57 +++++++++++ .../SolutionTest.java | 44 +++++++++ .../SolutionTest.java | 29 ++++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 32 +++++++ .../SolutionTest.java | 27 ++++++ .../SolutionTest.java | 29 ++++++ .../SolutionTest.java | 26 +++++ .../SolutionTest.java | 29 ++++++ .../SolutionTest.java | 19 ++++ .../SolutionTest.java | 18 ++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 24 +++++ .../SolutionTest.java | 29 ++++++ .../SolutionTest.java | 29 ++++++ .../SolutionTest.java | 29 ++++++ .../SolutionTest.java | 29 ++++++ 60 files changed, 2569 insertions(+) create mode 100644 src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/Solution.java create mode 100644 src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/readme.md create mode 100644 src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java create mode 100644 src/main/java/g4001_4100/s4023_elevator_requests_ii/readme.md create mode 100644 src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java create mode 100644 src/main/java/g4001_4100/s4024_nearest_available_drone/readme.md create mode 100644 src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java create mode 100644 src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/readme.md create mode 100644 src/main/java/g4001_4100/s4026_maximum_gap_between_stations/Solution.java create mode 100644 src/main/java/g4001_4100/s4026_maximum_gap_between_stations/readme.md create mode 100644 src/main/java/g4001_4100/s4027_elevator_requests_iii/Solution.java create mode 100644 src/main/java/g4001_4100/s4027_elevator_requests_iii/readme.md create mode 100644 src/main/java/g4001_4100/s4030_check_ascii_palindromic/Solution.java create mode 100644 src/main/java/g4001_4100/s4030_check_ascii_palindromic/readme.md create mode 100644 src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/Solution.java create mode 100644 src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/readme.md create mode 100644 src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/Solution.java create mode 100644 src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/readme.md create mode 100644 src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java create mode 100644 src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/readme.md create mode 100644 src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java create mode 100644 src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/readme.md create mode 100644 src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java create mode 100644 src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/readme.md create mode 100644 src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/Solution.java create mode 100644 src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/readme.md create mode 100644 src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/Solution.java create mode 100644 src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/readme.md create mode 100644 src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/Solution.java create mode 100644 src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/readme.md create mode 100644 src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java create mode 100644 src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/readme.md create mode 100644 src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java create mode 100644 src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/readme.md create mode 100644 src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java create mode 100644 src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/readme.md create mode 100644 src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/Solution.java create mode 100644 src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/readme.md create mode 100644 src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java create mode 100644 src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/readme.md create mode 100644 src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java diff --git a/src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/Solution.java b/src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/Solution.java new file mode 100644 index 000000000..cca1a9b78 --- /dev/null +++ b/src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/Solution.java @@ -0,0 +1,45 @@ +package g4001_4100.s4022_k_th_digit_in_infinite_string; + +// #Medium #Math #Binary_Search #Staff #Biweekly_Contest_189 +// #2026_11_25_Time_2_ms_(88.89%)_Space_42.48_MB_(52.78%) + +public class Solution { + public int kthDigit(long k) { + if (k <= 9) { + return (int) k; + } + long remaining = k - 9; + long power10 = 10; + int d = 2; + while (true) { + long firstBlock = power10 / 10; + long lastBlock = power10 - 1; + long numberOfBlocks = lastBlock - firstBlock + 1; + long digitsPerBlock = 10L * d; + long groupDigits = numberOfBlocks * digitsPerBlock; + if (remaining > groupDigits) { + remaining -= groupDigits; + power10 *= 10; + d++; + continue; + } + long zeroBased = remaining - 1; + long blockOffset = zeroBased / digitsPerBlock; + long positionInBlock = zeroBased % digitsPerBlock; + long b = firstBlock + blockOffset; + long numberIndex = positionInBlock / d; + int digitIndex = (int) (positionInBlock % d); + long number; + if (b % 2 == 0) { + number = 10 * b + numberIndex; + } else { + number = 10 * b + 9 - numberIndex; + } + long divisor = 1; + for (int i = 0; i < d - 1 - digitIndex; i++) { + divisor *= 10; + } + return (int) ((number / divisor) % 10); + } + } +} diff --git a/src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/readme.md b/src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/readme.md new file mode 100644 index 000000000..84a1b8cfe --- /dev/null +++ b/src/main/java/g4001_4100/s4022_k_th_digit_in_infinite_string/readme.md @@ -0,0 +1,50 @@ +4022\. K-th Digit in Infinite String + +Medium + +You are given an integer `k`. + +An **infinite** string is formed by **concatenating** the **decimal** representations of the **positive** integers, without separators. + +For every nonnegative integer `b`, block `b` contains the **positive** integers from `10 * b` through `10 * b + 9`. The integers in each block are appended as follows: + +* If `b` is even, append the integers in **increasing** order. +* If `b` is odd, append the integers in **decreasing** order. + +Therefore, the string starts with the integers 1 through 9, followed by 19 through 10, then 20 through 29, then 39 through 30, and so on. + +Return the kth digit (1-indexed) of this string. + +**Example 1:** + +**Input:** k = 4 + +**Output:** 4 + +**Explanation:** + +The string begins as "123456789..". The 4th digit is `'4'`. + +**Example 2:** + +**Input:** k = 15 + +**Output:** 7 + +**Explanation:** + +The string begins as "123456789191817..". The 15th digit is `'7'`. + +**Example 3:** + +**Input:** k = 11 + +**Output:** 9 + +**Explanation:** + +The string begins as "12345678919..". The 11th digit is `'9'`. + +**Constraints:** + +* 1 <= k <= 1015 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java b/src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java new file mode 100644 index 000000000..40a37e0ca --- /dev/null +++ b/src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java @@ -0,0 +1,71 @@ +package g4001_4100.s4023_elevator_requests_ii; + +// #Hard #Array #Dynamic_Programming #Sorting #Senior_Staff #Biweekly_Contest_189 +// #2026_11_25_Time_26_ms_(100.00%)_Space_69.24_MB_(79.07%) + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Solution { + public long elevatorRequests(int n, int start, int[] requests) { + List l = new ArrayList<>(); + List r = new ArrayList<>(); + for (int x : requests) { + if (x < start) { + l.add(start - x); + } else if (x > start) { + r.add(x - start); + } + } + Collections.sort(l); + Collections.sort(r); + int a = l.size(); + int b = r.size(); + int t = a + b; + if (t == 0) { + return 0; + } + long[] x = new long[a + 1]; + long[] y = new long[b + 1]; + for (int i = 1; i <= a; i++) { + x[i] = l.get(i - 1); + } + for (int j = 1; j <= b; j++) { + y[j] = r.get(j - 1); + } + long inf = Long.MAX_VALUE / 4; + long[][] dl = new long[a + 1][b + 1]; + long[][] dr = new long[a + 1][b + 1]; + for (int i = 1; i <= a; i++) { + dl[i][0] = dl[i - 1][0] + (x[i] - x[i - 1]) * (t - i + 1); + } + for (int j = 1; j <= b; j++) { + dr[0][j] = dr[0][j - 1] + (y[j] - y[j - 1]) * (t - j + 1); + } + for (int i = 1; i <= a; i++) { + for (int j = 1; j <= b; j++) { + long w = t - i - j + 1; + long p = inf; + if (i >= 2) { + p = dl[i - 1][j] + (x[i] - x[i - 1]) * w; + } + long q = dr[i - 1][j] + (y[j] + x[i]) * w; + dl[i][j] = Math.min(p, q); + long s = inf; + if (j >= 2) { + s = dr[i][j - 1] + (y[j] - y[j - 1]) * w; + } + long z = dl[i][j - 1] + (x[i] + y[j]) * w; + dr[i][j] = Math.min(s, z); + } + } + if (a == 0) { + return dr[0][b]; + } + if (b == 0) { + return dl[a][0]; + } + return Math.min(dl[a][b], dr[a][b]); + } +} diff --git a/src/main/java/g4001_4100/s4023_elevator_requests_ii/readme.md b/src/main/java/g4001_4100/s4023_elevator_requests_ii/readme.md new file mode 100644 index 000000000..3e101c908 --- /dev/null +++ b/src/main/java/g4001_4100/s4023_elevator_requests_ii/readme.md @@ -0,0 +1,63 @@ +4023\. Elevator Requests II + +Hard + +You are given an integer `n` denoting the number of floors in a building, where the floors are numbered from 0 to `n - 1`. + +You are also given an integer `start`, representing the floor where the elevator begins, and an integer array `requests`, where `requests[i]` is a floor that the elevator is requested to reach. All floors in `requests` are **distinct**. + +At time 0, the elevator is on floor `start`, and all requests are made **simultaneously**. + +During each second before all requests are fulfilled, the elevator moves **exactly** one floor, either **up** or **down**. A request is fulfilled **instantly** when the elevator reaches its requested floor. If `start` appears in `requests`, that request is fulfilled at time 0. + +For each second that a request remains unfulfilled, you receive 1 penalty. Equivalently, a request fulfilled at time `t` contributes `t` to the total penalty. + +Return the **minimum** total penalty required to fulfill all requests. + +**Example 1:** + +**Input:** n = 6, start = 4, requests = [1,5] + +**Output:** 6 + +**Explanation:** + +* Move from floor 4 (`start`) to floor 5 in 1 second. Penalty for floor 5 is 1. +* Move from floor 5 to floor 1 in 4 seconds. Penalty for floor 1 is 5. + +Thus, the total penalty is `1 + 5 = 6`. + +**Example 2:** + +**Input:** n = 8, start = 3, requests = [3,7,1] + +**Output:** 10 + +**Explanation:** + +* Floor 3 (`start`) is fulfilled instantly. Penalty for floor 3 is 0. +* Move from floor 3 to floor 1 in 2 seconds. Penalty for floor 1 is 2. +* Move from floor 1 to floor 7 in 6 seconds. Penalty for floor 7 is 8. + +Thus, the total penalty is `0 + 2 + 8 = 10`. + +**Example 3:** + +**Input:** n = 10, start = 5, requests = [0,2,9] + +**Output:** 22 + +**Explanation:** + +* Move from floor 5 (`start`) to floor 2 in 3 seconds. Penalty for floor 2 is 3. +* Move from floor 2 to floor 0 in 2 seconds. Penalty for floor 0 is 5. +* Move from floor 0 to floor 9 in 9 seconds. Penalty for floor 9 is 14. + +Thus, the total penalty is `3 + 5 + 14 = 22`. + +**Constraints:** + +* 1 <= n <= 109 +* `1 <= requests.length <= 1500` +* `0 <= start, requests[i] <= n - 1` +* All values in `requests` are **distinct**. \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java b/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java new file mode 100644 index 000000000..90cd15b58 --- /dev/null +++ b/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java @@ -0,0 +1,23 @@ +package g4001_4100.s4024_nearest_available_drone; + +// #Easy #Array #Enumeration #Mid_Level #Weekly_Contest_515 +// #2026_11_25_Time_2_ms_(50.35%)_Space_45.70_MB_(93.36%) + +public class Solution { + public int nearestDrone(int[][] drones, int[] target) { + int ri = -1; + + for (int i = 0, l = drones.length, x = target[0], y = target[1], rv = Integer.MAX_VALUE, d; + i < l; + i++) { + d = Math.abs(x - drones[i][0]) + Math.abs(y - drones[i][1]); + + if (d < rv && d <= drones[i][2]) { + rv = d; + ri = i; + } + } + + return ri; + } +} diff --git a/src/main/java/g4001_4100/s4024_nearest_available_drone/readme.md b/src/main/java/g4001_4100/s4024_nearest_available_drone/readme.md new file mode 100644 index 000000000..36a9371b4 --- /dev/null +++ b/src/main/java/g4001_4100/s4024_nearest_available_drone/readme.md @@ -0,0 +1,55 @@ +4024\. Nearest Available Drone + +Easy + +You are given a 2D integer array `drones`, where drones[i] = [xi, yi, rangei] represents the x-coordinate, y-coordinate, and travel range of the ith drone. + +You are also given an integer array `target = [tx, ty]`, representing the coordinates of the target. + +A drone `drones[i]` can reach the target if the Manhattan distance between its coordinates and the target coordinates is **less than or equal** to its rangei. + +Return the **index** of the reachable drone with the **minimum Manhattan distance** to the target. If there is a tie, return the **smallest index**. If no drone can reach the target, return `-1`. + +**Example 1:** + +**Input:** drones = [[0,0,8],[2,2,9]], target = [3,4] + +**Output:** 1 + +**Explanation:** + +* The distance between `drones[0]` and `target` is `|0 - 3| + |0 - 4| = 7`, which is within its range of 8. +* The distance between `drones[1]` and `target` is `|2 - 3| + |2 - 4| = 3`, which is within its range of 9. +* Since `drones[1]` is the nearest drone, the answer is 1. + +**Example 2:** + +**Input:** drones = [[2,1,5],[4,4,5],[6,6,8]], target = [5,5] + +**Output:** 1 + +**Explanation:** + +* The distance between `drones[0]` and `target` is `|2 - 5| + |1 - 5| = 7`, which is greater than its range of 5. +* The distance between `drones[1]` and `target` is `|4 - 5| + |4 - 5| = 2`, which is within its range of 5. +* The distance between `drones[2]` and `target` is `|6 - 5| + |6 - 5| = 2`, which is within its range of 8. +* Both `drones[1]` and `drones[2]` are the nearest drones. Since we should return the smallest index, the answer is 1. + +**Example 3:** + +**Input:** drones = [[4,4,5]], target = [8,6] + +**Output:** \-1 + +**Explanation:** + +* The distance between `drones[0]` and `target` is `|4 - 8| + |4 - 6| = 6`, which is greater than its range of 5. +* No drone can reach the target, so the answer is -1. + +**Constraints:** + +* `1 <= drones.length <= 100` +* drones[i] = [xi, yi, rangei] +* `target = [tx, ty]` +* -25 <= xi, yi, tx, ty <= 25 +* 1 <= rangei <= 100 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java b/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java new file mode 100644 index 000000000..180055ff7 --- /dev/null +++ b/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java @@ -0,0 +1,23 @@ +package g4001_4100.s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights; + +// #Medium #Array #Greedy #Senior #Weekly_Contest_515 +// #2026_11_25_Time_3_ms_(100.00%)_Space_121.58_MB_(33.61%) + +public class Solution { + public int minPenalty(int period, int[] lights, int[] arrivalTime) { + int greenMax = Integer.MIN_VALUE; + for (int light : lights) { + greenMax = Math.max(greenMax, light); + } + int ans = 0; + for (int i : arrivalTime) { + int r = i % period; + int curr = period - r; + + if (r >= greenMax) { + ans = Math.max(ans, curr); + } + } + return ans; + } +} diff --git a/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/readme.md b/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/readme.md new file mode 100644 index 000000000..a5917bee3 --- /dev/null +++ b/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/readme.md @@ -0,0 +1,76 @@ +4025\. Minimize the Maximum Waiting Time at Synchronized Traffic Lights + +Medium + +You are given an integer `period` and an integer array `lights`, where `lights[i]` is the duration, in seconds, of the green phase of the ith traffic light. + +At time 0, every traffic light starts at the beginning of its green phase. Their cycles are synchronized: every traffic light starts a new cycle at the same time, and every cycle lasts **exactly** `period` seconds. Therefore, the red phase of the ith traffic light lasts for `period - lights[i]` seconds. + +You are also given an integer array `arrivalTime`, where `arrivalTime[j]` is the arrival time, in seconds, of the jth car. + +Each car must be assigned to **exactly** one traffic light. Multiple cars may be assigned to the same traffic light. Any number of cars may cross the same traffic light simultaneously while it is green. Cars do not block or delay one another. + +For a car `j` assigned to the ith traffic light, let `r = arrivalTime[j] % period`. If `r < lights[i]`, its waiting time is 0. Otherwise, its waiting time is `period - r`. + +The **penalty** of an assignment is the **maximum** waiting time among all cars. + +Return an integer denoting the **minimum possible penalty**. + +**Example 1:** + +**Input:** period = 8, lights = [2,3], arrivalTime = [2,5,8,11] + +**Output:** 5 + +**Explanation:** + +One optimal solution is: + +* Assign `arrivalTime[0]` to the traffic light with `lights[1] = 3`. Here, `r = 2 % 8 = 2`. Since `2 < 3`, the waiting time is 0. +* Assign `arrivalTime[1]` to the traffic light with `lights[0] = 2`. Here, `r = 5 % 8 = 5`. Since `5 >= 2`, the waiting time is `8 - 5 = 3`. +* Assign `arrivalTime[2]` to the traffic light with `lights[0] = 2`. Here, `r = 8 % 8 = 0`. Since `0 < 2`, the waiting time is 0. +* Assign `arrivalTime[3]` to the traffic light with `lights[0] = 2`. Here, `r = 11 % 8 = 3`. Since `3 >= 2`, the waiting time is `8 - 3 = 5`. + +The penalty of this assignment is 5, which is the minimum possible. Other optimal assignments may exist. + +**Example 2:** + +**Input:** period = 10, lights = [3,6,8], arrivalTime = [4,9,15] + +**Output:** 1 + +**Explanation:** + +One optimal solution is: + +* Assign `arrivalTime[0]` to the traffic light with `lights[2] = 8`. Here, `r = 4 % 10 = 4`. Since `4 < 8`, the waiting time is 0. +* Assign `arrivalTime[1]` to the traffic light with `lights[2] = 8`. Here, `r = 9 % 10 = 9`. Since `9 >= 8`, the waiting time is `10 - 9 = 1`. +* Assign `arrivalTime[2]` to the traffic light with `lights[2] = 8`. Here, `r = 15 % 10 = 5`. Since `5 < 8`, the waiting time is 0. + +The penalty of this assignment is 1, which is the minimum possible. + +**Example 3:** + +**Input:** period = 5, lights = [2], arrivalTime = [2,3,4,5,6] + +**Output:** 3 + +**Explanation:** + +One optimal solution is: + +* Assign `arrivalTime[0]` to the traffic light with `lights[0] = 2`. Here, `r = 2 % 5 = 2`. Since `2 >= 2`, the waiting time is `5 - 2 = 3`. +* Assign `arrivalTime[1]` to the traffic light with `lights[0] = 2`. Here, `r = 3 % 5 = 3`. Since `3 >= 2`, the waiting time is `5 - 3 = 2`. +* Assign `arrivalTime[2]` to the traffic light with `lights[0] = 2`. Here, `r = 4 % 5 = 4`. Since `4 >= 2`, the waiting time is `5 - 4 = 1`. +* Assign `arrivalTime[3]` to the traffic light with `lights[0] = 2`. Here, `r = 5 % 5 = 0`. Since `0 < 2`, the waiting time is 0. +* Assign `arrivalTime[4]` to the traffic light with `lights[0] = 2`. Here, `r = 6 % 5 = 1`. Since `1 < 2`, the waiting time is 0. + +The penalty of this assignment is 3, which is the minimum possible. + +**Constraints:** + +* 2 <= period <= 109 +* 1 <= lights.length <= 104 +* `1 <= lights[i] <= period - 1` +* 1 <= arrivalTime.length <= 105 +* 1 <= arrivalTime[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4026_maximum_gap_between_stations/Solution.java b/src/main/java/g4001_4100/s4026_maximum_gap_between_stations/Solution.java new file mode 100644 index 000000000..76edd651a --- /dev/null +++ b/src/main/java/g4001_4100/s4026_maximum_gap_between_stations/Solution.java @@ -0,0 +1,35 @@ +package g4001_4100.s4026_maximum_gap_between_stations; + +// #Medium #String #Greedy #Two_Pointers #Staff #Weekly_Contest_515 +// #2026_11_25_Time_28_ms_(100.00%)_Space_47.45_MB_(96.06%) + +public class Solution { + public int maximumGap(String skill, String station) { + if (skill.length() == 1) { + return 0; + } + return maximumGap(skill.toCharArray(), station.toCharArray()); + } + + private static int maximumGap(char[] skill, char[] station) { + int n = skill.length; + int[] last = new int[n]; + int pos = station.length - 1; + for (int i = n - 1; i >= 0; --i) { + while (station[pos] != skill[i]) { + --pos; + } + last[i] = (pos--); + } + int result = 0; + pos = 0; + for (int i = 0, limit = n - 1; i < limit; ++i) { + while (station[pos] != skill[i]) { + ++pos; + } + result = Math.max(result, last[i + 1] - pos); + ++pos; + } + return result; + } +} diff --git a/src/main/java/g4001_4100/s4026_maximum_gap_between_stations/readme.md b/src/main/java/g4001_4100/s4026_maximum_gap_between_stations/readme.md new file mode 100644 index 000000000..38ff06310 --- /dev/null +++ b/src/main/java/g4001_4100/s4026_maximum_gap_between_stations/readme.md @@ -0,0 +1,61 @@ +4026\. Maximum Gap Between Stations + +Medium + +You are given two strings `skill` and `station` of lengths `n` and `m`, respectively. + +`skill[i]` represents the skill of worker `i`, and `station[j]` represents the skill supported by station `j`. + +You must assign **every** worker to a **distinct** station. Let ji be the index of the station assigned to worker `i`. A valid assignment must satisfy: + +* station[ji] == skill[i] for every `0 <= i < n`. +* The assigned station indices must be **strictly** increasing in worker order, meaning j0 < j1 < ... < jn - 1. + +The **gap** of an assignment is the **maximum difference** between the station indices assigned to two **consecutive** workers. In other words, it is max(ji - ji - 1) over all `1 <= i < n`. + +If there is only one worker, the gap is 0. + +Return the **maximum** possible gap among all valid assignments. It is guaranteed that **at least** one valid assignment exists. + +**Example 1:** + +**Input:** skill = "aa", station = "aaaa" + +**Output:** 3 + +**Explanation:** + +* The two workers must be assigned to two different `'a'` stations. +* Assigning them to stations `[0, 3]` gives a gap of 3. + +**Example 2:** + +**Input:** skill = "xyz", station = "xyzz" + +**Output:** 2 + +**Explanation:** + +* Assign worker 0 to station `j = 0`, and worker 1 to station `j = 1`. +* To maximize the gap, assign worker 2 to station `j = 3`. +* This gives the assignment `[0, 1, 3]` with gaps `[1, 2]`, so the gap is 2. + +**Example 3:** + +**Input:** skill = "cbc", station = "cbcdbc" + +**Output:** 4 + +**Explanation:** + +* Assign worker 0 to station `j = 0`, and worker 1 to station `j = 1`. +* To maximize the gap, assign worker 2 to station `j = 5`. +* This gives the assignment `[0, 1, 5]` with gaps `[1, 4]`, so the gap is 4. + +**Constraints:** + +* `skill.length == n` +* `station.length == m` +* 1 <= n <= m <= 105 +* `skill` and `station` consist of lowercase English letters. +* It is guaranteed that a valid assignment exists for every worker. \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4027_elevator_requests_iii/Solution.java b/src/main/java/g4001_4100/s4027_elevator_requests_iii/Solution.java new file mode 100644 index 000000000..edc727303 --- /dev/null +++ b/src/main/java/g4001_4100/s4027_elevator_requests_iii/Solution.java @@ -0,0 +1,50 @@ +package g4001_4100.s4027_elevator_requests_iii; + +// #Hard #Array #Dynamic_Programming #Sorting #Bit_Manipulation #Bitmask #Senior_Staff +// #Weekly_Contest_515 #2026_11_25_Time_3_ms_(98.11%)_Space_44.46_MB_(92.45%) + +public class Solution { + public long elevatorRequests(int n, int start, int[][] requests) { + int m = requests.length; + return solve(start, 0L, 0, requests); + } + + private long solve(int currFloor, long currTime, int fulfilledMask, int[][] requests) { + int m = requests.length; + if (fulfilledMask == (1 << m) - 1) { + return currTime; + } + int minFloor = Integer.MAX_VALUE; + int maxFloor = Integer.MIN_VALUE; + for (int i = 0; i < m; i++) { + if ((fulfilledMask & (1 << i)) == 0) { + minFloor = Math.min(minFloor, requests[i][1]); + maxFloor = Math.max(maxFloor, requests[i][1]); + } + } + long res = Long.MAX_VALUE; + int[] targets = + (minFloor == maxFloor) ? new int[] {minFloor} : new int[] {minFloor, maxFloor}; + for (int target : targets) { + int nextMask = fulfilledMask; + long waitTargetTime = currTime + Math.abs(target - currFloor); + for (int i = 0; i < m; i++) { + if ((fulfilledMask & (1 << i)) == 0) { + int f = requests[i][1]; + long arr = requests[i][0]; + if ((currFloor <= f && f <= target) || (target <= f && f <= currFloor)) { + long passTime = currTime + Math.abs(f - currFloor); + if (passTime >= arr) { + nextMask |= (1 << i); + } else if (f == target) { + waitTargetTime = Math.max(waitTargetTime, arr); + nextMask |= (1 << i); + } + } + } + } + res = Math.min(res, solve(target, waitTargetTime, nextMask, requests)); + } + return res; + } +} diff --git a/src/main/java/g4001_4100/s4027_elevator_requests_iii/readme.md b/src/main/java/g4001_4100/s4027_elevator_requests_iii/readme.md new file mode 100644 index 000000000..e99eadf53 --- /dev/null +++ b/src/main/java/g4001_4100/s4027_elevator_requests_iii/readme.md @@ -0,0 +1,63 @@ +4027\. Elevator Requests III + +Hard + +You are given an integer `n` denoting the number of floors in a building, where the floors are numbered from 0 to `n - 1`. + +You are also given an integer `start` and a 2D integer array `requests`, where requests[i] = [arrivali, floori] indicates that a request for floori is made at time arrivali. + +At time 0, the elevator is at floor `start`. + +At each second, the elevator may move **up** by 1 floor, move **down** by 1 floor, or **remain** on its current floor. + +A request can be fulfilled **only** at or after its arrival time; it is fulfilled **instantly** when the elevator is on its requested floor at any time from its arrival time onward. + +Return the **minimum** time needed to fulfill all requests. + +**Example 1:** + +**Input:** n = 9, start = 0, requests = [[0,8],[6,5]] + +**Output:** 9 + +**Explanation:** + +* Move from floor 0 (`start`) to floor 5 (`requests[1][1]`) in 5 seconds, reaching at time 5. Since `requests[1][0] = 6`, wait until time 6 to fulfill it. +* Move from floor 5 to floor 8 (`requests[0][1]`) in 3 seconds, fulfilling it at time 9. + +Thus, all requests are fulfilled by time 9. + +**Example 2:** + +**Input:** n = 8, start = 5, requests = [[1,7],[7,3]] + +**Output:** 7 + +**Explanation:** + +* Move from floor 5 (`start`) to floor 7 (`requests[0][1]`) in 2 seconds, reaching at time 2. Since `requests[0][0] = 1` has already passed, floor 7 is fulfilled at time 2. +* Move from floor 7 to floor 3 (`requests[1][1]`) in 4 seconds, reaching at time 6. Since `requests[1][0] = 7`, wait until time 7. + +Thus, all requests are fulfilled by time 7. + +**Example 3:** + +**Input:** n = 7, start = 3, requests = [[0,5],[0,1],[6,3]] + +**Output:** 8 + +**Explanation:** + +* Move from floor 3 (`start`) to floor 5 (`requests[0][1]`) in 2 seconds, fulfilling it at time 2. +* Move from floor 5 to floor 1 (`requests[1][1]`) in 4 seconds, fulfilling it at time 6. +* Move from floor 1 to floor 3 (`requests[2][1]`) in 2 seconds, reaching at time 8. Its request arrived at `requests[2][0] = 6`, so floor 3 is fulfilled at time 8. + +Thus, all requests are fulfilled by time 8. + +**Constraints:** + +* 1 <= n <= 109 +* `1 <= requests.length <= 16` +* requests[i] == [arrivali, floori] +* 0 <= arrivali <= 109 +* 0 <= start, floori <= n - 1 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4030_check_ascii_palindromic/Solution.java b/src/main/java/g4001_4100/s4030_check_ascii_palindromic/Solution.java new file mode 100644 index 000000000..53ef25592 --- /dev/null +++ b/src/main/java/g4001_4100/s4030_check_ascii_palindromic/Solution.java @@ -0,0 +1,22 @@ +package g4001_4100.s4030_check_ascii_palindromic; + +// #Easy #String #Two_Pointers #Bit_Manipulation #Mid_Level #Weekly_Contest_516 +// #2026_11_25_Time_1_ms_(99.80%)_Space_42.93_MB_(99.02%) + +public class Solution { + public boolean isPalindromic(String s) { + int n = s.length(); + for (int i = 0; i <= n / 2; i++) { + int lptr = s.charAt(i); + int rptr = s.charAt(n - i - 1); + for (int j = 0; j < 8; j++) { + int left = (lptr >> j) & 1; + int right = (rptr >> (7 - j)) & 1; + if (left != right) { + return false; + } + } + } + return true; + } +} diff --git a/src/main/java/g4001_4100/s4030_check_ascii_palindromic/readme.md b/src/main/java/g4001_4100/s4030_check_ascii_palindromic/readme.md new file mode 100644 index 000000000..4e32e6d48 --- /dev/null +++ b/src/main/java/g4001_4100/s4030_check_ascii_palindromic/readme.md @@ -0,0 +1,39 @@ +4030\. Check ASCII Palindromic + +Easy + +You are given a string `s` consisting of lowercase English letters. + +Construct a **binary string** by replacing each character in `s` with the 8-bit binary representation of its ASCII value, **including leading zeros**, while preserving the original order of the characters. + +Return `true` if the resulting binary string is a **palindrome**. Otherwise, return `false`. + +**Example 1:** + +**Input:** s = "ff" + +**Output:** true + +**Explanation:** + +* The ASCII value of `f` is 102, whose 8-bit binary representation is `01100110`. +* Thus, the binary string is `0110011001100110`. +* Since this binary string is a **palindrome**, the output is `true`. + +**Example 2:** + +**Input:** s = "leet" + +**Output:** false + +**Explanation:** + +* The ASCII values of `l`, `e`, `e`, and `t` are 108, 101, 101, and 116, respectively. +* Their 8-bit binary representations are `01101100`, `01100101`, `01100101`, and `01110100`. +* Thus, the binary string is `01101100011001010110010101110100`. +* Since this binary string is not a **palindrome**, the output is `false`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/Solution.java b/src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/Solution.java new file mode 100644 index 000000000..52f5f09d4 --- /dev/null +++ b/src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/Solution.java @@ -0,0 +1,34 @@ +package g4001_4100.s4031_find_all_numbers_disappeared_in_an_array_ii; + +// #Medium #Array #Sorting #Binary_Search #Senior #Weekly_Contest_516 +// #2026_11_25_Time_14_ms_(98.35%)_Space_166.69_MB_(53.84%) + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Solution { + public List> findDisappearedNumbers(int[] nums, int lower, int upper) { + List> res = new ArrayList<>(); + boolean[] avail = new boolean[upper + 1]; + for (int num : nums) { + if (num >= lower && num <= upper) { + avail[num] = true; + } + } + int i = lower; + while (i <= upper) { + if (avail[i]) { + i++; + continue; + } + int start = i; + while (i <= upper && !avail[i]) { + i++; + } + int end = i - 1; + res.add(Arrays.asList(start, end)); + } + return res; + } +} diff --git a/src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/readme.md b/src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/readme.md new file mode 100644 index 000000000..611e3fbad --- /dev/null +++ b/src/main/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/readme.md @@ -0,0 +1,52 @@ +4031\. Find All Numbers Disappeared in an Array II + +Medium + +You are given an integer array `nums` and two integers `lower` and `upper`. + +A **missing integer** is an integer in the inclusive range `[lower, upper]` that does not appear in `nums`. + +Return a 2D integer array where each element is of the form `[start, end]`, representing a **contiguous** range of missing integers. Return the ranges in **increasing** order. If there are no missing integers, return an empty array. + +**Note:** Consecutive missing integers should be grouped into a single range. + +**Example 1:** + +**Input:** nums = [3,9,7], lower = 1, upper = 12 + +**Output:** [[1,2],[4,6],[8,8],[10,12]] + +**Explanation:** + +* The missing integers are `[1, 2, 4, 5, 6, 8, 10, 11, 12]`. +* Grouping the missing integers into the minimum number of contiguous ranges, we get `[1, 2]`, `[4, 6]`, `[8, 8]`, and `[10, 12]`. +* Therefore, the answer is `[[1, 2], [4, 6], [8, 8], [10, 12]]`. + +**Example 2:** + +**Input:** nums = [1,1], lower = 5, upper = 7 + +**Output:** [[5,7]] + +**Explanation:** + +* The missing integers are `[5, 6, 7]`. +* Grouping the missing integers into the minimum number of contiguous ranges, we get `[5, 7]`. +* Therefore, the answer is `[[5, 7]]`. + +**Example 3:** + +**Input:** nums = [2,3,5], lower = 2, upper = 3 + +**Output:** [] + +**Explanation:** + +* There are no missing integers. +* Therefore, the answer is `[]`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* 1 <= lower <= upper <= 105 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/Solution.java b/src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/Solution.java new file mode 100644 index 000000000..9c093e60f --- /dev/null +++ b/src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/Solution.java @@ -0,0 +1,81 @@ +package g4001_4100.s4032_longest_subarray_with_at_most_k_distinct_prime_factors; + +// #Medium #Array #Hash_Table #Math #Sliding_Window #Number_Theory #Staff #Weekly_Contest_516 +// #2026_11_25_Time_40_ms_(100.00%)_Space_84.33_MB_(76.79%) + +import java.util.Arrays; + +public class Solution { + public int longestSubarray(int[] nums, int k) { + int n = nums.length; + int maxVal = 0; + for (int x : nums) { + if (x > maxVal) { + maxVal = x; + } + } + // 1. Sieve of Eratosthenes to compute Smallest Prime Factor (SPF) + int[] spf = new int[maxVal + 1]; + for (int i = 2; i <= maxVal; i++) { + spf[i] = i; + } + for (int i = 2; i * i <= maxVal; i++) { + if (spf[i] == i) { + for (int j = i * i; j <= maxVal; j += i) { + if (spf[j] == j) { + spf[j] = i; + } + } + } + } + // 2. Precompute distinct prime factors for each unique element present in nums + int[][] primeFactors = new int[maxVal + 1][]; + for (int x : nums) { + if (primeFactors[x] == null) { + primeFactors[x] = getDistinctPrimeFactors(x, spf); + } + } + // 3. Sliding Window (Two Pointers) + int[] primeCount = new int[maxVal + 1]; + int distinctPrimes = 0; + int maxLen = 0; + int left = 0; + for (int right = 0; right < n; right++) { + // Add all prime factors of nums[right] to the window + for (int prime : primeFactors[nums[right]]) { + if (primeCount[prime] == 0) { + distinctPrimes++; + } + primeCount[prime]++; + } + // Shrink the window from the left if distinct prime factor count exceeds k + while (distinctPrimes > k) { + for (int prime : primeFactors[nums[left]]) { + primeCount[prime]--; + if (primeCount[prime] == 0) { + distinctPrimes--; + } + } + left++; + } + // If the valid window has length >= 1, update maxLen + if (left <= right) { + maxLen = Math.max(maxLen, right - left + 1); + } + } + return maxLen; + } + + private int[] getDistinctPrimeFactors(int n, int[] spf) { + int[] temp = new int[7]; + int size = 0; + while (n > 1) { + int p = spf[n]; + temp[size++] = p; + while (n % p == 0) { + n /= p; + } + } + return Arrays.copyOf(temp, size); + } +} diff --git a/src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/readme.md b/src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/readme.md new file mode 100644 index 000000000..a258c6e98 --- /dev/null +++ b/src/main/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/readme.md @@ -0,0 +1,62 @@ +4032\. Longest Subarray With at Most K Distinct Prime Factors + +Medium + +You are given an integer array `nums` consisting of positive integers and an integer `k`. + +The **prime factor set** of a non-empty subarrays is the **union** of the distinct **prime** factors of all its elements. + +Return the length of the **longest** subarray whose prime factor set contains **at most** `k` distinct prime factors. If no such subarray exists, return 0. + +**Example 1:** + +**Input:** nums = [7,6,10,12,11], k = 3 + +**Output:** 3 + +**Explanation:** + +Consider the subarray `[6, 10, 12]`: + +* The distinct prime factors of 6 are `{2, 3}`. +* The distinct prime factors of 10 are `{2, 5}`. +* The distinct prime factors of 12 are `{2, 3}`. +* The union of these sets is `{2, 3, 5}`, which contains 3 distinct prime factors. + +No longer subarray satisfies the condition. Therefore, the answer is 3. + +**Example 2:** + +**Input:** nums = [4,6,9,18], k = 4 + +**Output:** 4 + +**Explanation:** + +Consider the entire array `[4, 6, 9, 18]`: + +* The distinct prime factors of 4 are `{2}`. +* The distinct prime factors of 6 are `{2, 3}`. +* The distinct prime factors of 9 are `{3}`. +* The distinct prime factors of 18 are `{2, 3}`. +* The union of these sets is `{2, 3}`, which contains 2 distinct prime factors. + +Since `2 <= 4`, the entire array is valid. Therefore, the answer is 4. + +**Example 3:** + +**Input:** nums = [6,10,15], k = 2 + +**Output:** 1 + +**Explanation:** + +Every subarray of length at least 2 has prime factor set `{2, 3, 5}`, which contains 3 distinct prime factors. + +Since `3 > 2`, only subarrays of length 1 are valid. Therefore, the answer is 1. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 2 <= nums[i] <= 105 +* 1 <= k <= 104 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java b/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java new file mode 100644 index 000000000..c597fc9b8 --- /dev/null +++ b/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java @@ -0,0 +1,95 @@ +package g4001_4100.s4033_valid_k_unique_subarrays_i; + +// #Hard #Array #Hash_Table #Bit_Manipulation #Prefix_Sum #Sliding_Window #Segment_Tree +// #Hash_Function #Binary_Indexed_Tree #Senior_Staff #Weekly_Contest_516 +// #2026_11_25_Time_20_ms_(100.00%)_Space_235.47_MB_(70.27%) + +import java.util.Arrays; + +public class Solution { + private int countOdd; + private int countDistinct; + + public boolean[] validSubarrays(int[] nums, int k, int[][] queries) { + int maxNum = Integer.MIN_VALUE; + int[] prefixXor = new int[nums.length + 1]; + for (int i = 0; i < nums.length; i++) { + maxNum = Math.max(maxNum, nums[i]); + prefixXor[i + 1] = prefixXor[i] ^ nums[i]; + } + + int[][] sortedQueries = new int[queries.length][]; + int queriesCount = this.sortQueries(nums, k, prefixXor, queries, sortedQueries); + boolean[] result = new boolean[queries.length]; + if (queriesCount == 0) { + return result; + } + int currentLeft = sortedQueries[0][0]; + int currentRight = sortedQueries[0][0] - 1; + countOdd = 0; + countDistinct = 0; + int[] counts = new int[maxNum + 1]; + for (int i = 0; i < queriesCount; i++) { + int[] query = sortedQueries[i]; + while (currentLeft > query[0]) { + this.add(counts, nums, --currentLeft); + } + while (currentRight < query[1]) { + this.add(counts, nums, ++currentRight); + } + while (currentLeft < query[0]) { + this.remove(counts, nums, currentLeft++); + } + while (currentRight > query[1]) { + this.remove(counts, nums, currentRight--); + } + result[query[2]] = countDistinct == k && countOdd == 0; + } + + return result; + } + + private int sortQueries( + int[] nums, int k, int[] prefixXor, int[][] queries, int[][] sortedQueries) { + int blockSize = Math.max(1, (int) (nums.length / Math.sqrt(queries.length))); + int queriesCount = 0; + for (int i = 0; i < queries.length; i++) { + int[] query = queries[i]; + int length = query[1] - query[0]; + if (length % 2 == 1 + && length + 1 >= 2 * k + && (prefixXor[query[1] + 1] ^ prefixXor[query[0]]) == 0) { + sortedQueries[queriesCount++] = + new int[] {query[0], query[1], i, query[0] / blockSize}; + } + } + Arrays.sort( + sortedQueries, + 0, + queriesCount, + (o1, o2) -> { + if (o1[3] == o2[3]) { + return o1[3] % 2 == 0 ? o1[1] - o2[1] : o2[1] - o1[1]; + } else { + return o1[3] - o2[3]; + } + }); + return queriesCount; + } + + private void add(int[] counts, int[] nums, int index) { + int count = ++counts[nums[index]]; + if (count == 1) { + countDistinct++; + } + countOdd += 2 * (count & 1) - 1; + } + + private void remove(int[] counts, int[] nums, int index) { + int count = --counts[nums[index]]; + if (count == 0) { + countDistinct--; + } + countOdd += 2 * (count & 1) - 1; + } +} diff --git a/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/readme.md b/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/readme.md new file mode 100644 index 000000000..809521632 --- /dev/null +++ b/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/readme.md @@ -0,0 +1,54 @@ +4033\. Valid K-Unique Subarrays I + +Hard + +You are given an integer array `nums` and an integer `k`. + +You are also given a 2D integer array `queries`, where queries[i] = [li, ri] represents the non-empty subarrays nums[li..ri]. + +For each query, the **subarray** nums[li..ri] is considered **valid** if: + +* It contains **exactly** `k` **distinct** numbers, and +* The **frequency** of every number in the **subarray** is **even**. + +Return a boolean array `ans`, where `ans[i]` is `true` if nums[li..ri] is **valid**, and `false` otherwise. + +**Example 1:** + +**Input:** nums = [1,2,2,1], k = 2, queries = [[0,1],[0,3],[1,2]] + +**Output:** [false,true,false] + +**Explanation:** + +| `i` | [li, ri] | Subarray | Unique numbers | Frequency | Validity check | +| --- | --- | --- | --- | --- | --- | +| 0 | [0, 1] | [1, 2] | {1, 2} → 2 | {1: 1, 2: 1} | `false`: Element counts are not even. | +| 1 | [0, 3] | [1, 2, 2, 1] | {1, 2} → 2 | {1: 2, 2: 2} | `true`: Exactly `k = 2` distinct elements, all appear an even number of times. | +| 2 | [1, 2] | [2, 2] | {2} → 1 | {2: 2} | `false`: Number of distinct elements is less than `k = 2`. | + +Thus, `ans = [false, true, false]`. + +**Example 2:** + +**Input:** nums = [3,3,3], k = 1, queries = [[1,2],[0,2]] + +**Output:** [true,false] + +**Explanation:** + +| `i` | [li, ri] | Subarray | Unique numbers | Frequency | Validity check | +| --- | --- | --- | --- | --- | --- | +| 0 | [1, 2] | [3, 3] | {3} → 1 | {3: 2} | `true`: Exactly `k = 1` distinct element, appears an even number of times. | +| 1 | [0, 2] | [3, 3, 3] | {3} → 1 | {3: 3} | `false`: 3 does not appear an even number of times. | + +Thus, `ans = [true, false]`. + +**Constraints:** + +* 2 <= n == nums.length <= 105 +* 1 <= nums[i] <= 105 +* `1 <= k <= n` +* 1 <= queries.length <= 105 +* queries[i] == [li, ri] +* 0 <= li < ri <= n - 1 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java b/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java new file mode 100644 index 000000000..b1e2f8692 --- /dev/null +++ b/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java @@ -0,0 +1,19 @@ +package g4001_4100.s4034_minimum_bishop_moves_to_reach_target; + +// #Medium #Array #Math #Senior #Biweekly_Contest_190 +// #2026_11_25_Time_1_ms_(96.38%)_Space_43.66_MB_(75.36%) + +public class Solution { + public int minBishopMoves(int[] source, int[] target) { + int r1 = source[0], c1 = source[1]; + int r2 = target[0], c2 = target[1]; + if (r1 == r2 && c1 == c2) return 0; + if (Math.abs(r1 - r2) == Math.abs(c1 - c2)) { + return 1; + } + if ((r1 + c1) % 2 != (r2 + c2) % 2) { + return -1; + } + return 2; + } +} diff --git a/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/readme.md b/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/readme.md new file mode 100644 index 000000000..f7154bdd4 --- /dev/null +++ b/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/readme.md @@ -0,0 +1,51 @@ +4034\. Minimum Bishop Moves to Reach Target + +Medium + +There is an `8 x 8` empty chessboard with **1-indexed** rows and columns. + +You are given an array `source = [sr, sc]` representing the starting position of a **bishop**, and an array `target = [tr, tc]` representing the target position. + +In one move, the bishop travels one or more squares along a single **diagonal** direction, staying within the board. + +Return the **minimum** number of moves for the bishop to land **exactly** on `target`. If it can never reach `target`, return -1. + +**Example 1:** + +**Input:** source = [8,1], target = [1,8] + +**Output:** 1 + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2026/08/31/111.png)** + +A single diagonal move takes the bishop straight from `(8, 1)` to `(1, 8)`. + +**Example 2:** + +**Input:** source = [4,2], target = [1,3] + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2026/08/31/22-ezgifcom-invert-colors.png) + +The bishop moves from `(4, 2)` to `(3, 1)`, then from `(3, 1)` to `(1, 3)`, reaching the target in 2 moves. + +**Example 3:** + +**Input:** source = [1,1], target = [3,4] + +**Output:** \-1 + +**Explanation:** + +No matter how many diagonal moves it makes, the bishop starting at `(1, 1)` can never land on `(3, 4)`. Thus, the answer is -1. + +**Constraints:** + +* `source.length == target.length == 2` +* `1 <= sr, sc, tr, tc <= 8` +* `source != target` \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java b/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java new file mode 100644 index 000000000..fb515c89e --- /dev/null +++ b/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java @@ -0,0 +1,54 @@ +package g4001_4100.s4035_maximum_valid_split_positions_i; + +// #Medium #Array #Math #Enumeration #Number_Theory #Senior #Biweekly_Contest_190 +// #2026_11_25_Time_2_ms_(98.99%)_Space_45.72_MB_(100.00%) + +public class Solution { + public int maxValidSplits(int[] nums) { + int n = nums.length; + int[] pref = new int[n + 1]; + int[] suf = new int[n + 1]; + for (int k = 0; k < n; k++) { + pref[k + 1] = gcd(pref[k], nums[k]); + } + for (int k = n - 1; k >= 0; k--) { + suf[k] = gcd(suf[k + 1], nums[k]); + } + int total = pref[n], best = 0; + for (int k = 0; k <= n; k++) { + if (pref[k] == suf[k]) { + best++; + } + } + for (int i = 0; i < n; i++) { + int g = gcd(pref[i], suf[i + 1]); + if (g == total) { + continue; + } + int l = pref[i] == g ? walk(nums, 0, 0, 1, g) : walk(nums, i + 1, pref[i], 1, g); + int r = + suf[i + 1] == g + ? walk(nums, n - 1, 0, -1, g) + : walk(nums, i - 1, suf[i + 1], -1, g); + best = Math.max(best, r - l + 2 - (l <= i && i <= r ? 1 : 0)); + } + return best; + } + + private int walk(int[] nums, int k, int cur, int step, int g) { + while (cur != g) { + cur = gcd(cur, nums[k]); + k += step; + } + return k; + } + + private int gcd(int a, int b) { + while (b != 0) { + int t = a % b; + a = b; + b = t; + } + return a; + } +} diff --git a/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/readme.md b/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/readme.md new file mode 100644 index 000000000..f9bad25d7 --- /dev/null +++ b/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/readme.md @@ -0,0 +1,81 @@ +4035\. Maximum Valid Split Positions I + +Medium + +You are given an integer array `nums`. + +You may remove **at most one** element from `nums`. Let `arr` be the array of remaining elements in their original order, and let `m` be its length. + +A **split position** `i` of `arr` is **valid** if: + +* `0 <= i < m - 1`, and +* `gcd(arr[0..i]) == gcd(arr[i + 1..m - 1])`. + +An array of length 1 has no valid split positions. + +The **score** of `arr` is the number of valid split positions in it. + +Return the **maximum possible score** of `arr`. + +Here, `gcd(a)` denotes the **greatest common divisor** of all elements in the array `a`. + +**Example 1:** + +**Input:** nums = [10,30,15,10] + +**Output:** 2 + +**Explanation:** + +One optimal solution is to remove `nums[2] = 15`. Then `arr = [10, 30, 10]`. + +The split positions are: + +| Split Position `i` | `gcd(arr[0..i])` | `gcd(arr[i + 1..m - 1])` | +| --- | --- | --- | +| 0 | 10 | 10 | +| 1 | 10 | 10 | + +All split positions are valid. Thus, the answer is 2. + +**Example 2:** + +**Input:** nums = [2,10,14] + +**Output:** 1 + +**Explanation:** + +One optimal solution is to not remove any element. Then `arr = [2, 10, 14]`. + +The split positions are: + +| Split Position `i` | `gcd(arr[0..i])` | `gcd(arr[i + 1..m - 1])` | +| --- | --- | --- | +| 0 | 2 | 2 | +| 1 | 2 | 14 | + +Only the split position at index 0 is valid. Thus, the answer is 1. + +**Example 3:** + +**Input:** nums = [2,4] + +**Output:** 0 + +**Explanation:** + +The only remaining array that has a split position is `arr = [2, 4]`. + +The split positions are: + +| Split Position `i` | `gcd(arr[0..i])` | `gcd(arr[i + 1..m - 1])` | +| --- | --- | --- | +| 0 | 2 | 4 | + +There are no valid split positions. Thus, the answer is 0. + +**Constraints:** + +* `2 <= nums.length <= 1000` +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/Solution.java b/src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/Solution.java new file mode 100644 index 000000000..b07a0ccbf --- /dev/null +++ b/src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/Solution.java @@ -0,0 +1,31 @@ +package g4001_4100.s4036_lexicographically_largest_string_after_pair_transformations; + +// #Medium #Array #String #Greedy #Bit_Manipulation #Staff #Biweekly_Contest_190 +// #2026_11_25_Time_46_ms_(99.50%)_Space_130.17_MB_(98.51%) + +public class Solution { + public String[] largestString(int[] nums) { + int n = nums.length; + String[] ans = new String[n]; + for (int i = 0; i < n; i++) { + int num = nums[i]; + int mask = num & 0b1111111111111111111111111; + int zCount = num >>> 25; + char[] arr = new char[Integer.bitCount(mask) + zCount]; + for (int j = 0; j < zCount; j++) { + arr[j] = 'z'; + } + int index = arr.length - 1; + char c = 'a'; + while (mask > 0) { + if ((mask & 1) == 1) { + arr[index--] = c; + } + c++; + mask >>>= 1; + } + ans[i] = String.valueOf(arr); + } + return ans; + } +} diff --git a/src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/readme.md b/src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/readme.md new file mode 100644 index 000000000..6db98f4a5 --- /dev/null +++ b/src/main/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/readme.md @@ -0,0 +1,50 @@ +4036\. Lexicographically Largest String After Pair Transformations + +Medium + +You are given an integer array `nums`. + +For each integer `x` in `nums`, start with a string consisting of exactly `x` lowercase `'a'` characters. + +You may perform the following operation any number of times (including zero): + +* Choose two **adjacent equal** letters and replace them with the next letter in the alphabet. + +For example, `"aa"` can be replaced with `"b"`, and `"bb"` can be replaced with `"c"`. The pair `"zz"` cannot be replaced. + +For each `x`, determine the **lexicographically largest** string that can be obtained. + +Return an array of strings where the ith string is the answer for `nums[i]`. + +A string `a` is **lexicographically larger** than a string `b` if, at the first position where they differ, `a` contains a letter that appears later in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters are equal, the longer string is lexicographically larger. + +**Example 1:** + +**Input:** nums = [2,5,7] + +**Output:** ["b","ca","cba"] + +**Explanation:** + +* `nums[0] = 2`: `"aa"` → `"b"`. +* `nums[1] = 5`: `"aaaaa"` → `"baaa"` → `"bba"` → `"ca"`. +* `nums[2] = 7`: `"aaaaaaa"` → `"baaaaa"` → `"bbaaa"` → `"bbba"` → `"cba"`. +* Therefore, `ans = ["b", "ca", "cba"]`. + +**Example 2:** + +**Input:** nums = [3,9,1] + +**Output:** ["ba","da","a"] + +**Explanation:** + +* `nums[0] = 3`: `"aaa"` → `"ba"`. +* `nums[1] = 9`: `"aaaaaaaaa"` → `"baaaaaaa"` → `"bbaaaaa"` → `"bbbaaa"` → `"bbbba"` → `"cbba"` → `"cca"` → `"da"`. +* `nums[2] = 1`: No transformation can be applied, so the result is `"a"`. +* Therefore, `ans = ["ba", "da", "a"]`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 108 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/Solution.java b/src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/Solution.java new file mode 100644 index 000000000..78d92e96d --- /dev/null +++ b/src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/Solution.java @@ -0,0 +1,77 @@ +package g4001_4100.s4037_maximum_valid_split_positions_ii; + +// #Hard #Array #Math #Enumeration #Number_Theory #Principal #Biweekly_Contest_190 +// #2026_11_25_Time_12_ms_(95.65%)_Space_120.92_MB_(69.57%) + +public class Solution { + public int maxValidSplits(int[] nums) { + int n = nums.length; + int[] preGcd = new int[n]; + int g = 0; + for (int i = 0; i < n; i++) { + g = gcd(g, nums[i]); + preGcd[i] = g; + } + int[] sufGcd = new int[n + 1]; + for (int i = n - 1; i >= 0; i--) { + sufGcd[i] = gcd(sufGcd[i + 1], nums[i]); + } + int allGcd = sufGcd[0]; + int p = 0; + while (preGcd[p] != allGcd) { + p++; + } + int q = n - 1; + while (sufGcd[q] != allGcd) { + q--; + } + int ans = Math.max(q - p, 0); + for (int i = 0; i < n; i++) { + if (i > 0 && preGcd[i] == preGcd[i - 1]) { + continue; + } + int newG = i > 0 ? gcd(preGcd[i - 1], sufGcd[i + 1]) : sufGcd[i + 1]; + if (newG == allGcd) { + continue; + } + g = 0; + for (int j = 0; j < n; j++) { + if (j == i) { + continue; + } + g = gcd(g, nums[j]); + if (g == newG) { + p = j; + break; + } + } + g = 0; + for (int j = n - 1; j >= 0; j--) { + if (j == i) { + continue; + } + g = gcd(g, nums[j]); + if (g == newG) { + q = j; + break; + } + } + int res = q - p; + if (p <= i && i < q) { + res--; + } + ans = Math.max(ans, res); + break; + } + return ans; + } + + private int gcd(int a, int b) { + while (a != 0) { + int tmp = a; + a = b % a; + b = tmp; + } + return b; + } +} diff --git a/src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/readme.md b/src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/readme.md new file mode 100644 index 000000000..27d94ca4a --- /dev/null +++ b/src/main/java/g4001_4100/s4037_maximum_valid_split_positions_ii/readme.md @@ -0,0 +1,81 @@ +4037\. Maximum Valid Split Positions II + +Hard + +You are given an integer array `nums`. + +You may remove **at most one** element from `nums`. Let `arr` be the array of remaining elements in their original order, and let `m` be its length. + +A **split position** `i` of `arr` is **valid** if: + +* `0 <= i < m - 1`, and +* `gcd(arr[0..i]) == gcd(arr[i + 1..m - 1])`. + +An array of length 1 has no valid split positions. + +The **score** of `arr` is the number of valid split positions in it. + +Return the **maximum possible score** of `arr`. + +Here, `gcd(a)` denotes the **greatest common divisor** of all elements in the array `a`. + +**Example 1:** + +**Input:** nums = [10,30,15,10] + +**Output:** 2 + +**Explanation:** + +One optimal solution is to remove `nums[2] = 15`. Then `arr = [10, 30, 10]`. + +The split positions are: + +| Split Position `i` | `gcd(arr[0..i])` | `gcd(arr[i + 1..m - 1])` | +| --- | --- | --- | +| 0 | 10 | 10 | +| 1 | 10 | 10 | + +All split positions are valid. Thus, the answer is 2. + +**Example 2:** + +**Input:** nums = [2,10,14] + +**Output:** 1 + +**Explanation:** + +One optimal solution is to not remove any element. Then `arr = [2, 10, 14]`. + +The split positions are: + +| Split Position `i` | `gcd(arr[0..i])` | `gcd(arr[i + 1..m - 1])` | +| --- | --- | --- | +| 0 | 2 | 2 | +| 1 | 2 | 14 | + +Only the split position at index 0 is valid. Thus, the answer is 1. + +**Example 3:** + +**Input:** nums = [2,4] + +**Output:** 0 + +**Explanation:** + +The only remaining array that has a split position is `arr = [2, 4]`. + +The split positions are: + +| Split Position `i` | `gcd(arr[0..i])` | `gcd(arr[i + 1..m - 1])` | +| --- | --- | --- | +| 0 | 2 | 4 | + +There are no valid split positions. Thus, the answer is 0. + +**Constraints:** + +* 2 <= nums.length <= 105 +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/Solution.java b/src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/Solution.java new file mode 100644 index 000000000..f4aac10fa --- /dev/null +++ b/src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/Solution.java @@ -0,0 +1,28 @@ +package g4001_4100.s4038_count_integers_appearing_in_a_single_block; + +// #Easy #Array #Hash_Table #Counting #Mid_Level #Weekly_Contest_517 +// #2026_11_25_Time_1_ms_(99.14%)_Space_43.37_MB_(73.66%) + +public class Solution { + public int countSpecialIntegers(int[] nums) { + boolean[] seen = new boolean[101]; + boolean[] special = new boolean[101]; + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (i == 0 || nums[i] != nums[i - 1]) { + if (seen[nums[i]]) { + special[nums[i]] = false; + } else { + seen[nums[i]] = true; + special[nums[i]] = true; + } + } + } + for (int x = 1; x <= 100; x++) { + if (special[x]) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/readme.md b/src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/readme.md new file mode 100644 index 000000000..54b1d9c2f --- /dev/null +++ b/src/main/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/readme.md @@ -0,0 +1,41 @@ +4038\. Count Integers Appearing in a Single Block + +Easy + +You are given an integer array `nums`. + +An integer `x` is **special** if all occurrences of `x` in `nums` appear in a single **contiguous** block. + +Return the number of **distinct** special integers in `nums`. + +**Example 1:** + +**Input:** nums = [1,2,2,1] + +**Output:** 1 + +**Explanation:** + +* 1 appears at indices 0 and 3, forming two separate blocks, so it is not special. +* 2 appears in a single contiguous block at indices `[1, 2]`, so it is special. + +Therefore, there is one special integer. + +**Example 2:** + +**Input:** nums = [3,3,1,2,2,1] + +**Output:** 2 + +**Explanation:** + +* 3 appears in a single contiguous block at indices `[0, 1]`, so it is special. +* 1 appears at indices 2 and 5, forming two separate blocks, so it is not special. +* 2 appears in a single contiguous block at indices `[3, 4]`, so it is special. + +Therefore, there are two special integers. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java b/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java new file mode 100644 index 000000000..f7f176050 --- /dev/null +++ b/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java @@ -0,0 +1,45 @@ +package g4001_4100.s4039_sum_of_decoded_numbers; + +// #Medium #Array #Math #Simulation #Staff #Weekly_Contest_517 +// #2026_11_25_Time_48_ms_(99.45%)_Space_78.74_MB_(98.35%) + +public class Solution { + private static final int MOD = (int) 1e9 + 7; + private static final int N = 16; + private static final long[] POW_10 = new long[N]; + + public int sumDecoded(long[] nums) { + if (POW_10[0] == 0) { + POW_10[0] = 1; + for (int i = 1; i < N; ++i) { + POW_10[i] = POW_10[i - 1] * 10; + } + } + long sum = 0; + for (long num : nums) { + int w = (int) (num % 10); + long v = num / 10; + int len = 1; + while (v >= POW_10[len]) { + len++; + } + long base = POW_10[len - w]; + long x = v / base; + long y = v % base; + sum += pow(x, y); + } + return (int) (sum % MOD); + } + + private long pow(long x, long n) { + long result = 1; + while (n > 0) { + if ((n & 1) == 1) { + result = result * x % MOD; + } + x = x * x % MOD; + n >>= 1; + } + return result; + } +} diff --git a/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/readme.md b/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/readme.md new file mode 100644 index 000000000..e32124090 --- /dev/null +++ b/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/readme.md @@ -0,0 +1,67 @@ +4039\. Sum of Decoded Numbers + +Medium + +You are given an integer array `nums`. + +Each `nums[i]` is an **encoded** integer representing two positive integers xi and yi. To decode `nums[i]`, define: + +* widthi = nums[i] % 10. +* di = floor(nums[i] / 10). +* xi as the integer formed by the first widthi digits of the decimal representation of di. +* yi as the integer formed by all remaining digits of the decimal representation of di. + +It is guaranteed that the decimal representation of di contains more than widthi digits. Therefore, both xi and yi contain at least one digit. + +The **decoded value** of `nums[i]` is xiyi. + +Return the sum of the decoded values of all elements in `nums`, modulo 109 + 7. + +The `floor()` function returns the integer part of the division. + +**Example 1:** + +**Input:** nums = [231] + +**Output:** 8 + +**Explanation:** + +* For 231, we have `width = 1`, `d = 23`, `x = 2`, and `y = 3`. +* The decoded value of 231 is 23 = 8. +* Since there is only one element in `nums`, the sum of the decoded values is 8. + +**Example 2:** + +**Input:** nums = [2522,2101] + +**Output:** 1649 + +**Explanation:** + +* For 2522, we have `width = 2`, `d = 252`, `x = 25`, and `y = 2`. +* The decoded value of 2522 is 252 = 625. +* For 2101, we have `width = 1`, `d = 210`, `x = 2`, and `y = 10`. +* The decoded value of 2101 is 210 = 1024. +* The sum of the decoded values is `625 + 1024 = 1649`. + +**Example 3:** + +**Input:** nums = [2301] + +**Output:** 73741817 + +**Explanation:** + +* For 2301, we have `width = 1`, `d = 230`, `x = 2`, and `y = 30`. +* The decoded value is 230 = 1073741824. +* Therefore, the answer is 1073741824 modulo (109 + 7) = 73741817. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 100 < nums[i] < 1015 +* 1 <= widthi <= 9 +* 1 <= xi, yi < 109 +* The digit sequences used to form xi and yi do not have leading zeros. +* It is guaranteed that every element in `nums` is a valid encoded integer. \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java b/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java new file mode 100644 index 000000000..5ea2e0ba3 --- /dev/null +++ b/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java @@ -0,0 +1,46 @@ +package g4001_4100.s4040_minimum_operations_to_form_subset_sum_i; + +// #Medium #Array #Dynamic_Programming #Staff #Weekly_Contest_517 +// #2026_11_25_Time_64_ms_(91.80%)_Space_46.09_MB_(90.16%) + +import java.util.Arrays; + +public class Solution { + private static final int INF = 1000000; + + public int minOperations(int[] nums, int sum) { + int[] dp = new int[sum + 1]; + Arrays.fill(dp, INF); + dp[0] = 0; + + for (int x : nums) { + int[] next = dp.clone(); + long value = x; + int cost = 0; + while (value <= sum) { + int v = (int) value; + for (int s = v; s <= sum; s++) { + if (dp[s - v] != INF) { + next[s] = Math.min(next[s], dp[s - v] + cost); + } + } + value *= 2; + cost++; + } + value = x; + cost = 0; + while (value > 1) { + value /= 2; + cost++; + int v = (int) value; + for (int s = v; s <= sum; s++) { + if (dp[s - v] != INF) { + next[s] = Math.min(next[s], dp[s - v] + cost); + } + } + } + dp = next; + } + return dp[sum] == INF ? -1 : dp[sum]; + } +} diff --git a/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/readme.md b/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/readme.md new file mode 100644 index 000000000..9f7afda79 --- /dev/null +++ b/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/readme.md @@ -0,0 +1,53 @@ +4040\. Minimum Operations to Form Subset Sum I + +Medium + +You are given an integer array `nums` and an integer `sum`. + +In one **operation**, choose an element with current value `x` and replace it with either `2 * x` or `floor(x / 2)`. + +For each element, all **multiplication** operations performed on it must occur **before** any **division** operations performed on it. + +Return the **minimum** number of operations needed so that some subset of the resulting array has a sum **exactly** equal to `sum`. If it is impossible, return -1. + +The `floor()` function returns the integer part of the division. + +**Example 1:** + +**Input:** nums = [5,6,10], sum = 4 + +**Output:** 3 + +**Explanation:** + +* Divide `nums[0] = 5` twice: `5 → 2 → 1`, costing 2 operations. +* Divide `nums[1] = 6` once: `6 → 3`, costing 1 operation. +* After these operations, `nums = [1, 3, 10]`. The subset `{1, 3}` sums to 4 using 3 operations in total. + +**Example 2:** + +**Input:** nums = [10,2], sum = 13 + +**Output:** 3 + +**Explanation:** + +* Divide `nums[0] = 10` once: `10 → 5`, costing 1 operation. +* Multiply `nums[1] = 2` twice: `2 → 4 → 8`, costing 2 operations. +* After these operations, `nums = [5, 8]`. The subset `{5, 8}` sums to 13 using 3 operations in total. + +**Example 3:** + +**Input:** nums = [6,3], sum = 8 + +**Output:** \-1 + +**Explanation:** + +* No sequence of operations lets a subset of `nums` sum to 8, so the answer is -1. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 500` +* `1 <= sum <= 5000` \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java b/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java new file mode 100644 index 000000000..0fd6de1d0 --- /dev/null +++ b/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java @@ -0,0 +1,49 @@ +package g4001_4100.s4041_minimum_operations_to_form_subset_sum_ii; + +// #Hard #Array #Dynamic_Programming #Senior_Staff #Weekly_Contest_517 +// #2026_11_25_Time_89_ms_(98.57%)_Space_46.04_MB_(85.71%) + +import java.util.Arrays; + +public class Solution { + private static final int INF = (int) 1e9; + + public int minOperations(int[] nums, int sum) { + int[] dp = new int[sum + 1]; + Arrays.fill(dp, INF); + dp[0] = 0; + for (int num : nums) { + int[] cost = new int[sum + 1]; + Arrays.fill(cost, INF); + int v = num; + int k = 0; + while (v > 0) { + int t = v; + int m = 0; + while (t <= sum) { + if (k + m < cost[t]) cost[t] = k + m; + t *= 2; + m++; + } + v /= 2; + k++; + } + int[] ndp = dp.clone(); + for (int t = 1; t <= sum; t++) { + int c = cost[t]; + if (c >= INF) { + continue; + } + for (int j = sum; j >= t; j--) { + int cur = dp[j - t] + c; + if (cur < ndp[j]) ndp[j] = cur; + } + } + dp = ndp; + if (dp[sum] == 0) { + return 0; + } + } + return dp[sum] >= INF ? -1 : dp[sum]; + } +} diff --git a/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/readme.md b/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/readme.md new file mode 100644 index 000000000..2b785cc41 --- /dev/null +++ b/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/readme.md @@ -0,0 +1,54 @@ +4041\. Minimum Operations to Form Subset Sum II + +Hard + +You are given an integer array `nums` and an integer `sum`. + +In one **operation**, choose an element with current value `x` and replace it with either `2 * x` or `floor(x / 2)`. + +For each element, **multiplication** and **division** operations may be performed in any order. + +Return the **minimum** number of operations needed so that some subset of the resulting array has a sum **exactly** equal to `sum`. If it is impossible, return -1. + +The `floor()` function returns the integer part of the division. + +**Example 1:** + +**Input:** nums = [10,2], sum = 13 + +**Output:** 3 + +**Explanation:** + +* Divide `nums[0] = 10` once: `10 → 5`, costing 1 operation. +* Multiply `nums[1] = 2` twice: `2 → 4 → 8`, costing 2 operations. +* After these operations, `nums = [5, 8]`. The subset `{5, 8}` sums to 13 using 3 operations in total. + +**Example 2:** + +**Input:** nums = [6,3], sum = 8 + +**Output:** 2 + +**Explanation:** + +* Turn `nums[1] = 3` into 2 using 2 operations: + * Divide `nums[1]` to get 1. + * Multiply `nums[1] = 1` to get 2. +* After these operations, `nums = [6, 2]`. The subset `{6, 2}` sums to 8 using 2 operations in total. + +**Example 3:** + +**Input:** nums = [2,2], sum = 7 + +**Output:** \-1 + +**Explanation:** + +* No sequence of operations lets a subset of `nums` sum to 7, so the answer is -1. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 500` +* `1 <= sum <= 5000` \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/Solution.java b/src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/Solution.java new file mode 100644 index 000000000..811525c9f --- /dev/null +++ b/src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/Solution.java @@ -0,0 +1,17 @@ +package g4001_4100.s4043_count_rotations_with_exactly_k_equal_adjacent_pairs; + +// #Easy #String #Sliding_Window #Enumeration #Mid_Level #Weekly_Contest_518 +// #2026_11_25_Time_2_ms_(81.84%)_Space_43.68_MB_(66.83%) + +public class Solution { + public int countRotations(String s, int k) { + int n = s.length(); + int same = 0; + for (int i = 0; i < n; i++) { + if (s.charAt(i) == s.charAt((i + 1) % n)) { + same++; + } + } + return k == same - 1 ? same : k == same ? n - same : 0; + } +} diff --git a/src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/readme.md b/src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/readme.md new file mode 100644 index 000000000..6b6680329 --- /dev/null +++ b/src/main/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/readme.md @@ -0,0 +1,50 @@ +4043\. Count Rotations With Exactly K Equal Adjacent Pairs + +Easy + +You are given a string `s` of length `n` and an integer `k`. + +A **cyclic rotation** of `s` is obtained by choosing a prefix of `s` whose length is between 0 and `n - 1` (inclusive), and moving it to the end of the string while preserving the order of all characters. + +For **every** cyclic rotation of `s`, let its **score** be the number of indices `i` such that `0 <= i < n - 1` and the characters at positions `i` and `i + 1` are equal. + +Return the number of cyclic rotations of `s` whose score equals `k`. + +**Example 1:** + +**Input:** s = "aab", k = 1 + +**Output:** 2 + +**Explanation:** + +The cyclic rotations of `s` are: + +* `"aab"`: The characters at positions 0 and 1 are equal, so `score = 1`. +* `"aba"`: No two adjacent characters are equal, so `score = 0`. +* `"baa"`: The characters at positions 1 and 2 are equal, so `score = 1`. + +Since `score` equals `k` for 2 cyclic rotations of `s`, the answer is 2. + +**Example 2:** + +**Input:** s = "abca", k = 0 + +**Output:** 1 + +**Explanation:** + +The cyclic rotations of `s` are: + +* `"abca"`: No two adjacent characters are equal, so `score = 0`. +* `"bcaa"`: The characters at positions 2 and 3 are equal, so `score = 1`. +* `"caab"`: The characters at positions 1 and 2 are equal, so `score = 1`. +* `"aabc"`: The characters at positions 0 and 1 are equal, so `score = 1`. + +Since `score` equals `k` for only 1 cyclic rotation of `s`, the answer is 1. + +**Constraints:** + +* `2 <= n == s.length <= 100` +* `s` only consists of lowercase English letters. +* `0 <= k <= n - 1` \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java b/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java new file mode 100644 index 000000000..9c87caec5 --- /dev/null +++ b/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java @@ -0,0 +1,27 @@ +package g4001_4100.s4044_count_good_cyclic_rotations; + +// #Medium #Array #Prefix_Sum #Sliding_Window #Senior #Weekly_Contest_518 +// #2026_11_25_Time_3_ms_(98.00%)_Space_71.09_MB_(63.67%) + +public class Solution { + public int countGoodRotations(int[] nums) { + long leftsum = 0; + long rightsum = 0; + int n = nums.length; + int count = 0; + for (int i = 0; i < n / 2; i++) { + leftsum += nums[i]; + rightsum += nums[n - 1 - i]; + } + int left = 0; + int right = n / 2; + while (right < n) { + leftsum = leftsum - nums[left] + nums[right]; + rightsum = rightsum - nums[right] + nums[left]; + left++; + right++; + if (leftsum != rightsum) count++; + } + return count; + } +} diff --git a/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/readme.md b/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/readme.md new file mode 100644 index 000000000..ea28b6c64 --- /dev/null +++ b/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/readme.md @@ -0,0 +1,57 @@ +4044\. Count Good Cyclic Rotations + +Medium + +You are given an integer array `nums` of even length `n`. + +A **cyclic rotation** of `nums` is obtained by choosing a prefix of `nums` whose length is between 0 and `n - 1` (inclusive), and moving it to the end of the array while preserving the order of all elements. + +A cyclic rotation is **good** if the sum of its first `n / 2` elements is **strictly greater** than the sum of its last `n / 2` elements. + +Return the number of cyclic rotations of `nums` that are good. + +**Example 1:** + +**Input:** nums = [1,2,3,4,5,6] + +**Output:** 3 + +**Explanation:** + +The cyclic rotations of `nums` are: + +| Cyclic rotation | Sum of first `n / 2` elements | Sum of last `n / 2` elements | +| --- | --- | --- | +| `[1, 2, 3, 4, 5, 6]` | `1 + 2 + 3 = 6` | `4 + 5 + 6 = 15` | +| `[2, 3, 4, 5, 6, 1]` | `2 + 3 + 4 = 9` | `5 + 6 + 1 = 12` | +| `[3, 4, 5, 6, 1, 2]` | `3 + 4 + 5 = 12` | `6 + 1 + 2 = 9` | +| `[4, 5, 6, 1, 2, 3]` | `4 + 5 + 6 = 15` | `1 + 2 + 3 = 6` | +| `[5, 6, 1, 2, 3, 4]` | `5 + 6 + 1 = 12` | `2 + 3 + 4 = 9` | +| `[6, 1, 2, 3, 4, 5]` | `6 + 1 + 2 = 9` | `3 + 4 + 5 = 12` | + +The first half has a greater sum than the second half for 3 rotations. Thus, the answer is 3. + +**Example 2:** + +**Input:** nums = [1,2,1,2] + +**Output:** 0 + +**Explanation:** + +The cyclic rotations of `nums` are: + +| Cyclic rotation | Sum of first `n / 2` elements | Sum of last `n / 2` elements | +| --- | --- | --- | +| `[1, 2, 1, 2]` | `1 + 2 = 3` | `1 + 2 = 3` | +| `[2, 1, 2, 1]` | `2 + 1 = 3` | `2 + 1 = 3` | +| `[1, 2, 1, 2]` | `1 + 2 = 3` | `1 + 2 = 3` | +| `[2, 1, 2, 1]` | `2 + 1 = 3` | `2 + 1 = 3` | + +No cyclic rotation is good because the two sums are equal for every rotation. Thus, the answer is 0. + +**Constraints:** + +* 2 <= n == nums.length <= 105 +* 1 <= nums[i] <= 109 +* `n` is even. \ No newline at end of file diff --git a/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java b/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java new file mode 100644 index 000000000..4a94a29d3 --- /dev/null +++ b/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java @@ -0,0 +1,44 @@ +package g4001_4100.s4022_k_th_digit_in_infinite_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void kthDigit() { + assertThat(new Solution().kthDigit(1), equalTo(1)); + } + + @Test + void kthDigit2() { + assertThat(new Solution().kthDigit(9), equalTo(9)); + } + + @Test + void kthDigit3() { + assertThat(new Solution().kthDigit(10), equalTo(1)); + } + + @Test + void kthDigit4() { + assertThat(new Solution().kthDigit(11), equalTo(9)); + } + + @Test + void kthDigit5() { + assertThat(new Solution().kthDigit(15), equalTo(7)); + } + + @Test + void kthDigit6() { + assertThat(new Solution().kthDigit(19), equalTo(5)); + } + + @Test + void kthDigit7() { + assertThat(new Solution().kthDigit(20), equalTo(1)); + } +} + diff --git a/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java b/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java new file mode 100644 index 000000000..bcc05f25f --- /dev/null +++ b/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4023_elevator_requests_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void elevatorRequests() { + assertThat(new Solution().elevatorRequests(10, 5, new int[] {}), equalTo(0L)); + } + + @Test + void elevatorRequests2() { + assertThat(new Solution().elevatorRequests(10, 5, new int[] {5, 5}), equalTo(0L)); + } + + @Test + void elevatorRequests3() { + assertThat(new Solution().elevatorRequests(10, 5, new int[] {2, 4}), equalTo(4L)); + } + + @Test + void elevatorRequests4() { + assertThat(new Solution().elevatorRequests(10, 5, new int[] {7, 9}), equalTo(6L)); + } +} + diff --git a/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java b/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java new file mode 100644 index 000000000..50604ff69 --- /dev/null +++ b/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4024_nearest_available_drone; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void nearestDrone() { + assertThat(new Solution().nearestDrone(new int[][] {{1, 2, 3}, {2, 2, 2}}, new int[] {0, 0}), equalTo(0)); + } + + @Test + void nearestDrone2() { + assertThat(new Solution().nearestDrone(new int[][] {{1, 0, 1}, {-1, 0, 1}}, new int[] {0, 0}), equalTo(0)); + } + + @Test + void nearestDrone3() { + assertThat(new Solution().nearestDrone(new int[][] {{3, 0, 2}, {0, 4, 3}}, new int[] {0, 0}), equalTo(-1)); + } +} + diff --git a/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java b/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java new file mode 100644 index 000000000..6aa3b76b5 --- /dev/null +++ b/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minPenalty() { + assertThat(new Solution().minPenalty(10, new int[] {3, 5}, new int[] {1, 2, 8}), equalTo(2)); + } + + @Test + void minPenalty2() { + assertThat(new Solution().minPenalty(10, new int[] {3}, new int[] {1, 2, 3}), equalTo(7)); + } + + @Test + void minPenalty3() { + assertThat(new Solution().minPenalty(10, new int[] {3}, new int[] {13, 18}), equalTo(7)); + } +} + diff --git a/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java b/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java new file mode 100644 index 000000000..10d114b43 --- /dev/null +++ b/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4026_maximum_gap_between_stations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumGap() { + assertThat(new Solution().maximumGap("abc", "abc"), equalTo(1)); + } + + @Test + void maximumGap2() { + assertThat(new Solution().maximumGap("abc", "axbyc"), equalTo(2)); + } + + @Test + void maximumGap3() { + assertThat(new Solution().maximumGap("a", "anything"), equalTo(0)); + } +} + diff --git a/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java b/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java new file mode 100644 index 000000000..9601552fa --- /dev/null +++ b/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4027_elevator_requests_iii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void elevatorRequests() { + assertThat(new Solution().elevatorRequests(10, 5, new int[][] {}), equalTo(0L)); + } + + @Test + void elevatorRequests2() { + assertThat(new Solution().elevatorRequests(10, 5, new int[][] {{0, 5}, {0, 5}}), equalTo(0L)); + } + + @Test + void elevatorRequests3() { + assertThat(new Solution().elevatorRequests(10, 5, new int[][] {{0, 7}}), equalTo(2L)); + } +} + diff --git a/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java b/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java new file mode 100644 index 000000000..dc3d9d515 --- /dev/null +++ b/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java @@ -0,0 +1,32 @@ +package g4001_4100.s4030_check_ascii_palindromic; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isPalindromic() { + assertThat(new Solution().isPalindromic("A"), equalTo(false)); + } + + @Test + void isPalindromic2() { + assertThat(new Solution().isPalindromic("\u0000"), equalTo(true)); + } + + @Test + void isPalindromic3() { + assertThat(new Solution().isPalindromic("\u0001\u0080"), equalTo(true)); + } + + @Test + void isPalindromic4() { + assertThat(new Solution().isPalindromic("ab"), equalTo(false)); + } +} + + + + diff --git a/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java b/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java new file mode 100644 index 000000000..c7e386d81 --- /dev/null +++ b/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java @@ -0,0 +1,27 @@ +package g4001_4100.s4031_find_all_numbers_disappeared_in_an_array_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Arrays; +import java.util.Collections; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findDisappearedNumbers() { + assertThat(new Solution().findDisappearedNumbers(new int[] {2, 3, 7, 2}, 1, 8), + equalTo(Arrays.asList(Arrays.asList(1, 1), Arrays.asList(4, 6), Arrays.asList(8, 8)))); + } + + @Test + void findDisappearedNumbers2() { + assertThat(new Solution().findDisappearedNumbers(new int[] {1, 2, 3}, 1, 3), equalTo(Collections.emptyList())); + } + + @Test + void findDisappearedNumbers3() { + assertThat(new Solution().findDisappearedNumbers(new int[] {}, 4, 6), equalTo(Arrays.asList(Arrays.asList(4, 6)))); + } +} + diff --git a/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java b/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java new file mode 100644 index 000000000..bf8525086 --- /dev/null +++ b/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4032_longest_subarray_with_at_most_k_distinct_prime_factors; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void longestSubarray() { + assertThat(new Solution().longestSubarray(new int[] {2, 3, 4, 5}, 1), equalTo(1)); + } + + @Test + void longestSubarray2() { + assertThat(new Solution().longestSubarray(new int[] {2, 4, 8}, 1), equalTo(3)); + } + + @Test + void longestSubarray3() { + assertThat(new Solution().longestSubarray(new int[] {2, 3}, 0), equalTo(0)); + } + + @Test + void longestSubarray4() { + assertThat(new Solution().longestSubarray(new int[] {1, 1, 2}, 0), equalTo(2)); + } +} + diff --git a/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java b/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java new file mode 100644 index 000000000..06c0eb157 --- /dev/null +++ b/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java @@ -0,0 +1,26 @@ +package g4001_4100.s4033_valid_k_unique_subarrays_i; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void validSubarrays() { + assertArrayEquals(new boolean[] {true, false}, + new Solution().validSubarrays(new int[] {1, 1}, 1, new int[][] {{0, 1}, {0, 0}})); + } + + @Test + void validSubarrays2() { + assertArrayEquals(new boolean[] {true, false, false}, + new Solution().validSubarrays(new int[] {1, 2, 1, 2}, 2, + new int[][] {{0, 3}, {0, 1}, {1, 2}})); + } + + @Test + void validSubarrays3() { + assertArrayEquals(new boolean[] {}, new Solution().validSubarrays(new int[] {1, 2}, 2, new int[][] {})); + } +} + diff --git a/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java b/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java new file mode 100644 index 000000000..bc5000d63 --- /dev/null +++ b/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4034_minimum_bishop_moves_to_reach_target; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minBishopMoves() { + assertThat(new Solution().minBishopMoves(new int[] {0, 0}, new int[] {0, 0}), equalTo(0)); + } + + @Test + void minBishopMoves2() { + assertThat(new Solution().minBishopMoves(new int[] {0, 0}, new int[] {3, 3}), equalTo(1)); + } + + @Test + void minBishopMoves3() { + assertThat(new Solution().minBishopMoves(new int[] {0, 0}, new int[] {2, 4}), equalTo(2)); + } + + @Test + void minBishopMoves4() { + assertThat(new Solution().minBishopMoves(new int[] {0, 0}, new int[] {1, 0}), equalTo(-1)); + } +} + diff --git a/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java b/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java new file mode 100644 index 000000000..90543cc56 --- /dev/null +++ b/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java @@ -0,0 +1,19 @@ +package g4001_4100.s4035_maximum_valid_split_positions_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxValidSplits() { + assertThat(new Solution().maxValidSplits(new int[] {3, 3, 3}), equalTo(2)); + } + + @Test + void maxValidSplits2() { + assertThat(new Solution().maxValidSplits(new int[] {5}), equalTo(1)); + } +} + diff --git a/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java b/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java new file mode 100644 index 000000000..b5cbc57a1 --- /dev/null +++ b/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java @@ -0,0 +1,18 @@ +package g4001_4100.s4036_lexicographically_largest_string_after_pair_transformations; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void largestString() { + assertArrayEquals(new String[] {"a", "ba", ""}, new Solution().largestString(new int[] {1, 3, 0})); + } + + @Test + void largestString2() { + assertArrayEquals(new String[] {"za"}, new Solution().largestString(new int[] {(1 << 25) | 1})); + } +} + diff --git a/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java b/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java new file mode 100644 index 000000000..d8bfa1443 --- /dev/null +++ b/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4037_maximum_valid_split_positions_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxValidSplits() { + assertThat(new Solution().maxValidSplits(new int[] {2, 4, 6}), equalTo(1)); + } + + @Test + void maxValidSplits2() { + assertThat(new Solution().maxValidSplits(new int[] {3, 3, 3}), equalTo(2)); + } + + @Test + void maxValidSplits3() { + assertThat(new Solution().maxValidSplits(new int[] {5}), equalTo(0)); + } +} + diff --git a/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java b/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java new file mode 100644 index 000000000..4e141120a --- /dev/null +++ b/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4038_count_integers_appearing_in_a_single_block; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countSpecialIntegers() { + assertThat(new Solution().countSpecialIntegers(new int[] {1, 1, 2, 3, 2, 4}), equalTo(3)); + } + + @Test + void countSpecialIntegers2() { + assertThat(new Solution().countSpecialIntegers(new int[] {7, 7, 7}), equalTo(1)); + } + + @Test + void countSpecialIntegers3() { + assertThat(new Solution().countSpecialIntegers(new int[] {}), equalTo(0)); + } +} + diff --git a/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java b/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java new file mode 100644 index 000000000..91651fc21 --- /dev/null +++ b/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java @@ -0,0 +1,24 @@ +package g4001_4100.s4039_sum_of_decoded_numbers; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumDecoded() { + assertThat(new Solution().sumDecoded(new long[] {111}), equalTo(1)); + } + + @Test + void sumDecoded2() { + assertThat(new Solution().sumDecoded(new long[] {111, 112}), equalTo(2)); + } + + @Test + void sumDecoded3() { + assertThat(new Solution().sumDecoded(new long[] {}), equalTo(0)); + } +} + diff --git a/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java b/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java new file mode 100644 index 000000000..1f5d5be62 --- /dev/null +++ b/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4040_minimum_operations_to_form_subset_sum_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minOperations() { + assertThat(new Solution().minOperations(new int[] {1, 2, 8}, 7), equalTo(1)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {1, 2, 4}, 7), equalTo(0)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {2, 4}, 1), equalTo(1)); + } + + @Test + void minOperations4() { + assertThat(new Solution().minOperations(new int[] {}, 0), equalTo(0)); + } +} + diff --git a/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java b/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java new file mode 100644 index 000000000..2f238878e --- /dev/null +++ b/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4041_minimum_operations_to_form_subset_sum_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minOperations() { + assertThat(new Solution().minOperations(new int[] {1, 2, 8}, 7), equalTo(1)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {1, 2, 4}, 7), equalTo(0)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {2, 4}, 1), equalTo(1)); + } + + @Test + void minOperations4() { + assertThat(new Solution().minOperations(new int[] {}, 1), equalTo(-1)); + } +} + diff --git a/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java b/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java new file mode 100644 index 000000000..815715089 --- /dev/null +++ b/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4043_count_rotations_with_exactly_k_equal_adjacent_pairs; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countRotations() { + assertThat(new Solution().countRotations("aaaa", 4), equalTo(0)); + } + + @Test + void countRotations2() { + assertThat(new Solution().countRotations("aaaa", 3), equalTo(4)); + } + + @Test + void countRotations3() { + assertThat(new Solution().countRotations("abab", 0), equalTo(4)); + } + + @Test + void countRotations4() { + assertThat(new Solution().countRotations("abab", 1), equalTo(0)); + } +} + diff --git a/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java b/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java new file mode 100644 index 000000000..426e284de --- /dev/null +++ b/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java @@ -0,0 +1,29 @@ +package g4001_4100.s4044_count_good_cyclic_rotations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countGoodRotations() { + assertThat(new Solution().countGoodRotations(new int[] {1, 2, 3, 4}), equalTo(1)); + } + + @Test + void countGoodRotations2() { + assertThat(new Solution().countGoodRotations(new int[] {1, 1, 1, 1}), equalTo(0)); + } + + @Test + void countGoodRotations3() { + assertThat(new Solution().countGoodRotations(new int[] {5, 1}), equalTo(1)); + } + + @Test + void countGoodRotations4() { + assertThat(new Solution().countGoodRotations(new int[] {7}), equalTo(0)); + } +} + From 3bb943ad24a0a3ed12e6a031b3b4b1ae3c0c29ba Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Wed, 25 Nov 2026 22:13:27 +0700 Subject: [PATCH 2/9] fix format --- .../SolutionTest.java | 1 - .../s4023_elevator_requests_ii/SolutionTest.java | 1 - .../SolutionTest.java | 13 +++++++++---- .../SolutionTest.java | 4 ++-- .../SolutionTest.java | 1 - .../SolutionTest.java | 4 ++-- .../SolutionTest.java | 4 ---- .../SolutionTest.java | 16 +++++++++++----- .../SolutionTest.java | 1 - .../SolutionTest.java | 16 ++++++++++------ .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - .../SolutionTest.java | 7 ++++--- .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - .../SolutionTest.java | 1 - 20 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java b/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java index 4a94a29d3..bbb49e696 100644 --- a/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java +++ b/src/test/java/g4001_4100/s4022_k_th_digit_in_infinite_string/SolutionTest.java @@ -41,4 +41,3 @@ void kthDigit7() { assertThat(new Solution().kthDigit(20), equalTo(1)); } } - diff --git a/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java b/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java index bcc05f25f..c15f6a7af 100644 --- a/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4023_elevator_requests_ii/SolutionTest.java @@ -26,4 +26,3 @@ void elevatorRequests4() { assertThat(new Solution().elevatorRequests(10, 5, new int[] {7, 9}), equalTo(6L)); } } - diff --git a/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java b/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java index 50604ff69..0ebb3df7b 100644 --- a/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java +++ b/src/test/java/g4001_4100/s4024_nearest_available_drone/SolutionTest.java @@ -8,17 +8,22 @@ class SolutionTest { @Test void nearestDrone() { - assertThat(new Solution().nearestDrone(new int[][] {{1, 2, 3}, {2, 2, 2}}, new int[] {0, 0}), equalTo(0)); + assertThat( + new Solution().nearestDrone(new int[][] {{1, 2, 3}, {2, 2, 2}}, new int[] {0, 0}), + equalTo(0)); } @Test void nearestDrone2() { - assertThat(new Solution().nearestDrone(new int[][] {{1, 0, 1}, {-1, 0, 1}}, new int[] {0, 0}), equalTo(0)); + assertThat( + new Solution().nearestDrone(new int[][] {{1, 0, 1}, {-1, 0, 1}}, new int[] {0, 0}), + equalTo(0)); } @Test void nearestDrone3() { - assertThat(new Solution().nearestDrone(new int[][] {{3, 0, 2}, {0, 4, 3}}, new int[] {0, 0}), equalTo(-1)); + assertThat( + new Solution().nearestDrone(new int[][] {{3, 0, 2}, {0, 4, 3}}, new int[] {0, 0}), + equalTo(-1)); } } - diff --git a/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java b/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java index 6aa3b76b5..dd2d2f1dd 100644 --- a/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java +++ b/src/test/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/SolutionTest.java @@ -8,7 +8,8 @@ class SolutionTest { @Test void minPenalty() { - assertThat(new Solution().minPenalty(10, new int[] {3, 5}, new int[] {1, 2, 8}), equalTo(2)); + assertThat( + new Solution().minPenalty(10, new int[] {3, 5}, new int[] {1, 2, 8}), equalTo(2)); } @Test @@ -21,4 +22,3 @@ void minPenalty3() { assertThat(new Solution().minPenalty(10, new int[] {3}, new int[] {13, 18}), equalTo(7)); } } - diff --git a/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java b/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java index 10d114b43..23ace95b1 100644 --- a/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java +++ b/src/test/java/g4001_4100/s4026_maximum_gap_between_stations/SolutionTest.java @@ -21,4 +21,3 @@ void maximumGap3() { assertThat(new Solution().maximumGap("a", "anything"), equalTo(0)); } } - diff --git a/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java b/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java index 9601552fa..d7e514091 100644 --- a/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4027_elevator_requests_iii/SolutionTest.java @@ -13,7 +13,8 @@ void elevatorRequests() { @Test void elevatorRequests2() { - assertThat(new Solution().elevatorRequests(10, 5, new int[][] {{0, 5}, {0, 5}}), equalTo(0L)); + assertThat( + new Solution().elevatorRequests(10, 5, new int[][] {{0, 5}, {0, 5}}), equalTo(0L)); } @Test @@ -21,4 +22,3 @@ void elevatorRequests3() { assertThat(new Solution().elevatorRequests(10, 5, new int[][] {{0, 7}}), equalTo(2L)); } } - diff --git a/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java b/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java index dc3d9d515..be50bdafc 100644 --- a/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java +++ b/src/test/java/g4001_4100/s4030_check_ascii_palindromic/SolutionTest.java @@ -26,7 +26,3 @@ void isPalindromic4() { assertThat(new Solution().isPalindromic("ab"), equalTo(false)); } } - - - - diff --git a/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java b/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java index c7e386d81..7e605350e 100644 --- a/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4031_find_all_numbers_disappeared_in_an_array_ii/SolutionTest.java @@ -10,18 +10,24 @@ class SolutionTest { @Test void findDisappearedNumbers() { - assertThat(new Solution().findDisappearedNumbers(new int[] {2, 3, 7, 2}, 1, 8), - equalTo(Arrays.asList(Arrays.asList(1, 1), Arrays.asList(4, 6), Arrays.asList(8, 8)))); + assertThat( + new Solution().findDisappearedNumbers(new int[] {2, 3, 7, 2}, 1, 8), + equalTo( + Arrays.asList( + Arrays.asList(1, 1), Arrays.asList(4, 6), Arrays.asList(8, 8)))); } @Test void findDisappearedNumbers2() { - assertThat(new Solution().findDisappearedNumbers(new int[] {1, 2, 3}, 1, 3), equalTo(Collections.emptyList())); + assertThat( + new Solution().findDisappearedNumbers(new int[] {1, 2, 3}, 1, 3), + equalTo(Collections.emptyList())); } @Test void findDisappearedNumbers3() { - assertThat(new Solution().findDisappearedNumbers(new int[] {}, 4, 6), equalTo(Arrays.asList(Arrays.asList(4, 6)))); + assertThat( + new Solution().findDisappearedNumbers(new int[] {}, 4, 6), + equalTo(Arrays.asList(Arrays.asList(4, 6)))); } } - diff --git a/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java b/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java index bf8525086..c9a483f5b 100644 --- a/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java +++ b/src/test/java/g4001_4100/s4032_longest_subarray_with_at_most_k_distinct_prime_factors/SolutionTest.java @@ -26,4 +26,3 @@ void longestSubarray4() { assertThat(new Solution().longestSubarray(new int[] {1, 1, 2}, 0), equalTo(2)); } } - diff --git a/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java b/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java index 06c0eb157..2e86439ed 100644 --- a/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java +++ b/src/test/java/g4001_4100/s4033_valid_k_unique_subarrays_i/SolutionTest.java @@ -7,20 +7,24 @@ class SolutionTest { @Test void validSubarrays() { - assertArrayEquals(new boolean[] {true, false}, + assertArrayEquals( + new boolean[] {true, false}, new Solution().validSubarrays(new int[] {1, 1}, 1, new int[][] {{0, 1}, {0, 0}})); } @Test void validSubarrays2() { - assertArrayEquals(new boolean[] {true, false, false}, - new Solution().validSubarrays(new int[] {1, 2, 1, 2}, 2, - new int[][] {{0, 3}, {0, 1}, {1, 2}})); + assertArrayEquals( + new boolean[] {true, false, false}, + new Solution() + .validSubarrays( + new int[] {1, 2, 1, 2}, 2, new int[][] {{0, 3}, {0, 1}, {1, 2}})); } @Test void validSubarrays3() { - assertArrayEquals(new boolean[] {}, new Solution().validSubarrays(new int[] {1, 2}, 2, new int[][] {})); + assertArrayEquals( + new boolean[] {}, + new Solution().validSubarrays(new int[] {1, 2}, 2, new int[][] {})); } } - diff --git a/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java b/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java index bc5000d63..20af47c74 100644 --- a/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java +++ b/src/test/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/SolutionTest.java @@ -26,4 +26,3 @@ void minBishopMoves4() { assertThat(new Solution().minBishopMoves(new int[] {0, 0}, new int[] {1, 0}), equalTo(-1)); } } - diff --git a/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java b/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java index 90543cc56..920b18323 100644 --- a/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java +++ b/src/test/java/g4001_4100/s4035_maximum_valid_split_positions_i/SolutionTest.java @@ -16,4 +16,3 @@ void maxValidSplits2() { assertThat(new Solution().maxValidSplits(new int[] {5}), equalTo(1)); } } - diff --git a/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java b/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java index b5cbc57a1..b55bbfacb 100644 --- a/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java +++ b/src/test/java/g4001_4100/s4036_lexicographically_largest_string_after_pair_transformations/SolutionTest.java @@ -7,12 +7,13 @@ class SolutionTest { @Test void largestString() { - assertArrayEquals(new String[] {"a", "ba", ""}, new Solution().largestString(new int[] {1, 3, 0})); + assertArrayEquals( + new String[] {"a", "ba", ""}, new Solution().largestString(new int[] {1, 3, 0})); } @Test void largestString2() { - assertArrayEquals(new String[] {"za"}, new Solution().largestString(new int[] {(1 << 25) | 1})); + assertArrayEquals( + new String[] {"za"}, new Solution().largestString(new int[] {(1 << 25) | 1})); } } - diff --git a/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java b/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java index d8bfa1443..f4a1cfee2 100644 --- a/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4037_maximum_valid_split_positions_ii/SolutionTest.java @@ -21,4 +21,3 @@ void maxValidSplits3() { assertThat(new Solution().maxValidSplits(new int[] {5}), equalTo(0)); } } - diff --git a/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java b/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java index 4e141120a..6b2495ea3 100644 --- a/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java +++ b/src/test/java/g4001_4100/s4038_count_integers_appearing_in_a_single_block/SolutionTest.java @@ -21,4 +21,3 @@ void countSpecialIntegers3() { assertThat(new Solution().countSpecialIntegers(new int[] {}), equalTo(0)); } } - diff --git a/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java b/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java index 91651fc21..35017103a 100644 --- a/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java +++ b/src/test/java/g4001_4100/s4039_sum_of_decoded_numbers/SolutionTest.java @@ -21,4 +21,3 @@ void sumDecoded3() { assertThat(new Solution().sumDecoded(new long[] {}), equalTo(0)); } } - diff --git a/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java b/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java index 1f5d5be62..0f926592a 100644 --- a/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java +++ b/src/test/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/SolutionTest.java @@ -26,4 +26,3 @@ void minOperations4() { assertThat(new Solution().minOperations(new int[] {}, 0), equalTo(0)); } } - diff --git a/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java b/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java index 2f238878e..55d2916d8 100644 --- a/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/SolutionTest.java @@ -26,4 +26,3 @@ void minOperations4() { assertThat(new Solution().minOperations(new int[] {}, 1), equalTo(-1)); } } - diff --git a/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java b/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java index 815715089..02b03cae2 100644 --- a/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java +++ b/src/test/java/g4001_4100/s4043_count_rotations_with_exactly_k_equal_adjacent_pairs/SolutionTest.java @@ -26,4 +26,3 @@ void countRotations4() { assertThat(new Solution().countRotations("abab", 1), equalTo(0)); } } - diff --git a/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java b/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java index 426e284de..63080c79a 100644 --- a/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java +++ b/src/test/java/g4001_4100/s4044_count_good_cyclic_rotations/SolutionTest.java @@ -26,4 +26,3 @@ void countGoodRotations4() { assertThat(new Solution().countGoodRotations(new int[] {7}), equalTo(0)); } } - From 96614d1b72716175c8dcd654fb26e2e5a323185e Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Wed, 25 Nov 2026 22:15:56 +0700 Subject: [PATCH 3/9] fix format --- .../Solution.java | 10 +++++++--- .../Solution.java | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java b/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java index b1e2f8692..0999ac9f4 100644 --- a/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java +++ b/src/main/java/g4001_4100/s4034_minimum_bishop_moves_to_reach_target/Solution.java @@ -5,9 +5,13 @@ public class Solution { public int minBishopMoves(int[] source, int[] target) { - int r1 = source[0], c1 = source[1]; - int r2 = target[0], c2 = target[1]; - if (r1 == r2 && c1 == c2) return 0; + int r1 = source[0]; + int c1 = source[1]; + int r2 = target[0]; + int c2 = target[1]; + if (r1 == r2 && c1 == c2) { + return 0; + } if (Math.abs(r1 - r2) == Math.abs(c1 - c2)) { return 1; } diff --git a/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java b/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java index fb515c89e..49018f8dd 100644 --- a/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java +++ b/src/main/java/g4001_4100/s4035_maximum_valid_split_positions_i/Solution.java @@ -14,7 +14,8 @@ public int maxValidSplits(int[] nums) { for (int k = n - 1; k >= 0; k--) { suf[k] = gcd(suf[k + 1], nums[k]); } - int total = pref[n], best = 0; + int total = pref[n]; + int best = 0; for (int k = 0; k <= n; k++) { if (pref[k] == suf[k]) { best++; From d8e9bdbbba5585c008ea15b3db8f1ddc43926a53 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Wed, 25 Nov 2026 22:19:49 +0700 Subject: [PATCH 4/9] fix format --- .../Solution.java | 8 ++++++-- .../s4044_count_good_cyclic_rotations/Solution.java | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java b/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java index 0fd6de1d0..54df49055 100644 --- a/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java +++ b/src/main/java/g4001_4100/s4041_minimum_operations_to_form_subset_sum_ii/Solution.java @@ -21,7 +21,9 @@ public int minOperations(int[] nums, int sum) { int t = v; int m = 0; while (t <= sum) { - if (k + m < cost[t]) cost[t] = k + m; + if (k + m < cost[t]) { + cost[t] = k + m; + } t *= 2; m++; } @@ -36,7 +38,9 @@ public int minOperations(int[] nums, int sum) { } for (int j = sum; j >= t; j--) { int cur = dp[j - t] + c; - if (cur < ndp[j]) ndp[j] = cur; + if (cur < ndp[j]) { + ndp[j] = cur; + } } } dp = ndp; diff --git a/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java b/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java index 9c87caec5..537b31f7a 100644 --- a/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java +++ b/src/main/java/g4001_4100/s4044_count_good_cyclic_rotations/Solution.java @@ -20,7 +20,9 @@ public int countGoodRotations(int[] nums) { rightsum = rightsum - nums[right] + nums[left]; left++; right++; - if (leftsum != rightsum) count++; + if (leftsum != rightsum) { + count++; + } } return count; } From 9814e17a17a19b2ac82ea7dc2c10fdb145addc4f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 25 Nov 2026 17:35:08 +0200 Subject: [PATCH 5/9] Refactor nearestDrone method for clarity --- .../g4001_4100/s4024_nearest_available_drone/Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java b/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java index 90cd15b58..9ba324628 100644 --- a/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java +++ b/src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java @@ -6,18 +6,15 @@ public class Solution { public int nearestDrone(int[][] drones, int[] target) { int ri = -1; - for (int i = 0, l = drones.length, x = target[0], y = target[1], rv = Integer.MAX_VALUE, d; i < l; i++) { d = Math.abs(x - drones[i][0]) + Math.abs(y - drones[i][1]); - if (d < rv && d <= drones[i][2]) { rv = d; ri = i; } } - return ri; } } From c13e70a54d48817755c166e3eaf9e9d136e3c122 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 25 Nov 2026 17:35:44 +0200 Subject: [PATCH 6/9] Fix logic for calculating maximum waiting time --- .../Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java b/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java index 180055ff7..14a656361 100644 --- a/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java +++ b/src/main/java/g4001_4100/s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights/Solution.java @@ -13,7 +13,6 @@ public int minPenalty(int period, int[] lights, int[] arrivalTime) { for (int i : arrivalTime) { int r = i % period; int curr = period - r; - if (r >= greenMax) { ans = Math.max(ans, curr); } From 9575f8c618cfe70d69865f81131c5973ecc30f93 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 25 Nov 2026 17:37:36 +0200 Subject: [PATCH 7/9] Refactor Solution.java for improved readability --- .../g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java b/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java index c597fc9b8..b63a65c1f 100644 --- a/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java +++ b/src/main/java/g4001_4100/s4033_valid_k_unique_subarrays_i/Solution.java @@ -17,7 +17,6 @@ public boolean[] validSubarrays(int[] nums, int k, int[][] queries) { maxNum = Math.max(maxNum, nums[i]); prefixXor[i + 1] = prefixXor[i] ^ nums[i]; } - int[][] sortedQueries = new int[queries.length][]; int queriesCount = this.sortQueries(nums, k, prefixXor, queries, sortedQueries); boolean[] result = new boolean[queries.length]; @@ -45,7 +44,6 @@ public boolean[] validSubarrays(int[] nums, int k, int[][] queries) { } result[query[2]] = countDistinct == k && countOdd == 0; } - return result; } From 0689a7f71dbe8603accb368ffce2ba460f6f4901 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 25 Nov 2026 17:38:50 +0200 Subject: [PATCH 8/9] Refactor minOperations method in Solution.java --- .../s4040_minimum_operations_to_form_subset_sum_i/Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java b/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java index 5ea2e0ba3..13ad28dae 100644 --- a/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java +++ b/src/main/java/g4001_4100/s4040_minimum_operations_to_form_subset_sum_i/Solution.java @@ -12,7 +12,6 @@ public int minOperations(int[] nums, int sum) { int[] dp = new int[sum + 1]; Arrays.fill(dp, INF); dp[0] = 0; - for (int x : nums) { int[] next = dp.clone(); long value = x; From 88363fc61833f00bb81155b76732b1f6908c3599 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 25 Nov 2026 17:47:14 +0200 Subject: [PATCH 9/9] Refactor POW_10 to pow10 for better encapsulation --- .../s4039_sum_of_decoded_numbers/Solution.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java b/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java index f7f176050..d215ce431 100644 --- a/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java +++ b/src/main/java/g4001_4100/s4039_sum_of_decoded_numbers/Solution.java @@ -1,18 +1,18 @@ package g4001_4100.s4039_sum_of_decoded_numbers; // #Medium #Array #Math #Simulation #Staff #Weekly_Contest_517 -// #2026_11_25_Time_48_ms_(99.45%)_Space_78.74_MB_(98.35%) +// #2026_11_25_Time_50_ms_(98.90%)_Space_78.76_MB_(98.35%) public class Solution { private static final int MOD = (int) 1e9 + 7; private static final int N = 16; - private static final long[] POW_10 = new long[N]; + private final long[] pow10 = new long[N]; public int sumDecoded(long[] nums) { - if (POW_10[0] == 0) { - POW_10[0] = 1; + if (pow10[0] == 0) { + pow10[0] = 1; for (int i = 1; i < N; ++i) { - POW_10[i] = POW_10[i - 1] * 10; + pow10[i] = pow10[i - 1] * 10; } } long sum = 0; @@ -20,10 +20,10 @@ public int sumDecoded(long[] nums) { int w = (int) (num % 10); long v = num / 10; int len = 1; - while (v >= POW_10[len]) { + while (v >= pow10[len]) { len++; } - long base = POW_10[len - w]; + long base = pow10[len - w]; long x = v / base; long y = v % base; sum += pow(x, y);