diff --git a/src/main/java/g3801_3900/s3899_angles_of_a_triangle/Solution.java b/src/main/java/g3801_3900/s3899_angles_of_a_triangle/Solution.java new file mode 100644 index 000000000..06eaf7162 --- /dev/null +++ b/src/main/java/g3801_3900/s3899_angles_of_a_triangle/Solution.java @@ -0,0 +1,60 @@ +package g3801_3900.s3899_angles_of_a_triangle; + +// #Medium #Array #Math #Geometry #Senior #Weekly_Contest_497 +// #2026_09_17_Time_1_ms_(100.00%)_Space_49.33_MB_(42.62%) + +public class Solution { + public double[] internalAngles(int[] sides) { + if (sides[0] + sides[1] <= sides[2] + || sides[1] + sides[2] <= sides[0] + || sides[0] + sides[2] <= sides[1]) { + return new double[0]; + } + double[] angle = new double[sides.length]; + angle[0] = + Math.toDegrees( + Math.acos( + (double) + (sides[1] * sides[1] + + sides[2] * sides[2] + - sides[0] * sides[0]) + / (2 * sides[1] * sides[2]))); + angle[1] = + Math.toDegrees( + Math.acos( + (double) + (sides[0] * sides[0] + + sides[2] * sides[2] + - sides[1] * sides[1]) + / (2 * sides[0] * sides[2]))); + angle[2] = + Math.toDegrees( + Math.acos( + (double) + (sides[1] * sides[1] + + sides[0] * sides[0] + - sides[2] * sides[2]) + / (2 * sides[1] * sides[0]))); + double max = angle[0]; + double mid = 0; + double min = 0; + for (int i = 1; i < angle.length; i++) { + if (angle[i] > max) { + min = mid; + mid = max; + max = angle[i]; + } else { + if (angle[i] > mid) { + min = mid; + mid = angle[i]; + } else { + min = angle[i]; + } + } + } + angle[0] = min; + angle[1] = mid; + angle[2] = max; + return angle; + } +} diff --git a/src/main/java/g3801_3900/s3899_angles_of_a_triangle/readme.md b/src/main/java/g3801_3900/s3899_angles_of_a_triangle/readme.md new file mode 100644 index 000000000..e910b38f4 --- /dev/null +++ b/src/main/java/g3801_3900/s3899_angles_of_a_triangle/readme.md @@ -0,0 +1,36 @@ +3899\. Angles of a Triangle + +Medium + +You are given a positive integer array `sides` of length 3. + +Determine if there exists a triangle with **positive** area whose three side lengths are given by the elements of `sides`. + +If such a triangle exists, return an array of three floating-point numbers representing its internal angles (in **degrees**), **sorted** in **non-decreasing** order. Otherwise, return an empty array. + +Answers within 10-5 of the actual answer will be accepted. + +**Example 1:** + +**Input:** sides = [3,4,5] + +**Output:** [36.86990,53.13010,90.00000] + +**Explanation:** + +You can form a right-angled triangle with side lengths 3, 4, and 5. The internal angles of this triangle are approximately 36.869897646, 53.130102354, and 90 degrees respectively. + +**Example 2:** + +**Input:** sides = [2,4,2] + +**Output:** [] + +**Explanation:** + +You cannot form a triangle with positive area using side lengths 2, 4, and 2. + +**Constraints:** + +* `sides.length == 3` +* `1 <= sides[i] <= 1000` \ No newline at end of file diff --git a/src/main/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/Solution.java b/src/main/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/Solution.java new file mode 100644 index 000000000..270ac6b5a --- /dev/null +++ b/src/main/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/Solution.java @@ -0,0 +1,47 @@ +package g3801_3900.s3900_longest_balanced_substring_after_one_swap; + +// #Medium #String #Hash_Table #Prefix_Sum #Staff #Weekly_Contest_497 +// #2026_09_17_Time_15_ms_(100.00%)_Space_48.30_MB_(97.14%) + +import java.util.Arrays; + +public class Solution { + public int longestBalanced(String s) { + char[] arr = s.toCharArray(); + int n = arr.length; + int bal = n + 1; + int ans = 0; + int[] nextIndex = new int[n]; + int[] balIndex = new int[2 * n + 3]; + Arrays.fill(balIndex, n + 1); + for (int i = n - 1; i >= 0; i--) { + bal += (('0' ^ arr[i]) << 1) - 1; + nextIndex[i] = balIndex[bal]; + balIndex[bal] = i; + } + if (bal == n + 1) { + return n; + } + int zeros = (2 * n + 1 - bal) / 2; + int maxLength = 2 * Math.min(zeros, n - zeros); + for (int i = 1; i <= n && ans < maxLength; i++) { + bal += (('1' ^ arr[i - 1]) << 1) - 1; + if (i - balIndex[bal] > ans) { + ans = i - balIndex[bal]; + } + if (balIndex[bal - 2] < i - maxLength) { + balIndex[bal - 2] = nextIndex[balIndex[bal - 2]]; + } + if (i - balIndex[bal - 2] > ans) { + ans = i - balIndex[bal - 2]; + } + if (balIndex[bal + 2] < i - maxLength) { + balIndex[bal + 2] = nextIndex[balIndex[bal + 2]]; + } + if (i - balIndex[bal + 2] > ans) { + ans = i - balIndex[bal + 2]; + } + } + return ans; + } +} diff --git a/src/main/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/readme.md b/src/main/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/readme.md new file mode 100644 index 000000000..f6571d645 --- /dev/null +++ b/src/main/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/readme.md @@ -0,0 +1,38 @@ +3900\. Longest Balanced Substring After One Swap + +Medium + +You are given a binary string `s` consisting only of characters `'0'` and `'1'`. + +A string is **balanced** if it contains an **equal** number of `'0'`s and `'1'`s. + +You can perform **at most one** swap between any two characters in `s`. Then, you select a **balanced** substring from `s`. + +Return an integer representing the **maximum** length of the **balanced** substring you can select. + +**Example 1:** + +**Input:** s = "100001" + +**Output:** 4 + +**Explanation:** + +* Swap "10**0**00**1**". The string becomes `"101000"`. +* Select the substring "**1010**00", which is balanced because it has two `'0'`s and two `'1'`s. + +**Example 2:** + +**Input:** s = "111" + +**Output:** 0 + +**Explanation:** + +* Choose not to perform any swaps. +* Select the empty substring, which is balanced because it has zero `'0'`s and zero `'1'`s. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists only of the characters `'0'` and `'1'`. \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3901_good_subsequence_queries/Solution.java b/src/main/java/g3901_4000/s3901_good_subsequence_queries/Solution.java new file mode 100644 index 000000000..70085b29b --- /dev/null +++ b/src/main/java/g3901_4000/s3901_good_subsequence_queries/Solution.java @@ -0,0 +1,103 @@ +package g3901_4000.s3901_good_subsequence_queries; + +// #Hard #Array #Math #Segment_Tree #Number_Theory #Weekly_Contest_497 #Principal +// #2026_09_17_Time_20_ms_(100.00%)_Space_133.27_MB_(34.15%) + +public class Solution { + private int[] tree; + private int validCount = 0; + + public int countGoodSubseq(int[] nums, int p, int[][] queries) { + int n = nums.length; + // 1. Iterative Segment Tree only needs 2n space + tree = new int[2 * n]; + // Build the leaves of the tree and get initial count + for (int i = 0; i < n; i++) { + if (nums[i] % p == 0) { + tree[n + i] = nums[i]; + validCount++; + } + } + // Build the internal nodes bottom-up + for (int i = n - 1; i > 0; --i) { + // tree[i << 1] is the left child, tree[i << 1 | 1] is the right child + tree[i] = gcd(tree[i << 1], tree[i << 1 | 1]); + } + int ans = 0; + for (int[] q : queries) { + int idx = q[0]; + int value = q[1]; + // 2. O(1) tracking for validCount (No segment tree needed for this!) + boolean wasValid = (nums[idx] % p == 0); + boolean isValid = (value % p == 0); + if (wasValid && !isValid) { + validCount--; + } + if (!wasValid && isValid) { + validCount++; + } + + // Update the original array to keep track of the old values + nums[idx] = value; + // Point update for the Iterative Tree + tree[idx + n] = isValid ? value : 0; + // Climb up the tree using bitwise shifts (i >>= 1 means i = i / 2) + for (int i = idx + n; i > 1; i >>= 1) { + tree[i >> 1] = gcd(tree[i], tree[i ^ 1]); + } + // 3. The logic check (tree[1] is ALWAYS the root in an iterative tree) + if (tree[1] == p) { + if (validCount < n) { + ans++; + } else { + // validCount == n (Every element is a multiple of p) + if (n > 20) { + // THE O(1) MATH BYPASS! + ans++; + } else { + // Only run this heavy check if n is 20 or smaller + boolean flag = false; + for (int i = 0; i < n; i++) { + int leftGcd = query(0, i - 1, n); + int rightGcd = query(i + 1, n - 1, n); + if (gcd(leftGcd, rightGcd) == p) { + flag = true; + break; + } + } + if (flag) { + ans++; + } + } + } + } + } + return ans; + } + + // Iterative Range Query [l, r] inclusive + private int query(int l, int r, int n) { + if (l > r) { + return 0; + } + int res = 0; + for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) { + if ((l & 1) == 1) { + res = gcd(res, tree[l++]); + } + if ((r & 1) == 1) { + res = gcd(res, tree[--r]); + } + } + return res; + } + + private int gcd(int a, int b) { + while (b > 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } +} diff --git a/src/main/java/g3901_4000/s3901_good_subsequence_queries/readme.md b/src/main/java/g3901_4000/s3901_good_subsequence_queries/readme.md new file mode 100644 index 000000000..4dee4bf19 --- /dev/null +++ b/src/main/java/g3901_4000/s3901_good_subsequence_queries/readme.md @@ -0,0 +1,74 @@ +3901\. Good Subsequence Queries + +Hard + +You are given an integer array `nums` of length `n` and an integer `p`. + +A **non-empty subsequence** of `nums` is called **good** if: + +* Its length is **strictly less** than `n`. +* The **greatest common divisor (GCD)** of its elements is **exactly** `p`. + +You are also given a 2D integer array `queries` of length `q`, where each queries[i] = [indi, vali] indicates that you should update nums[indi] to vali. + +After each query, determine whether there exists **any good subsequence** in the current array. + +Return the **number** of queries for which a **good subsequence** exists. + +The term `gcd(a, b)` denotes the **greatest common divisor** of `a` and `b`. + +**Example 1:** + +**Input:** nums = [4,8,12,16], p = 2, queries = [[0,3],[2,6]] + +**Output:** 1 + +**Explanation:** + +| i | `[ind_i, val_i]` | Operation | Updated `nums` | Any good Subsequence | +|---:|---|---|---|---| +| 0 | `[0, 3]` | Update `nums[0]` to `3` | `[3, 8, 12, 16]` | No, as no subsequence has GCD exactly `p = 2` | +| 1 | `[2, 6]` | Update `nums[2]` to `6` | `[3, 8, 6, 16]` | Yes, subsequence `[8, 6]` has GCD exactly `p = 2` | + + +Thus, the answer is 1. + +**Example 2:** + +**Input:** nums = [4,5,7,8], p = 3, queries = [[0,6],[1,9],[2,3]] + +**Output:** 2 + +**Explanation:** + +| i | `[ind_i, val_i]` | Operation | Updated `nums` | Any good Subsequence | +|---:|---|---|---|---| +| 0 | `[0, 6]` | Update `nums[0]` to `6` | `[6, 5, 7, 8]` | No, as no subsequence has GCD exactly `p = 3` | +| 1 | `[1, 9]` | Update `nums[1]` to `9` | `[6, 9, 7, 8]` | Yes, subsequence `[6, 9]` has GCD exactly `p = 3` | +| 2 | `[2, 3]` | Update `nums[2]` to `3` | `[6, 9, 3, 8]` | Yes, subsequence `[6, 9, 3]` has GCD exactly `p = 3` | + +Thus, the answer is 2. + +**Example 3:** + +**Input:** nums = [5,7,9], p = 2, queries = [[1,4],[2,8]] + +**Output:** 0 + +**Explanation:** + +| i | `[ind_i, val_i]` | Operation | Updated `nums` | Any good Subsequence | +|---:|---|---|---|---| +| 0 | `[1, 4]` | Update `nums[1]` to `4` | `[5, 4, 9]` | No, as no subsequence has GCD exactly `p = 2` | +| 1 | `[2, 8]` | Update `nums[2]` to `8` | `[5, 4, 8]` | No, as no subsequence has GCD exactly `p = 2` | + +Thus, the answer is 0. + +**Constraints:** + +* 2 <= n == nums.length <= 5 * 104 +* 1 <= nums[i] <= 5 * 104 +* 1 <= queries.length <= 5 * 104 +* queries[i] = [indi, vali] +* 1 <= vali, p <= 5 * 104 +* 0 <= indi <= n - 1 \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3903_smallest_stable_index_i/Solution.java b/src/main/java/g3901_4000/s3903_smallest_stable_index_i/Solution.java new file mode 100644 index 000000000..0bebfeb28 --- /dev/null +++ b/src/main/java/g3901_4000/s3903_smallest_stable_index_i/Solution.java @@ -0,0 +1,28 @@ +package g3901_4000.s3903_smallest_stable_index_i; + +// #Easy #Array #Prefix_Sum #Mid_Level #Weekly_Contest_498 +// #2026_09_17_Time_1_ms_(99.37%)_Space_46.08_MB_(92.83%) + +public class Solution { + public int firstStableIndex(int[] nums, int k) { + int n = nums.length; + int[] mini = new int[n]; + int mint = Integer.MAX_VALUE; + for (int i = n - 1; i >= 0; i--) { + if (nums[i] < mint) { + mint = nums[i]; + } + mini[i] = mint; + } + int maxt = 0; + for (int i = 0; i < n; i++) { + if (nums[i] > maxt) { + maxt = nums[i]; + } + if (maxt - mini[i] <= k) { + return i; + } + } + return -1; + } +} diff --git a/src/main/java/g3901_4000/s3903_smallest_stable_index_i/readme.md b/src/main/java/g3901_4000/s3903_smallest_stable_index_i/readme.md new file mode 100644 index 000000000..54af4679e --- /dev/null +++ b/src/main/java/g3901_4000/s3903_smallest_stable_index_i/readme.md @@ -0,0 +1,59 @@ +3903\. Smallest Stable Index I + +Easy + +You are given an integer array `nums` of length `n` and an integer `k`. + +For each index `i`, define its **instability score** as `max(nums[0..i]) - min(nums[i..n - 1])`. + +In other words: + +* `max(nums[0..i])` is the **largest** value among the elements from index 0 to index `i`. +* `min(nums[i..n - 1])` is the **smallest** value among the elements from index `i` to index `n - 1`. + +An index `i` is called **stable** if its instability score is **less than or equal to** `k`. + +Return the **smallest** stable index. If no such index exists, return -1. + +**Example 1:** + +**Input:** nums = [5,0,1,4], k = 3 + +**Output:** 3 + +**Explanation:** + +* At index 0: The maximum in `[5]` is 5, and the minimum in `[5, 0, 1, 4]` is 0, so the instability score is `5 - 0 = 5`. +* At index 1: The maximum in `[5, 0]` is 5, and the minimum in `[0, 1, 4]` is 0, so the instability score is `5 - 0 = 5`. +* At index 2: The maximum in `[5, 0, 1]` is 5, and the minimum in `[1, 4]` is 1, so the instability score is `5 - 1 = 4`. +* At index 3: The maximum in `[5, 0, 1, 4]` is 5, and the minimum in `[4]` is 4, so the instability score is `5 - 4 = 1`. +* This is the first index with an instability score less than or equal to `k = 3`. Thus, the answer is 3. + +**Example 2:** + +**Input:** nums = [3,2,1], k = 1 + +**Output:** \-1 + +**Explanation:** + +* At index 0, the instability score is `3 - 1 = 2`. +* At index 1, the instability score is `3 - 1 = 2`. +* At index 2, the instability score is `3 - 1 = 2`. +* None of these values is less than or equal to `k = 1`, so the answer is -1. + +**Example 3:** + +**Input:** nums = [0], k = 0 + +**Output:** 0 + +**Explanation:** + +At index 0, the instability score is `0 - 0 = 0`, which is less than or equal to `k = 0`. Therefore, the answer is 0. + +**Constraints:** + +* `1 <= nums.length <= 100` +* 0 <= nums[i] <= 109 +* 0 <= k <= 109 \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3904_smallest_stable_index_ii/Solution.java b/src/main/java/g3901_4000/s3904_smallest_stable_index_ii/Solution.java new file mode 100644 index 000000000..7fbcec535 --- /dev/null +++ b/src/main/java/g3901_4000/s3904_smallest_stable_index_ii/Solution.java @@ -0,0 +1,28 @@ +package g3901_4000.s3904_smallest_stable_index_ii; + +// #Medium #Array #Prefix_Sum #Senior #Weekly_Contest_498 +// #2026_09_17_Time_3_ms_(92.71%)_Space_133.24_MB_(29.87%) + +public class Solution { + public int firstStableIndex(int[] nums, int k) { + int n = nums.length; + int[] mini = new int[n]; + int mint = Integer.MAX_VALUE; + for (int i = n - 1; i >= 0; i--) { + if (nums[i] < mint) { + mint = nums[i]; + } + mini[i] = mint; + } + int maxt = 0; + for (int i = 0; i < n; i++) { + if (nums[i] > maxt) { + maxt = nums[i]; + } + if (maxt - mini[i] <= k) { + return i; + } + } + return -1; + } +} diff --git a/src/main/java/g3901_4000/s3904_smallest_stable_index_ii/readme.md b/src/main/java/g3901_4000/s3904_smallest_stable_index_ii/readme.md new file mode 100644 index 000000000..f7ec8a14c --- /dev/null +++ b/src/main/java/g3901_4000/s3904_smallest_stable_index_ii/readme.md @@ -0,0 +1,59 @@ +3904\. Smallest Stable Index II + +Medium + +You are given an integer array `nums` of length `n` and an integer `k`. + +For each index `i`, define its **instability score** as `max(nums[0..i]) - min(nums[i..n - 1])`. + +In other words: + +* `max(nums[0..i])` is the **largest** value among the elements from index 0 to index `i`. +* `min(nums[i..n - 1])` is the **smallest** value among the elements from index `i` to index `n - 1`. + +An index `i` is called **stable** if its instability score is **less than or equal to** `k`. + +Return the **smallest** stable index. If no such index exists, return -1. + +**Example 1:** + +**Input:** nums = [5,0,1,4], k = 3 + +**Output:** 3 + +**Explanation:** + +* At index 0: The maximum in `[5]` is 5, and the minimum in `[5, 0, 1, 4]` is 0, so the instability score is `5 - 0 = 5`. +* At index 1: The maximum in `[5, 0]` is 5, and the minimum in `[0, 1, 4]` is 0, so the instability score is `5 - 0 = 5`. +* At index 2: The maximum in `[5, 0, 1]` is 5, and the minimum in `[1, 4]` is 1, so the instability score is `5 - 1 = 4`. +* At index 3: The maximum in `[5, 0, 1, 4]` is 5, and the minimum in `[4]` is 4, so the instability score is `5 - 4 = 1`. +* This is the first index with an instability score less than or equal to `k = 3`. Thus, the answer is 3. + +**Example 2:** + +**Input:** nums = [3,2,1], k = 1 + +**Output:** \-1 + +**Explanation:** + +* At index 0, the instability score is `3 - 1 = 2`. +* At index 1, the instability score is `3 - 1 = 2`. +* At index 2, the instability score is `3 - 1 = 2`. +* None of these values is less than or equal to `k = 1`, so the answer is -1. + +**Example 3:** + +**Input:** nums = [0], k = 0 + +**Output:** 0 + +**Explanation:** + +At index 0, the instability score is `0 - 0 = 0`, which is less than or equal to `k = 0`. Therefore, the answer is 0. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 109 +* 0 <= k <= 109 \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3905_multi_source_flood_fill/Solution.java b/src/main/java/g3901_4000/s3905_multi_source_flood_fill/Solution.java new file mode 100644 index 000000000..d2c0e9624 --- /dev/null +++ b/src/main/java/g3901_4000/s3905_multi_source_flood_fill/Solution.java @@ -0,0 +1,50 @@ +package g3901_4000.s3905_multi_source_flood_fill; + +// #Medium #Array #Matrix #Staff #Weekly_Contest_498 #Breadth_First_Search +// #2026_09_17_Time_33_ms_(100.00%)_Space_90.55_MB_(100.00%) + +import java.util.Arrays; + +public class Solution { + public int[][] colorGrid(int n, int m, int[][] sources) { + int[][] grid = new int[n][m]; + int lenqavirod = n * m; + int[] dist = new int[lenqavirod]; + Arrays.fill(dist, Integer.MAX_VALUE); + int[] q = new int[lenqavirod]; + int head = 0; + int tail = 0; + for (int[] src : sources) { + int r = src[0]; + int c = src[1]; + int color = src[2]; + grid[r][c] = color; + dist[r * m + c] = 0; + q[tail++] = r * m + c; + } + int[] dr = {-1, 1, 0, 0}; + int[] dc = {0, 0, -1, 1}; + while (head < tail) { + int curr = q[head++]; + int r = curr / m; + int c = curr % m; + int d = dist[curr]; + int color = grid[r][c]; + for (int i = 0; i < 4; i++) { + int nr = r + dr[i]; + int nc = c + dc[i]; + if (nr >= 0 && nr < n && nc >= 0 && nc < m) { + int nextIdx = nr * m + nc; + if (dist[nextIdx] > d + 1) { + dist[nextIdx] = d + 1; + grid[nr][nc] = color; + q[tail++] = nextIdx; + } else if (dist[nextIdx] == d + 1 && (color > grid[nr][nc])) { + grid[nr][nc] = color; + } + } + } + } + return grid; + } +} diff --git a/src/main/java/g3901_4000/s3905_multi_source_flood_fill/readme.md b/src/main/java/g3901_4000/s3905_multi_source_flood_fill/readme.md new file mode 100644 index 000000000..8583330ab --- /dev/null +++ b/src/main/java/g3901_4000/s3905_multi_source_flood_fill/readme.md @@ -0,0 +1,66 @@ +3905\. Multi Source Flood Fill + +Medium + +You are given two integers `n` and `m` representing the number of rows and columns of a grid, respectively. + +You are also given a 2D integer array `sources`, where sources[i] = [ri, ci, colori] indicates that the cell (ri, ci) is initially colored with colori. All other cells are initially uncolored and represented as 0. + +At each time step, every currently colored cell spreads its color to all adjacent **uncolored** cells in the four directions: up, down, left, and right. All spreads happen simultaneously. + +If **multiple** colors reach the same uncolored cell at the same time step, the cell takes the color with the **maximum** value. + +The process continues until no more cells can be colored. + +Return a 2D integer array representing the final state of the grid, where each cell contains its final color. + +**Example 1:** + +**Input:** n = 3, m = 3, sources = [[0,0,1],[2,2,2]] + +**Output:** [[1,1,2],[1,2,2],[2,2,2]] + +**Explanation:** + +The grid at each time step is as follows: + +![](https://assets.leetcode.com/uploads/2026/03/29/g50new.png) + +At time step 2, cells `(0, 2)`, `(1, 1)`, and `(2, 0)` are reached by both colors, so they are assigned color 2 as it has the maximum value among them. + +**Example 2:** + +**Input:** n = 3, m = 3, sources = [[0,1,3],[1,1,5]] + +**Output:** [[3,3,3],[5,5,5],[5,5,5]] + +**Explanation:** + +The grid at each time step is as follows: + +![](https://assets.leetcode.com/uploads/2026/03/29/g51new.png) + +**Example 3:** + +**Input:** n = 2, m = 2, sources = [[1,1,5]] + +**Output:** [[5,5],[5,5]] + +**Explanation:** + +The grid at each time step is as follows: + +![](https://assets.leetcode.com/uploads/2026/03/29/g52new.png) + +Since there is only one source, all cells are assigned the same color. + +**Constraints:** + +* 1 <= n, m <= 105 +* 1 <= n * m <= 105 +* `1 <= sources.length <= n * m` +* sources[i] = [ri, ci, colori] +* 0 <= ri <= n - 1 +* 0 <= ci <= m - 1 +* 1 <= colori <= 106 +* All (ri, ci) in `sources` are distinct. \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/Solution.java b/src/main/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/Solution.java new file mode 100644 index 000000000..a32922f68 --- /dev/null +++ b/src/main/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/Solution.java @@ -0,0 +1,69 @@ +package g3901_4000.s3906_count_good_integers_on_a_grid_path; + +// #Hard #Dynamic_Programming #Senior_Staff #Weekly_Contest_498 +// #2026_09_17_Time_7_ms_(93.94%)_Space_46.60_MB_(72.73%) + +import java.util.Arrays; + +public class Solution { + private StringBuilder a; + private StringBuilder b; + private boolean[] arr; + private final long[][] dp = new long[16][11]; + + private long rec(int idx, int tl, int tu, int prev) { + int n = 16; + if (idx == n) { + return 1; + } + if (tl == 0 && tu == 0 && dp[idx][prev] != -1) { + return dp[idx][prev]; + } + long res = 0; + int lb = (tl == 1) ? a.charAt(idx) - '0' : 0; + int ub = (tu == 1) ? b.charAt(idx) - '0' : 9; + for (int digit = lb; digit <= ub; digit += 1) { + int ntl = (tl == 1 && digit == lb) ? 1 : 0; + int ntu = (tu == 1 && digit == ub) ? 1 : 0; + if (arr[idx] || prev == 10) { + if (prev == 10 || digit >= prev) { + res += rec(idx + 1, ntl, ntu, digit); + } + } else { + res += rec(idx + 1, ntl, ntu, prev); + } + } + if (tl == 0 && tu == 0) { + dp[idx][prev] = res; + } + return res; + } + + public long countGoodIntegersOnPath(long l, long r, String s) { + a = new StringBuilder(String.valueOf(l)); + b = new StringBuilder(String.valueOf(r)); + while (b.length() < 16) { + b.insert(0, '0'); + } + while (a.length() < 16) { + a.insert(0, '0'); + } + arr = new boolean[16]; + arr[0] = true; + int i = 0; + int j = 0; + for (int k = 0; k < 6; k++) { + char c = s.charAt(k); + if (c == 'D') { + i += 1; + } else { + j += 1; + } + arr[(i * 4) + j] = true; + } + for (long[] x : dp) { + Arrays.fill(x, -1); + } + return rec(0, 1, 1, 10); + } +} diff --git a/src/main/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/readme.md b/src/main/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/readme.md new file mode 100644 index 000000000..916b11c9d --- /dev/null +++ b/src/main/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/readme.md @@ -0,0 +1,126 @@ +3906\. Count Good Integers on a Grid Path + +Hard + +You are given two integers `l` and `r`, and a string `directions` consisting of **exactly** three `'D'` characters and three `'R'` characters. + +For each integer `x` in the range `[l, r]` (inclusive), perform the following steps: + +1. If `x` has fewer than 16 digits, pad it on the left with **leading zeros** to obtain a 16-digit string. +2. Place the 16 digits into a `4 × 4` grid in **row-major** order (the first 4 digits form the first row from left to right, the next 4 digits form the second row, and so on). +3. Starting at the **top-left** cell (`row = 0`, `column = 0`), apply the 6 characters of `directions` in order: + * `'D'` increments the row by 1. + * `'R'` increments the column by 1. +4. Record the sequence of digits visited along the path (including the starting cell), producing a sequence of length 7. + +The integer `x` is considered **good** if the recorded sequence is **non-decreasing**. + +Return an integer representing the number of good integers in the range `[l, r]`. + +**Example 1:** + +**Input:** l = 8, r = 10, directions = "DDDRRR" + +**Output:** 2 + +**Explanation:** + +The grid for `x = 8`: + +| | | | | +|---|---|---|---| +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 8 | + +* Path: `(0,0) → (1,0) → (2,0) → (3,0) → (3,1) → (3,2) → (3,3)` +* The sequence of digits visited is `[0, 0, 0, 0, 0, 0, 8]`. +* As the sequence of digits visited is non-decreasing, 8 is a good integer. + +The grid for `x = 9`: + +| | | | | +|---|---|---|---| +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 9 | + +* The sequence of digits visited is `[0, 0, 0, 0, 0, 0, 9]`. +* As the sequence of digits visited is non-decreasing, 9 is a good integer. + +The grid for `x = 10`: + +| | | | | +|---|---|---|---| +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 0 | +| 0 | 0 | 1 | 0 | + +* The sequence of digits visited is `[0, 0, 0, 0, 0, 1, 0]`. +* As the sequence of digits visited is not non-decreasing, 10 is not a good integer. +* Hence, only 8 and 9 are good, giving a total of 2 good integers in the range. + +**Example 2:** + +**Input:** l = 123456789, r = 123456790, directions = "DDRRDR" + +**Output:** 1 + +**Explanation:** + +The grid for `x = 123456789`: + +| | | | | +|---|---|---|---| +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 1 | +| 2 | 3 | 4 | 5 | +| 6 | 7 | 8 | 9 | + +* Path: `(0,0) → (1,0) → (2,0) → (2,1) → (2,2) → (3,2) → (3,3)` +* The sequence of digits visited is `[0, 0, 2, 3, 4, 8, 9]`. +* As the sequence of digits visited is non-decreasing, 123456789 is a good integer. + +The grid for `x = 123456790`: + +| | | | | +|---|---|---|---| +| 0 | 0 | 0 | 0 | +| 0 | 0 | 0 | 1 | +| 2 | 3 | 4 | 5 | +| 6 | 7 | 9 | 0 | + +* The sequence of digits visited is `[0, 0, 2, 3, 4, 9, 0]`. +* As the sequence of digits visited is not non-decreasing, 123456790 is not a good integer. +* Hence, only 123456789 is good, giving a total of 1 good integer in the range. + +**Example 3:** + +**Input:** l = 1288561398769758, r = 1288561398769758, directions = "RRRDDD" + +**Output:** 0 + +**Explanation:** + +The grid for `x = 1288561398769758`: + +| | | | | +|---|---|---|---| +| 1 | 2 | 8 | 8 | +| 5 | 6 | 1 | 3 | +| 9 | 8 | 7 | 6 | +| 9 | 7 | 5 | 8 | + +* Path: `(0,0) → (0,1) → (0,2) → (0,3) → (1,3) → (2,3) → (3,3)` +* The sequence of digits visited is `[1, 2, 8, 8, 3, 6, 8]`. +* As the sequence of digits visited is not non-decreasing, 1288561398769758 is not a good integer. +* No numbers are good, giving a total of 0 good integers in the range. + +**Constraints:** + +* 1 <= l <= r <= 9 × 1015 +* `directions.length == 6` +* `directions` consists of **exactly** three `'D'` characters and three `'R'` characters. \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3908_valid_digit_number/Solution.java b/src/main/java/g3901_4000/s3908_valid_digit_number/Solution.java new file mode 100644 index 000000000..5e43f708b --- /dev/null +++ b/src/main/java/g3901_4000/s3908_valid_digit_number/Solution.java @@ -0,0 +1,21 @@ +package g3901_4000.s3908_valid_digit_number; + +// #Easy #Math #Mid_Level #Biweekly_Contest_181 +// #2026_09_17_Time_1_ms_(98.66%)_Space_42.77_MB_(28.00%) + +public class Solution { + public boolean validDigit(int n, int x) { + boolean value = false; + while (n > 0) { + int rem = n % 10; + n /= 10; + if (rem == x) { + value = true; + } + if (n == 0 && rem == x) { + value = false; + } + } + return value; + } +} diff --git a/src/main/java/g3901_4000/s3908_valid_digit_number/readme.md b/src/main/java/g3901_4000/s3908_valid_digit_number/readme.md new file mode 100644 index 000000000..a71feecfb --- /dev/null +++ b/src/main/java/g3901_4000/s3908_valid_digit_number/readme.md @@ -0,0 +1,47 @@ +3908\. Valid Digit Number + +Easy + +You are given an integer `n` and a digit `x`. + +A number is considered **valid** if: + +* It contains **at least one** occurrence of digit `x`, and +* It **does not start** with digit `x`. + +Return `true` if `n` is **valid**, otherwise return `false`. + +**Example 1:** + +**Input:** n = 101, x = 0 + +**Output:** true + +**Explanation:** + +The number contains digit 0 at index 1. It does not start with 0, so it satisfies both conditions. Thus, the answer is `true`. + +**Example 2:** + +**Input:** n = 232, x = 2 + +**Output:** false + +**Explanation:** + +The number starts with 2, which violates the condition. Thus, the answer is `false`. + +**Example 3:** + +**Input:** n = 5, x = 1 + +**Output:** false + +**Explanation:** + +The number does not contain digit 1. Thus, the answer is `false`. + +**Constraints:** + +* 0 <= n <= 105 +* `0 <= x <= 9` \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/Solution.java b/src/main/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/Solution.java new file mode 100644 index 000000000..cf1a8c4e8 --- /dev/null +++ b/src/main/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/Solution.java @@ -0,0 +1,25 @@ +package g3901_4000.s3909_compare_sums_of_bitonic_parts; + +// #Medium #Array #Senior #Biweekly_Contest_181 +// #2026_09_17_Time_1_ms_(100.00%)_Space_104.27_MB_(31.52%) + +public class Solution { + public int compareBitonicSums(int[] nums) { + long asc = 0; + long desc = 0; + int i; + for (i = 0; i < nums.length - 1; i++) { + asc += nums[i]; + if (nums[i] > nums[i + 1]) { + break; + } + } + for (; i < nums.length; i++) { + desc += nums[i]; + } + if (asc == desc) { + return -1; + } + return asc > desc ? 0 : 1; + } +} diff --git a/src/main/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/readme.md b/src/main/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/readme.md new file mode 100644 index 000000000..2efa1b1d7 --- /dev/null +++ b/src/main/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/readme.md @@ -0,0 +1,69 @@ +3909\. Compare Sums of Bitonic Parts + +Medium + +You are given a **bitonic** array `nums` of length `n`. + +Split the array into **two** parts: + +* **Ascending part**: from index 0 to the peak element (inclusive). +* **Descending part**: from the peak element to index `n - 1` (inclusive). + +The peak element belongs to both parts. + +Return: + +* 0 if the sum of the **ascending** part is greater. +* 1 if the sum of the **descending** part is greater. +* \-1 if both sums are **equal**. + +**Notes**: + +* A **bitonic** array is an array that is **strictly increasing** up to a **single peak** element and then **strictly decreasing**. +* An array is said to be **strictly increasing** if each element is **strictly greater** than its **previous** one (if exists). +* An array is said to be **strictly decreasing** if each element is **strictly smaller** than its **previous** one (if exists). + +**Example 1:** + +**Input:** nums = [1,3,2,1] + +**Output:** 1 + +**Explanation:** + +* Peak element is `nums[1] = 3` +* Ascending part = `[1, 3]`, sum is `1 + 3 = 4` +* Descending part = `[3, 2, 1]`, sum is `3 + 2 + 1 = 6` +* Since the descending part has a larger sum, return 1. + +**Example 2:** + +**Input:** nums = [2,4,5,2] + +**Output:** 0 + +**Explanation:** + +* Peak element is `nums[2] = 5` +* Ascending part = `[2, 4, 5]`, sum is `2 + 4 + 5 = 11` +* Descending part = `[5, 2]`, sum is `5 + 2 = 7` +* Since the ascending part has a larger sum, return 0. + +**Example 3:** + +**Input:** nums = [1,2,4,3] + +**Output:** \-1 + +**Explanation:** + +* Peak element is `nums[2] = 4` +* Ascending part = `[1, 2, 4]`, sum is `1 + 2 + 4 = 7` +* Descending part = `[4, 3]`, sum is `4 + 3 = 7` +* Since both parts have equal sums, return -1. + +**Constraints:** + +* 3 <= n == nums.length <= 105 +* 1 <= nums[i] <= 109 +* `nums` is a bitonic array. \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/Solution.java b/src/main/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/Solution.java new file mode 100644 index 000000000..6b643ac23 --- /dev/null +++ b/src/main/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/Solution.java @@ -0,0 +1,65 @@ +package g3901_4000.s3910_count_connected_subgraphs_with_even_node_sum; + +// #Hard #Array #Bit_Manipulation #Enumeration #Senior_Staff #Breadth_First_Search +// #Biweekly_Contest_181 #Depth_First_Search #Union_Find #Graph_Theory +// #2026_09_17_Time_3_ms_(98.31%)_Space_46.18_MB_(100.00%) + +public class Solution { + private long[] graph; + private int[] nums; + private int validCount; + + public int evenSumSubgraphs(int[] nums, int[][] edges) { + this.nums = nums; + int nodeCount = nums.length; + this.graph = new long[nodeCount]; + this.validCount = 0; + buildGraph(edges); + for (int root = 0; root < nodeCount; root++) { + long rootMask = 1L << root; + long allowedMask = -(1L << root); + long candidateMask = graph[root] & allowedMask; + search(rootMask, candidateMask, 0L, nums[root] & 1, allowedMask); + } + return validCount; + } + + private void buildGraph(int[][] edgeList) { + for (int[] edge : edgeList) { + int firstNode = edge[0]; + int secondNode = edge[1]; + graph[firstNode] |= 1L << secondNode; + graph[secondNode] |= 1L << firstNode; + } + } + + private void search( + long selectedMask, + long candidateMask, + long excludedMask, + int parity, + long allowedMask) { + if (parity == 0) { + validCount++; + } + while (candidateMask != 0) { + long currentBit = candidateMask & -candidateMask; + int currentNode = Long.numberOfTrailingZeros(currentBit); + candidateMask ^= currentBit; + long nextSelectedMask = selectedMask | currentBit; + long nextCandidateMask = + candidateMask + | (graph[currentNode] + & allowedMask + & ~nextSelectedMask + & ~excludedMask); + search( + nextSelectedMask, + nextCandidateMask, + excludedMask, + parity ^ (nums[currentNode] & 1), + allowedMask); + excludedMask |= currentBit; + } + } +} diff --git a/src/main/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/readme.md b/src/main/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/readme.md new file mode 100644 index 000000000..6bc1d78ba --- /dev/null +++ b/src/main/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/readme.md @@ -0,0 +1,54 @@ +3910\. Count Connected Subgraphs with Even Node Sum + +Hard + +You are given an undirected graph with `n` nodes labeled from 0 to `n - 1`. Node `i` has a **value** of `nums[i]`, which is either 0 or 1. The edges of the graph are given by a 2D array `edges` where edges[i] = [ui, vi] represents an edge between node ui and node vi. + +For a **non-empty subset** `s` of nodes in the graph, we consider the **induced subgraph** of `s` generated as follows: + +* We keep only the nodes in `s`. +* We keep only the edges whose two endpoints are both in `s`. + +Return an integer representing the number of **non-empty** subsets `s` of nodes in the graph such that: + +* The **induced subgraph** of `s` is **connected**. +* The **sum** of node **values** in `s` is **even**. + +**Example 1:** + +**Input:** nums = [1,0,1], edges = [[0,1],[1,2]] + +**Output:** 2 + +**Explanation:** + +| `s` | connected? | sum of node values | counted? | +|---|---|---:|---| +| `[0]` | Yes | 1 | No | +| `[1]` | Yes | 0 | Yes | +| `[2]` | Yes | 1 | No | +| `[0,1]` | Yes | 1 | No | +| `[0,2]` | No, node 0 and node 2 are disconnected. | 2 | No | +| `[1,2]` | Yes | 1 | No | +| `[0,1,2]` | Yes | 2 | Yes | + +**Example 2:** + +**Input:** nums = [1], edges = [] + +**Output:** 0 + +**Explanation:** + +| `s` | connected? | sum of node values | counted? | +|---|---|---:|---| +| `[0]` | Yes | 1 | No | + +**Constraints:** + +* `1 <= n == nums.length <= 13` +* `nums[i]` is 0 or 1. +* `0 <= edges.length <= n * (n - 1) / 2` +* edges[i] = [ui, vi] +* 0 <= ui < vi < n +* All edges are **distinct**. \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/Solution.java b/src/main/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/Solution.java new file mode 100644 index 000000000..6329b083a --- /dev/null +++ b/src/main/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/Solution.java @@ -0,0 +1,48 @@ +package g3901_4000.s3911_k_th_smallest_remaining_even_integer_in_subarray_queries; + +// #Hard #Array #Binary_Search #Senior_Staff #Biweekly_Contest_181 +// #2026_09_17_Time_5_ms_(100.00%)_Space_215.84_MB_(25.00%) + +public class Solution { + public int[] kthRemainingInteger(int[] nums, int[][] queries) { + int n = nums.length; + int[] ans = new int[queries.length]; + int[] prefix = new int[n + 1]; + for (int i = 1; i <= n; i++) { + prefix[i] = prefix[i - 1] + ((nums[i - 1] % 2 == 0) ? 1 : 0); + } + for (int q = 0; q < queries.length; q++) { + int l = queries[q][0]; + int r = queries[q][1]; + int k = queries[q][2]; + int lowerCnt = (nums[l] - 1) / 2; + int upperCnt = nums[r] / 2; + int remove = prefix[r + 1] - prefix[l]; + // first segment + if (lowerCnt >= k) { + ans[q] = 2 * k; + continue; + } + // third segment + if (upperCnt - remove < k) { + ans[q] = 2 * (k + remove); + } else { + // middle segment + int s = l; + int e = r; + while (s <= e) { + int m = s + (e - s) / 2; + int u = nums[m] / 2; + int rem = prefix[m + 1] - prefix[l]; + if (u - rem < k) { + s = m + 1; + } else { + e = m - 1; + } + } + ans[q] = 2 * (k + (prefix[s] - prefix[l])); + } + } + return ans; + } +} diff --git a/src/main/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/readme.md b/src/main/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/readme.md new file mode 100644 index 000000000..f70297087 --- /dev/null +++ b/src/main/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/readme.md @@ -0,0 +1,73 @@ +3911\. K-th Smallest Remaining Even Integer in Subarray Queries + +Hard + +You are given an integer array `nums` where `nums` is **strictly increasing**. + +You are also given a 2D integer array `queries`, where queries[i] = [li, ri, ki]. + +For each query [li, ri, ki]: + +* Consider the **non-empty subarrays** nums[li..ri] +* From the **infinite** sequence of all **positive even integers**: `2, 4, 6, 8, 10, 12, 14, ...` +* **Remove** all elements that appear in the **subarray** nums[li..ri]. +* Find the kith **smallest integer** remaining in the sequence after the removals. + +Return an integer array `ans`, where `ans[i]` is the result for the ith query. + +**Example 1:** + +**Input:** nums = [1,4,7], queries = [[0,2,1],[1,1,2],[0,0,3]] + +**Output:** [2,6,6] + +**Explanation:** + +| `i` | `queries[i]` | `nums[l_i..r_i]` | Removed Evens | Remaining Evens | `k_i` | `ans[i]` | +|---:|---|---|---|---|---:|---:| +| 0 | `[0, 2, 1]` | `[1, 4, 7]` | `[4]` | 2, 6, 8, ... | 1 | 2 | +| 1 | `[1, 1, 2]` | `[4]` | `[4]` | 2, 6, 8, ... | 2 | 6 | +| 2 | `[0, 0, 3]` | `[1]` | `[]` | 2, 4, 6, ... | 3 | 6 | + +Thus, `ans = [2, 6, 6]`. + +**Example 2:** + +**Input:** nums = [2,5,8], queries = [[0,1,2],[1,2,1],[0,2,4]] + +**Output:** [6,2,12] + +**Explanation:** + +| `i` | `queries[i]` | `nums[l_i..r_i]` | Removed Evens | Remaining Evens | `k_i` | `ans[i]` | +|---:|---|---|---|---|---:|---:| +| 0 | `[0, 1, 2]` | `[2, 5]` | `[2]` | 4, 6, 8, ... | 2 | 6 | +| 1 | `[1, 2, 1]` | `[5, 8]` | `[8]` | 2, 4, 6, ... | 1 | 2 | +| 2 | `[0, 2, 4]` | `[2, 5, 8]` | `[2, 8]` | 4, 6, 10, 12, ... | 4 | 12 | + +Thus, `ans = [6, 2, 12]`. + +**Example 3:** + +**Input:** nums = [3,6], queries = [[0,1,1],[1,1,3]] + +**Output:** [2,8] + +**Explanation:** + +| `i` | `queries[i]` | `nums[l_i..r_i]` | Removed Evens | Remaining Evens | `k_i` | `ans[i]` | +|---:|---|---|---|---|---:|---:| +| 0 | `[0, 1, 1]` | `[3, 6]` | `[6]` | 2, 4, 8, ... | 1 | 2 | +| 1 | `[1, 1, 3]` | `[6]` | `[6]` | 2, 4, 8, ... | 3 | 8 | + +Thus, `ans = [2, 8]`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* `nums` is strictly increasing +* 1 <= queries.length <= 105 +* queries[i] = [li, ri, ki] +* 0 <= li <= ri < nums.length +* 1 <= ki <= 109 \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3912_valid_elements_in_an_array/Solution.java b/src/main/java/g3901_4000/s3912_valid_elements_in_an_array/Solution.java new file mode 100644 index 000000000..3030f6805 --- /dev/null +++ b/src/main/java/g3901_4000/s3912_valid_elements_in_an_array/Solution.java @@ -0,0 +1,38 @@ +package g3901_4000.s3912_valid_elements_in_an_array; + +// #Easy #Array #Mid_Level #Weekly_Contest_499 +// #2026_09_17_Time_1_ms_(100.00%)_Space_46.47_MB_(82.58%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public List findValidElements(int[] nums) { + List ans = new ArrayList<>(); + int n = nums.length; + ans.add(nums[0]); + for (int i = 1; i < n - 1; i++) { + boolean left = true; + boolean right = true; + for (int j = 0; j < i; j++) { + if (nums[i] <= nums[j]) { + left = false; + break; + } + } + for (int j = i + 1; j < n; j++) { + if (nums[i] <= nums[j]) { + right = false; + break; + } + } + if (left || right) { + ans.add(nums[i]); + } + } + if (n > 1) { + ans.add(nums[n - 1]); + } + return ans; + } +} diff --git a/src/main/java/g3901_4000/s3912_valid_elements_in_an_array/readme.md b/src/main/java/g3901_4000/s3912_valid_elements_in_an_array/readme.md new file mode 100644 index 000000000..3a9c8de90 --- /dev/null +++ b/src/main/java/g3901_4000/s3912_valid_elements_in_an_array/readme.md @@ -0,0 +1,54 @@ +3912\. Valid Elements in an Array + +Easy + +You are given an integer array `nums`. + +An element `nums[i]` is considered **valid** if it satisfies **at least** one of the following conditions: + +* It is **strictly greater** than every element to its left. +* It is **strictly greater** than every element to its right. + +The first and last elements are always valid. + +Return an array of all valid elements in the same order as they appear in `nums`. + +**Example 1:** + +**Input:** nums = [1,2,4,2,3,2] + +**Output:** [1,2,4,3,2] + +**Explanation:** + +* `nums[0]` and `nums[5]` are always valid. +* `nums[1]` and `nums[2]` are strictly greater than every element to their left. +* `nums[4]` is strictly greater than every element to its right. +* Thus, the answer is `[1, 2, 4, 3, 2]`. + +**Example 2:** + +**Input:** nums = [5,5,5,5] + +**Output:** [5,5] + +**Explanation:** + +* The first and last elements are always valid. +* No other elements are strictly greater than all elements to their left or to their right. +* Thus, the answer is `[5, 5]`. + +**Example 3:** + +**Input:** nums = [1] + +**Output:** [1] + +**Explanation:** + +Since there is only one element, it is always valid. Thus, the answer is `[1]`. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3913_sort_vowels_by_frequency/Solution.java b/src/main/java/g3901_4000/s3913_sort_vowels_by_frequency/Solution.java new file mode 100644 index 000000000..ae085bc5e --- /dev/null +++ b/src/main/java/g3901_4000/s3913_sort_vowels_by_frequency/Solution.java @@ -0,0 +1,42 @@ +package g3901_4000.s3913_sort_vowels_by_frequency; + +// #Medium #String #Sorting #Counting #Senior #Weekly_Contest_499 +// #2026_09_17_Time_9_ms_(100.00%)_Space_47.32_MB_(89.74%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public String sortVowels(String s) { + int[] freq = new int[26]; + char[] ch = s.toCharArray(); + for (char c : ch) { + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + freq[c - 'a']++; + } + } + List x = new ArrayList<>(); + for (int i = 0; i < 26; i++) { + if (freq[i] > 0) { + x.add(new int[] {i, freq[i]}); + } + } + x.sort( + (a, b) -> + b[1] - a[1] == 0 + ? s.indexOf((char) (a[0] + 'a')) - s.indexOf((char) (b[0] + 'a')) + : b[1] - a[1]); + int i = 0; + for (int[] f : x) { + while (f[1] > 0) { + char c = ch[i]; + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + ch[i] = (char) (f[0] + 'a'); + f[1]--; + } + i++; + } + } + return new String(ch); + } +} diff --git a/src/main/java/g3901_4000/s3913_sort_vowels_by_frequency/readme.md b/src/main/java/g3901_4000/s3913_sort_vowels_by_frequency/readme.md new file mode 100644 index 000000000..9ee4b3e15 --- /dev/null +++ b/src/main/java/g3901_4000/s3913_sort_vowels_by_frequency/readme.md @@ -0,0 +1,53 @@ +3913\. Sort Vowels by Frequency + +Medium + +You are given a string `s` consisting of lowercase English characters. + +Rearrange only the **vowels** in the string so that they appear in **non-increasing** order of their frequency. + +If multiple vowels have the same **frequency**, order them by the position of their **first occurrence** in `s`. + +Return the modified string. + +Vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`. + +The **frequency** of a letter is the number of times it occurs in the string. + +**Example 1:** + +**Input:** s = "leetcode" + +**Output:** "leetcedo" + +**Explanation:** + +* Vowels in the string are `['e', 'e', 'o', 'e']` with frequencies: `e = 3`, `o = 1`. +* Sorting in non-increasing order of frequency and placing them back into the vowel positions results in `"leetcedo"`. + +**Example 2:** + +**Input:** s = "aeiaaioooa" + +**Output:** "aaaaoooiie" + +**Explanation:** + +* Vowels in the string are `['a', 'e', 'i', 'a', 'a', 'i', 'o', 'o', 'o', 'a']` with frequencies: `a = 4`, `o = 3`, `i = 2`, `e = 1`. +* Sorting them in non-increasing order of frequency and placing them back into the vowel positions results in `"aaaaoooiie"`. + +**Example 3:** + +**Input:** s = "baeiou" + +**Output:** "baeiou" + +**Explanation:** + +* Each vowel appears exactly once, so all have the same frequency. +* Thus, they retain their relative order based on first occurrence, and the string remains unchanged. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consists of lowercase English letters \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/Solution.java b/src/main/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/Solution.java new file mode 100644 index 000000000..0841a4c4c --- /dev/null +++ b/src/main/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/Solution.java @@ -0,0 +1,15 @@ +package g3901_4000.s3914_minimum_operations_to_make_array_non_decreasing; + +// #Medium #Array #Greedy #Staff #Weekly_Contest_499 +// #2026_09_17_Time_2_ms_(100.00%)_Space_87.58_MB_(31.66%) + +public class Solution { + public long minOperations(int[] nums) { + int n = nums.length; + long ans = 0; + for (int i = 1; i < n; ++i) { + ans += Math.max(nums[i - 1] - nums[i], 0); + } + return ans; + } +} diff --git a/src/main/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/readme.md b/src/main/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/readme.md new file mode 100644 index 000000000..677b0a7db --- /dev/null +++ b/src/main/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/readme.md @@ -0,0 +1,45 @@ +3914\. Minimum Operations to Make Array Non Decreasing + +Medium + +You are given an integer array `nums` of length `n`. + +In one operation, you may choose any **non-empty subarrays** `nums[l..r]` and **increase** each element in that **subarray** by `x`, where `x` is any **positive** integer. + +Return the **minimum** possible **sum** of the values of `x` across all operations required to make the array **non-decreasing**. + +An array is **non-decreasing** if `nums[i] <= nums[i + 1]` for all `0 <= i < n - 1`. + +**Example 1:** + +**Input:** nums = [3,3,2,1] + +**Output:** 2 + +**Explanation:** + +One optimal set of operations: + +* Choose subarray `[2..3]` and add `x = 1` resulting in `[3, 3, 3, 2]` +* Choose subarray `[3..3]` and add `x = 1` resulting in `[3, 3, 3, 3]` + +The array becomes non-decreasing, and the total sum of chosen `x` values is `1 + 1 = 2`. + +**Example 2:** + +**Input:** nums = [5,1,2,3] + +**Output:** 4 + +**Explanation:** + +One optimal set of operations: + +* Choose subarray `[1..3]` and add `x = 4` resulting in `[5, 5, 6, 7]` + +The array becomes non-decreasing, and the total sum of chosen `x` values is `4`. + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/Solution.java b/src/main/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/Solution.java new file mode 100644 index 000000000..08ebef23f --- /dev/null +++ b/src/main/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/Solution.java @@ -0,0 +1,77 @@ +package g3901_4000.s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k; + +// #Hard #Array #Dynamic_Programming #Segment_Tree #Senior_Staff #Weekly_Contest_499 +// #2026_09_17_Time_113_ms_(100.00%)_Space_268.70_MB_(70.37%) + +public class Solution { + public long maxAlternatingSum(int[] nums, int k) { + int n = nums.length; + int maxVal = 0; + // Find the maximum value in nums to define the size of our data structures + for (int x : nums) { + if (x > maxVal) { + maxVal = x; + } + } + // Variable created as requested to store an input midway in the function + // peak[i] stores the max alternating sum ending at index i where nums[i] is a peak + long[] peak = new long[n]; + // valley[i] stores the max alternating sum ending at index i where nums[i] is a valley + long[] valley = new long[n]; + // Fenwick Trees for Range Maximum Queries + // prefixBitTree will store valley[j] values to find max valley[j] where nums[j] < nums[i] + long[] prefixBitTree = new long[maxVal + 1]; + // suffixBitTree will store peak[j] values to find max peak[j] where nums[j] > nums[i] + // We use the transformation maxVal - nums[j] + 1 to treat suffix max as prefix max + long[] suffixBitTree = new long[maxVal + 1]; + long maxScore = 0; + for (int i = 0; i < n; i++) { + // Distance condition: consecutive indices must differ by at least k + if (i >= k) { + // Activate the valid index (i - k) by updating both Fenwick trees + int valIdxPrefix = nums[i - k]; + long valPrefix = valley[i - k]; + for (int idx = valIdxPrefix; idx <= maxVal; idx += idx & -idx) { + if (valPrefix > prefixBitTree[idx]) { + prefixBitTree[idx] = valPrefix; + } + } + int valIdxSuffix = maxVal - nums[i - k] + 1; + long valSuffix = peak[i - k]; + for (int idx = valIdxSuffix; idx <= maxVal; idx += idx & -idx) { + if (valSuffix > suffixBitTree[idx]) { + suffixBitTree[idx] = valSuffix; + } + } + } + // Find max alternating sum if nums[i] is the current peak (needs previous valley < + // nums[i]) + long maxPrevValley = 0; + for (int q = nums[i] - 1; q > 0; q -= q & -q) { + if (prefixBitTree[q] > maxPrevValley) { + maxPrevValley = prefixBitTree[q]; + } + } + // A length-1 subsequence is also strictly alternating + peak[i] = nums[i] + maxPrevValley; + // Find max alternating sum if nums[i] is the current valley (needs previous peak > + // nums[i]) + long maxPrevPeak = 0; + for (int q = maxVal - nums[i]; q > 0; q -= q & -q) { + if (suffixBitTree[q] > maxPrevPeak) { + maxPrevPeak = suffixBitTree[q]; + } + } + // A length-1 subsequence is also strictly alternating + valley[i] = nums[i] + maxPrevPeak; + // Update global maximum score + if (peak[i] > maxScore) { + maxScore = peak[i]; + } + if (valley[i] > maxScore) { + maxScore = valley[i]; + } + } + return maxScore; + } +} diff --git a/src/main/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/readme.md b/src/main/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/readme.md new file mode 100644 index 000000000..661f45e65 --- /dev/null +++ b/src/main/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/readme.md @@ -0,0 +1,62 @@ +3915\. Maximum Sum of Alternating Subsequence With Distance at Least K + +Hard + +You are given an integer array `nums` of length `n` and an integer `k`. + +Pick a **subsequence** with indices 0 <= i1 < i2 < ... < im < n such that: + +* For every `1 <= t < m`, it+1 - it >= k. +* The selected values form a **strictly alternating** sequence. In other words, either: + * nums[i1] < nums[i2] > nums[i3] < ..., or + * nums[i1] > nums[i2] < nums[i3] > ... + +A **subsequence** of length 1 is also considered **strictly** alternating. The score of a **valid** subsequence is the **sum** of its selected values. + +Return an integer denoting the **maximum** possible **score** of a valid subsequence. + +**Example 1:** + +**Input:** nums = [5,4,2], k = 2 + +**Output:** 7 + +**Explanation:** + +An optimal choice is indices `[0, 2]`, which gives values `[5, 2]`. + +* The distance condition holds because `2 - 0 = 2 >= k`. +* The values are strictly alternating because `5 > 2`. + +The score is `5 + 2 = 7`. + +**Example 2:** + +**Input:** nums = [3,5,4,2,4], k = 1 + +**Output:** 14 + +**Explanation:** + +An optimal choice is indices `[0, 1, 3, 4]`, which gives values `[3, 5, 2, 4]`. + +* The distance condition holds because each pair of consecutive chosen indices differs by at least `k = 1`. +* The values are strictly alternating since `3 < 5 > 2 < 4`. + +The score is `3 + 5 + 2 + 4 = 14`. + +**Example 3:** + +**Input:** nums = [5], k = 1 + +**Output:** 5 + +**Explanation:** + +The only valid subsequence is `[5]`. A subsequence with 1 element is always strictly alternating, so the score is 5. + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 105 +* `1 <= k <= n` \ No newline at end of file diff --git a/src/test/java/g3801_3900/s3899_angles_of_a_triangle/SolutionTest.java b/src/test/java/g3801_3900/s3899_angles_of_a_triangle/SolutionTest.java new file mode 100644 index 000000000..731cb289e --- /dev/null +++ b/src/test/java/g3801_3900/s3899_angles_of_a_triangle/SolutionTest.java @@ -0,0 +1,49 @@ +package g3801_3900.s3899_angles_of_a_triangle; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void internalAngles() { + assertArrayEquals( + new double[] {36.869897646, 53.130102354, 90.0}, + new Solution().internalAngles(new int[] {3, 4, 5}), + 0.00001); + } + + @Test + void internalAngles2() { + assertArrayEquals( + new double[] {36.869897646, 53.130102354, 90.0}, + new Solution().internalAngles(new int[] {5, 3, 4}), + 0.00001); + } + + @Test + void internalAngles3() { + assertArrayEquals( + new double[] {60.0, 60.0, 60.0}, + new Solution().internalAngles(new int[] {1000, 1000, 1000}), + 0.00001); + } + + @Test + void internalAngles4() { + assertArrayEquals( + new double[0], new Solution().internalAngles(new int[] {2, 4, 2}), 0.00001); + } + + @Test + void internalAngles5() { + assertArrayEquals( + new double[0], new Solution().internalAngles(new int[] {5, 1, 2}), 0.00001); + } + + @Test + void internalAngles6() { + assertArrayEquals( + new double[0], new Solution().internalAngles(new int[] {1, 2, 5}), 0.00001); + } +} diff --git a/src/test/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/SolutionTest.java b/src/test/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/SolutionTest.java new file mode 100644 index 000000000..3ee1ec19e --- /dev/null +++ b/src/test/java/g3801_3900/s3900_longest_balanced_substring_after_one_swap/SolutionTest.java @@ -0,0 +1,43 @@ +package g3801_3900.s3900_longest_balanced_substring_after_one_swap; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void longestBalanced() { + assertThat(new Solution().longestBalanced("100001"), equalTo(4)); + } + + @Test + void longestBalanced2() { + assertThat(new Solution().longestBalanced("111"), equalTo(0)); + } + + @Test + void longestBalanced3() { + assertThat(new Solution().longestBalanced("0000"), equalTo(0)); + } + + @Test + void longestBalanced4() { + assertThat(new Solution().longestBalanced("0"), equalTo(0)); + } + + @Test + void longestBalanced5() { + assertThat(new Solution().longestBalanced("1100"), equalTo(4)); + } + + @Test + void longestBalanced6() { + assertThat(new Solution().longestBalanced("0001000"), equalTo(2)); + } + + @Test + void longestBalanced7() { + assertThat(new Solution().longestBalanced("1100000011"), equalTo(6)); + } +} diff --git a/src/test/java/g3901_4000/s3901_good_subsequence_queries/SolutionTest.java b/src/test/java/g3901_4000/s3901_good_subsequence_queries/SolutionTest.java new file mode 100644 index 000000000..8d75e05ec --- /dev/null +++ b/src/test/java/g3901_4000/s3901_good_subsequence_queries/SolutionTest.java @@ -0,0 +1,64 @@ +package g3901_4000.s3901_good_subsequence_queries; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countGoodSubseq() { + int[] nums = new int[21]; + java.util.Arrays.fill(nums, 2); + assertThat( + new Solution().countGoodSubseq(nums, 2, new int[][] {{0, 4}, {20, 3}}), equalTo(2)); + } + + @Test + void countGoodSubseq2() { + assertThat( + new Solution() + .countGoodSubseq(new int[] {4, 8, 12, 16}, 2, new int[][] {{0, 3}, {2, 6}}), + equalTo(1)); + } + + @Test + void countGoodSubseq3() { + assertThat( + new Solution() + .countGoodSubseq( + new int[] {4, 5, 7, 8}, 3, new int[][] {{0, 6}, {1, 9}, {2, 3}}), + equalTo(2)); + } + + @Test + void countGoodSubseq4() { + assertThat( + new Solution() + .countGoodSubseq(new int[] {5, 7, 9}, 2, new int[][] {{1, 4}, {2, 8}}), + equalTo(0)); + } + + @Test + void countGoodSubseq5() { + assertThat( + new Solution().countGoodSubseq(new int[] {6, 10, 15}, 1, new int[][] {{0, 6}}), + equalTo(0)); + } + + @Test + void countGoodSubseq6() { + assertThat( + new Solution() + .countGoodSubseq( + new int[] {2, 4}, 2, new int[][] {{0, 3}, {1, 2}, {1, 5}, {0, 2}}), + equalTo(2)); + } + + @Test + void countGoodSubseq7() { + assertThat( + new Solution().countGoodSubseq(new int[] {6, 10, 14}, 2, new int[][] {{0, 6}}), + equalTo(1)); + } +} diff --git a/src/test/java/g3901_4000/s3903_smallest_stable_index_i/SolutionTest.java b/src/test/java/g3901_4000/s3903_smallest_stable_index_i/SolutionTest.java new file mode 100644 index 000000000..a682ccf3c --- /dev/null +++ b/src/test/java/g3901_4000/s3903_smallest_stable_index_i/SolutionTest.java @@ -0,0 +1,39 @@ +package g3901_4000.s3903_smallest_stable_index_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void firstStableIndex() { + assertThat(new Solution().firstStableIndex(new int[] {5, 0, 1, 4}, 3), equalTo(3)); + } + + @Test + void firstStableIndex2() { + assertThat(new Solution().firstStableIndex(new int[] {3, 2, 1}, 1), equalTo(-1)); + } + + @Test + void firstStableIndex3() { + assertThat(new Solution().firstStableIndex(new int[] {0}, 0), equalTo(0)); + } + + @Test + void firstStableIndex4() { + assertThat(new Solution().firstStableIndex(new int[] {1, 2, 3}, 0), equalTo(0)); + } + + @Test + void firstStableIndex5() { + assertThat(new Solution().firstStableIndex(new int[] {5, 0, 1, 4}, 4), equalTo(2)); + } + + @Test + void firstStableIndex6() { + assertThat( + new Solution().firstStableIndex(new int[] {1000000000, 0}, 1000000000), equalTo(0)); + } +} diff --git a/src/test/java/g3901_4000/s3904_smallest_stable_index_ii/SolutionTest.java b/src/test/java/g3901_4000/s3904_smallest_stable_index_ii/SolutionTest.java new file mode 100644 index 000000000..af4b8f555 --- /dev/null +++ b/src/test/java/g3901_4000/s3904_smallest_stable_index_ii/SolutionTest.java @@ -0,0 +1,39 @@ +package g3901_4000.s3904_smallest_stable_index_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void firstStableIndex() { + assertThat(new Solution().firstStableIndex(new int[] {5, 0, 1, 4}, 3), equalTo(3)); + } + + @Test + void firstStableIndex2() { + assertThat(new Solution().firstStableIndex(new int[] {3, 2, 1}, 1), equalTo(-1)); + } + + @Test + void firstStableIndex3() { + assertThat(new Solution().firstStableIndex(new int[] {0}, 0), equalTo(0)); + } + + @Test + void firstStableIndex4() { + assertThat(new Solution().firstStableIndex(new int[] {1, 2, 3}, 0), equalTo(0)); + } + + @Test + void firstStableIndex5() { + assertThat(new Solution().firstStableIndex(new int[] {5, 0, 1, 4}, 4), equalTo(2)); + } + + @Test + void firstStableIndex6() { + assertThat( + new Solution().firstStableIndex(new int[] {1000000000, 0}, 1000000000), equalTo(0)); + } +} diff --git a/src/test/java/g3901_4000/s3905_multi_source_flood_fill/SolutionTest.java b/src/test/java/g3901_4000/s3905_multi_source_flood_fill/SolutionTest.java new file mode 100644 index 000000000..2c8011e30 --- /dev/null +++ b/src/test/java/g3901_4000/s3905_multi_source_flood_fill/SolutionTest.java @@ -0,0 +1,50 @@ +package g3901_4000.s3905_multi_source_flood_fill; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void colorGrid() { + assertThat( + new Solution().colorGrid(3, 3, new int[][] {{0, 0, 1}, {2, 2, 2}}), + equalTo(new int[][] {{1, 1, 2}, {1, 2, 2}, {2, 2, 2}})); + } + + @Test + void colorGrid2() { + assertThat( + new Solution().colorGrid(3, 3, new int[][] {{0, 1, 3}, {1, 1, 5}}), + equalTo(new int[][] {{3, 3, 3}, {5, 5, 5}, {5, 5, 5}})); + } + + @Test + void colorGrid3() { + assertThat( + new Solution().colorGrid(2, 2, new int[][] {{1, 1, 5}}), + equalTo(new int[][] {{5, 5}, {5, 5}})); + } + + @Test + void colorGrid4() { + assertThat( + new Solution().colorGrid(1, 1, new int[][] {{0, 0, 7}}), + equalTo(new int[][] {{7}})); + } + + @Test + void colorGrid5() { + assertThat( + new Solution().colorGrid(1, 5, new int[][] {{0, 0, 2}, {0, 4, 9}}), + equalTo(new int[][] {{2, 2, 9, 9, 9}})); + } + + @Test + void colorGrid6() { + assertThat( + new Solution().colorGrid(5, 1, new int[][] {{4, 0, 9}, {0, 0, 2}}), + equalTo(new int[][] {{2}, {2}, {9}, {9}, {9}})); + } +} diff --git a/src/test/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/SolutionTest.java b/src/test/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/SolutionTest.java new file mode 100644 index 000000000..fb1f4a6c1 --- /dev/null +++ b/src/test/java/g3901_4000/s3906_count_good_integers_on_a_grid_path/SolutionTest.java @@ -0,0 +1,59 @@ +package g3901_4000.s3906_count_good_integers_on_a_grid_path; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countGoodIntegersOnPath() { + assertThat(new Solution().countGoodIntegersOnPath(8L, 10L, "DDDRRR"), equalTo(2L)); + } + + @Test + void countGoodIntegersOnPath2() { + assertThat( + new Solution().countGoodIntegersOnPath(123456789L, 123456790L, "DDRRDR"), + equalTo(1L)); + } + + @Test + void countGoodIntegersOnPath3() { + assertThat( + new Solution() + .countGoodIntegersOnPath(1288561398769758L, 1288561398769758L, "RRRDDD"), + equalTo(0L)); + } + + @Test + void countGoodIntegersOnPath4() { + assertThat( + new Solution() + .countGoodIntegersOnPath(1111111111111111L, 1111111111111111L, "RDRDRD"), + equalTo(1L)); + } + + @Test + void countGoodIntegersOnPath5() { + assertThat(new Solution().countGoodIntegersOnPath(1L, 9L, "DRDRDR"), equalTo(9L)); + } + + @Test + void countGoodIntegersOnPath6() { + assertThat(new Solution().countGoodIntegersOnPath(1L, 99L, "RRRDDD"), equalTo(99L)); + } + + @Test + void countGoodIntegersOnPath7() { + assertThat(new Solution().countGoodIntegersOnPath(1L, 99L, "DDDRRR"), equalTo(54L)); + } + + @Test + void countGoodIntegersOnPath8() { + assertThat( + new Solution() + .countGoodIntegersOnPath(9000000000000000L, 9000000000000000L, "RRRDDD"), + equalTo(0L)); + } +} diff --git a/src/test/java/g3901_4000/s3908_valid_digit_number/SolutionTest.java b/src/test/java/g3901_4000/s3908_valid_digit_number/SolutionTest.java new file mode 100644 index 000000000..a45479999 --- /dev/null +++ b/src/test/java/g3901_4000/s3908_valid_digit_number/SolutionTest.java @@ -0,0 +1,48 @@ +package g3901_4000.s3908_valid_digit_number; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void validDigit() { + assertThat(new Solution().validDigit(101, 0), equalTo(true)); + } + + @Test + void validDigit2() { + assertThat(new Solution().validDigit(232, 2), equalTo(false)); + } + + @Test + void validDigit3() { + assertThat(new Solution().validDigit(5, 1), equalTo(false)); + } + + @Test + void validDigit4() { + assertThat(new Solution().validDigit(0, 0), equalTo(false)); + } + + @Test + void validDigit5() { + assertThat(new Solution().validDigit(0, 1), equalTo(false)); + } + + @Test + void validDigit6() { + assertThat(new Solution().validDigit(5, 5), equalTo(false)); + } + + @Test + void validDigit7() { + assertThat(new Solution().validDigit(123, 3), equalTo(true)); + } + + @Test + void validDigit8() { + assertThat(new Solution().validDigit(100000, 0), equalTo(true)); + } +} diff --git a/src/test/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/SolutionTest.java b/src/test/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/SolutionTest.java new file mode 100644 index 000000000..9f6c1b86d --- /dev/null +++ b/src/test/java/g3901_4000/s3909_compare_sums_of_bitonic_parts/SolutionTest.java @@ -0,0 +1,37 @@ +package g3901_4000.s3909_compare_sums_of_bitonic_parts; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void compareBitonicSums() { + assertThat(new Solution().compareBitonicSums(new int[] {1, 3, 2, 1}), equalTo(1)); + } + + @Test + void compareBitonicSums2() { + assertThat(new Solution().compareBitonicSums(new int[] {2, 4, 5, 2}), equalTo(0)); + } + + @Test + void compareBitonicSums3() { + assertThat(new Solution().compareBitonicSums(new int[] {1, 2, 4, 3}), equalTo(-1)); + } + + @Test + void compareBitonicSums4() { + assertThat(new Solution().compareBitonicSums(new int[] {1, 2, 1}), equalTo(-1)); + } + + @Test + void compareBitonicSums5() { + assertThat( + new Solution() + .compareBitonicSums( + new int[] {999999997, 999999998, 999999999, 1000000000, 1}), + equalTo(0)); + } +} diff --git a/src/test/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/SolutionTest.java b/src/test/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/SolutionTest.java new file mode 100644 index 000000000..a1bcb9a46 --- /dev/null +++ b/src/test/java/g3901_4000/s3910_count_connected_subgraphs_with_even_node_sum/SolutionTest.java @@ -0,0 +1,57 @@ +package g3901_4000.s3910_count_connected_subgraphs_with_even_node_sum; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void evenSumSubgraphs() { + assertThat( + new Solution().evenSumSubgraphs(new int[] {1, 0, 1}, new int[][] {{0, 1}, {1, 2}}), + equalTo(2)); + } + + @Test + void evenSumSubgraphs2() { + assertThat(new Solution().evenSumSubgraphs(new int[] {1}, new int[][] {}), equalTo(0)); + } + + @Test + void evenSumSubgraphs3() { + assertThat(new Solution().evenSumSubgraphs(new int[] {0}, new int[][] {}), equalTo(1)); + } + + @Test + void evenSumSubgraphs4() { + assertThat( + new Solution().evenSumSubgraphs(new int[] {0, 1, 0}, new int[][] {}), equalTo(2)); + } + + @Test + void evenSumSubgraphs5() { + assertThat( + new Solution() + .evenSumSubgraphs( + new int[] {0, 0, 0}, new int[][] {{0, 1}, {1, 2}, {0, 2}}), + equalTo(7)); + } + + @Test + void evenSumSubgraphs6() { + assertThat( + new Solution() + .evenSumSubgraphs( + new int[] {1, 1, 1}, new int[][] {{0, 1}, {1, 2}, {0, 2}}), + equalTo(3)); + } + + @Test + void evenSumSubgraphs7() { + assertThat( + new Solution() + .evenSumSubgraphs(new int[] {1, 1, 0, 0}, new int[][] {{0, 1}, {2, 3}}), + equalTo(4)); + } +} diff --git a/src/test/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/SolutionTest.java b/src/test/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/SolutionTest.java new file mode 100644 index 000000000..84920d0e5 --- /dev/null +++ b/src/test/java/g3901_4000/s3911_k_th_smallest_remaining_even_integer_in_subarray_queries/SolutionTest.java @@ -0,0 +1,63 @@ +package g3901_4000.s3911_k_th_smallest_remaining_even_integer_in_subarray_queries; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void kthRemainingInteger() { + assertThat( + new Solution() + .kthRemainingInteger( + new int[] {1, 4, 7}, new int[][] {{0, 2, 1}, {1, 1, 2}, {0, 0, 3}}), + equalTo(new int[] {2, 6, 6})); + } + + @Test + void kthRemainingInteger2() { + assertThat( + new Solution() + .kthRemainingInteger( + new int[] {2, 5, 8}, new int[][] {{0, 1, 2}, {1, 2, 1}, {0, 2, 4}}), + equalTo(new int[] {6, 2, 12})); + } + + @Test + void kthRemainingInteger3() { + assertThat( + new Solution() + .kthRemainingInteger(new int[] {3, 6}, new int[][] {{0, 1, 1}, {1, 1, 3}}), + equalTo(new int[] {2, 8})); + } + + @Test + void kthRemainingInteger4() { + assertThat( + new Solution() + .kthRemainingInteger( + new int[] {2, 4, 6}, + new int[][] {{0, 2, 1}, {1, 2, 1}, {0, 2, 1000000000}}), + equalTo(new int[] {8, 2, 2000000006})); + } + + @Test + void kthRemainingInteger5() { + assertThat( + new Solution() + .kthRemainingInteger( + new int[] {2, 6, 10}, + new int[][] {{0, 2, 1}, {0, 2, 2}, {0, 2, 3}}), + equalTo(new int[] {4, 8, 12})); + } + + @Test + void kthRemainingInteger6() { + assertThat( + new Solution() + .kthRemainingInteger( + new int[] {1, 3, 5}, new int[][] {{0, 2, 1}, {1, 2, 4}}), + equalTo(new int[] {2, 8})); + } +} diff --git a/src/test/java/g3901_4000/s3912_valid_elements_in_an_array/SolutionTest.java b/src/test/java/g3901_4000/s3912_valid_elements_in_an_array/SolutionTest.java new file mode 100644 index 000000000..8c5984785 --- /dev/null +++ b/src/test/java/g3901_4000/s3912_valid_elements_in_an_array/SolutionTest.java @@ -0,0 +1,46 @@ +package g3901_4000.s3912_valid_elements_in_an_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findValidElements() { + assertThat( + new Solution().findValidElements(new int[] {1, 2, 4, 2, 3, 2}), + equalTo(List.of(1, 2, 4, 3, 2))); + } + + @Test + void findValidElements2() { + assertThat( + new Solution().findValidElements(new int[] {5, 5, 5, 5}), equalTo(List.of(5, 5))); + } + + @Test + void findValidElements3() { + assertThat(new Solution().findValidElements(new int[] {1}), equalTo(List.of(1))); + } + + @Test + void findValidElements4() { + assertThat( + new Solution().findValidElements(new int[] {1, 2, 3, 4}), + equalTo(List.of(1, 2, 3, 4))); + } + + @Test + void findValidElements5() { + assertThat( + new Solution().findValidElements(new int[] {4, 3, 2, 1}), + equalTo(List.of(4, 3, 2, 1))); + } + + @Test + void findValidElements6() { + assertThat(new Solution().findValidElements(new int[] {5, 1, 5}), equalTo(List.of(5, 5))); + } +} diff --git a/src/test/java/g3901_4000/s3913_sort_vowels_by_frequency/SolutionTest.java b/src/test/java/g3901_4000/s3913_sort_vowels_by_frequency/SolutionTest.java new file mode 100644 index 000000000..8c6927e63 --- /dev/null +++ b/src/test/java/g3901_4000/s3913_sort_vowels_by_frequency/SolutionTest.java @@ -0,0 +1,43 @@ +package g3901_4000.s3913_sort_vowels_by_frequency; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sortVowels() { + assertThat(new Solution().sortVowels("leetcode"), equalTo("leetcedo")); + } + + @Test + void sortVowels2() { + assertThat(new Solution().sortVowels("aeiaaioooa"), equalTo("aaaaoooiie")); + } + + @Test + void sortVowels3() { + assertThat(new Solution().sortVowels("baeiou"), equalTo("baeiou")); + } + + @Test + void sortVowels4() { + assertThat(new Solution().sortVowels("rhythm"), equalTo("rhythm")); + } + + @Test + void sortVowels5() { + assertThat(new Solution().sortVowels("u"), equalTo("u")); + } + + @Test + void sortVowels6() { + assertThat(new Solution().sortVowels("uoeaiuoeai"), equalTo("uuooeeaaii")); + } + + @Test + void sortVowels7() { + assertThat(new Solution().sortVowels("obabao"), equalTo("obobaa")); + } +} diff --git a/src/test/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/SolutionTest.java b/src/test/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/SolutionTest.java new file mode 100644 index 000000000..7d4f5f5da --- /dev/null +++ b/src/test/java/g3901_4000/s3914_minimum_operations_to_make_array_non_decreasing/SolutionTest.java @@ -0,0 +1,41 @@ +package g3901_4000.s3914_minimum_operations_to_make_array_non_decreasing; + +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[] {3, 3, 2, 1}), equalTo(2L)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations(new int[] {5, 1, 2, 3}), equalTo(4L)); + } + + @Test + void minOperations3() { + assertThat(new Solution().minOperations(new int[] {7}), equalTo(0L)); + } + + @Test + void minOperations4() { + assertThat(new Solution().minOperations(new int[] {1, 2, 2, 4}), equalTo(0L)); + } + + @Test + void minOperations5() { + assertThat(new Solution().minOperations(new int[] {5, 1, 5, 1}), equalTo(8L)); + } + + @Test + void minOperations6() { + assertThat( + new Solution() + .minOperations(new int[] {1000000000, 1, 1000000000, 1, 1000000000, 1}), + equalTo(2999999997L)); + } +} diff --git a/src/test/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/SolutionTest.java b/src/test/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/SolutionTest.java new file mode 100644 index 000000000..1ae2beb6b --- /dev/null +++ b/src/test/java/g3901_4000/s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k/SolutionTest.java @@ -0,0 +1,62 @@ +package g3901_4000.s3915_maximum_sum_of_alternating_subsequence_with_distance_at_least_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxAlternatingSum() { + int[] nums = new int[30000]; + for (int i = 0; i < nums.length; i++) { + nums[i] = i % 2 == 0 ? 100000 : 99999; + } + assertThat(new Solution().maxAlternatingSum(nums, 1), equalTo(2999985000L)); + } + + @Test + void maxAlternatingSum2() { + assertThat(new Solution().maxAlternatingSum(new int[] {5, 4, 2}, 2), equalTo(7L)); + } + + @Test + void maxAlternatingSum3() { + assertThat(new Solution().maxAlternatingSum(new int[] {3, 5, 4, 2, 4}, 1), equalTo(14L)); + } + + @Test + void maxAlternatingSum4() { + assertThat(new Solution().maxAlternatingSum(new int[] {5}, 1), equalTo(5L)); + } + + @Test + void maxAlternatingSum5() { + assertThat(new Solution().maxAlternatingSum(new int[] {4, 4, 4}, 1), equalTo(4L)); + } + + @Test + void maxAlternatingSum6() { + assertThat(new Solution().maxAlternatingSum(new int[] {3, 9, 4}, 3), equalTo(9L)); + } + + @Test + void maxAlternatingSum7() { + assertThat(new Solution().maxAlternatingSum(new int[] {1, 2, 3, 4}, 1), equalTo(7L)); + } + + @Test + void maxAlternatingSum8() { + assertThat(new Solution().maxAlternatingSum(new int[] {4, 3, 2, 1}, 1), equalTo(7L)); + } + + @Test + void maxAlternatingSum9() { + assertThat(new Solution().maxAlternatingSum(new int[] {2, 5, 1, 4, 2}, 1), equalTo(14L)); + } + + @Test + void maxAlternatingSum10() { + assertThat(new Solution().maxAlternatingSum(new int[] {5, 1, 4, 2, 6}, 2), equalTo(15L)); + } +}