From 00438479bed1ed4ec5275c200588ce8ee620a05b Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Sun, 26 Apr 2026 18:10:22 +0700 Subject: [PATCH 01/10] added tasks 3737-3747 --- .../Solution.java | 22 +++++ .../readme.md | 53 ++++++++++ .../Solution.java | 39 ++++++++ .../readme.md | 38 ++++++++ .../Solution.java | 21 ++++ .../readme.md | 53 ++++++++++ .../Solution.java | 24 +++++ .../readme.md | 50 ++++++++++ .../Solution.java | 27 ++++++ .../readme.md | 50 ++++++++++ .../Solution.java | 57 +++++++++++ .../readme.md | 96 +++++++++++++++++++ .../Solution.java | 72 ++++++++++++++ .../readme.md | 52 ++++++++++ .../Solution.java | 24 +++++ .../readme.md | 34 +++++++ .../Solution.java | 14 +++ .../readme.md | 44 +++++++++ .../Solution.java | 33 +++++++ .../readme.md | 33 +++++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ .../SolutionTest.java | 23 +++++ 30 files changed, 1066 insertions(+) create mode 100644 src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/Solution.java create mode 100644 src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/readme.md create mode 100644 src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java create mode 100644 src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/readme.md create mode 100644 src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java create mode 100644 src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/readme.md create mode 100644 src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java create mode 100644 src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/readme.md create mode 100644 src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java create mode 100644 src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/readme.md create mode 100644 src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java create mode 100644 src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md create mode 100644 src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java create mode 100644 src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/readme.md create mode 100644 src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/Solution.java create mode 100644 src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/readme.md create mode 100644 src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/Solution.java create mode 100644 src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/readme.md create mode 100644 src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java create mode 100644 src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/readme.md create mode 100644 src/test/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3742_maximum_path_score_in_a_grid/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3743_maximize_cyclic_partition_score/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3745_maximize_expression_of_three_elements/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/SolutionTest.java create mode 100644 src/test/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/SolutionTest.java diff --git a/src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/Solution.java b/src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/Solution.java new file mode 100644 index 000000000..1c4cda309 --- /dev/null +++ b/src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/Solution.java @@ -0,0 +1,22 @@ +package g3701_3800.s3737_count_subarrays_with_majority_element_i; + +// #Medium #Array #Hash_Table #Prefix_Sum #Counting #Divide_and_Conquer #Segment_Tree #Merge_Sort +// #Senior #Biweekly_Contest_169 #2026_04_26_Time_1_ms_(100.00%)_Space_46.96_MB_(57.62%) + +public class Solution { + public int countMajoritySubarrays(int[] a, int target) { + int n = a.length; + int pre = n + 1; + int res = 0; + int[] count = new int[2 * n + 2]; + int[] acc = new int[2 * n + 2]; + count[pre] = acc[pre] = 1; + for (int i : a) { + pre += (i == target ? 1 : -1); + count[pre]++; + acc[pre] = acc[pre - 1] + count[pre]; + res += acc[pre - 1]; + } + return res; + } +} diff --git a/src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/readme.md b/src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/readme.md new file mode 100644 index 000000000..1006f055a --- /dev/null +++ b/src/main/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/readme.md @@ -0,0 +1,53 @@ +3737\. Count Subarrays With Majority Element I + +Medium + +You are given an integer array `nums` and an integer `target`. + +Return the number of **non-empty subarrays** of `nums` in which `target` is the **majority element**. + +The **majority element** of a subarray is the element that appears **strictly** **more than half** of the times in that subarray. + +**Example 1:** + +**Input:** nums = [1,2,2,3], target = 2 + +**Output:** 5 + +**Explanation:** + +Valid subarrays with `target = 2` as the majority element: + +* `nums[1..1] = [2]` +* `nums[2..2] = [2]` +* `nums[1..2] = [2,2]` +* `nums[0..2] = [1,2,2]` +* `nums[1..3] = [2,2,3]` + +So there are 5 such subarrays. + +**Example 2:** + +**Input:** nums = [1,1,1,1], target = 1 + +**Output:** 10 + +**Explanation:** + +All 10 subarrays have 1 as the majority element. + +**Example 3:** + +**Input:** nums = [1,2,3], target = 4 + +**Output:** 0 + +**Explanation:** + +`target = 4` does not appear in `nums` at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 109 +* 1 <= target <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java b/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java new file mode 100644 index 000000000..f9fc24816 --- /dev/null +++ b/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java @@ -0,0 +1,39 @@ +package g3701_3800.s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element; + +// #Medium #Array #Dynamic_Programming #Staff #Biweekly_Contest_169 +// #2026_04_26_Time_7_ms_(99.28%)_Space_123.88_MB_(5.07%) + +public class Solution { + public int longestSubarray(int[] nums) { + int n = nums.length; + if (n < 3) return n; + int i = 0; + int len1 = 0; + int len2 = 0; + int ans = 2; + while (i < n) { + int l = (i == 0) ? -1000 * 1000 * 10 : nums[i - 1]; + int r = (i == n - 1) ? 1000 * 1000 * 10 : nums[i + 1]; + if (l <= nums[i]) { + len2++; + ans = Math.max(len2 + 1, ans); + } else if (r >= nums[i]) { + int j = i; + len1 = len2; + while (j < n - 1 && nums[j] <= nums[j + 1]) { + j++; + } + len2 = j - i + 1; + ans = Math.max(len2 + 1, ans); + if (l <= r || (len1 > 1 && nums[i - 2] <= nums[i])) + ans = Math.max(len1 + len2, ans); + i = j; + } else { + len2 = 0; + } + i++; + } + ans = Math.min(ans, n); + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/readme.md b/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/readme.md new file mode 100644 index 000000000..d0542bca3 --- /dev/null +++ b/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/readme.md @@ -0,0 +1,38 @@ +3738\. Longest Non-Decreasing Subarray After Replacing at Most One Element + +Medium + +You are given an integer array `nums`. + +You are allowed to replace **at most** one element in the array with any other integer value of your choice. + +Return the length of the **longest non-decreasing subarray** that can be obtained after performing at most one replacement. + +An array is said to be **non-decreasing** if each element is greater than or equal to its previous one (if it exists). + +**Example 1:** + +**Input:** nums = [1,2,3,1,2] + +**Output:** 4 + +**Explanation:** + +Replacing `nums[3] = 1` with 3 gives the array [1, 2, 3, 3, 2]. + +The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4. + +**Example 2:** + +**Input:** nums = [2,2,2,2,2] + +**Output:** 5 + +**Explanation:** + +All elements in `nums` are equal, so it is already non-decreasing and the entire `nums` forms a subarray of length 5. + +**Constraints:** + +* 1 <= nums.length <= 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java b/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java new file mode 100644 index 000000000..cb622c812 --- /dev/null +++ b/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java @@ -0,0 +1,21 @@ +package g3701_3800.s3739_count_subarrays_with_majority_element_ii; + +// #Hard #Array #Hash_Table #Prefix_Sum #Divide_and_Conquer #Segment_Tree #Merge_Sort #Senior_Staff +// #Biweekly_Contest_169 #2026_04_26_Time_3_ms_(100.00%)_Space_90.94_MB_(36.19%) + +public class Solution { + public long countMajoritySubarrays(int[] A, int target) { + int n = A.length; + int pre = n + 1; + long[] count = new long[2 * n + 2]; + long[] acc = new long[2 * n + 2]; + long res = 0; + count[pre] = acc[pre] = 1; + for (int a : A) { + pre += (a == target ? 1 : -1); + acc[pre] = ++count[pre] + acc[pre - 1]; + res += acc[pre - 1]; + } + return res; + } +} diff --git a/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/readme.md b/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/readme.md new file mode 100644 index 000000000..f8da05e77 --- /dev/null +++ b/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/readme.md @@ -0,0 +1,53 @@ +3739\. Count Subarrays With Majority Element II + +Hard + +You are given an integer array `nums` and an integer `target`. + +Return the number of **non-empty subarrays** of `nums` in which `target` is the **majority element**. + +The **majority element** of a subarray is the element that appears **strictly more than half** of the times in that subarray. + +**Example 1:** + +**Input:** nums = [1,2,2,3], target = 2 + +**Output:** 5 + +**Explanation:** + +Valid subarrays with `target = 2` as the majority element: + +* `nums[1..1] = [2]` +* `nums[2..2] = [2]` +* `nums[1..2] = [2,2]` +* `nums[0..2] = [1,2,2]` +* `nums[1..3] = [2,2,3]` + +So there are 5 such subarrays. + +**Example 2:** + +**Input:** nums = [1,1,1,1], target = 1 + +**Output:** 10 + +**Explanation:** + +All 10 subarrays have 1 as the majority element. + +**Example 3:** + +**Input:** nums = [1,2,3], target = 4 + +**Output:** 0 + +**Explanation:** + +`target = 4` does not appear in `nums` at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* 1 <= target <= 109 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java b/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java new file mode 100644 index 000000000..83ffcee06 --- /dev/null +++ b/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java @@ -0,0 +1,24 @@ +package g3701_3800.s3740_minimum_distance_between_three_equal_elements_i; + +// #Easy #Array #Hash_Table #Mid_Level #Weekly_Contest_475 +// #2026_04_26_Time_1_ms_(99.99%)_Space_44.21_MB_(75.36%) + +public class Solution { + public int minimumDistance(int[] nums) { + int len = nums.length; + int[] last2 = new int[len]; + int res = 200; + + for (int i = 0; i < len; i++) { + int val = nums[i] - 1; + int pos = i + 1, pack = last2[val]; + int old = pack & 255, cur = pack >> 8; + + last2[val] = cur | (pos << 8); + + if (old > 0) res = Math.min(res, (pos - old) << 1); + } + + return res == 200 ? -1 : res; + } +} diff --git a/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/readme.md b/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/readme.md new file mode 100644 index 000000000..28e3b80ac --- /dev/null +++ b/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/readme.md @@ -0,0 +1,50 @@ +3740\. Minimum Distance Between Three Equal Elements I + +Easy + +You are given an integer array `nums`. + +A tuple `(i, j, k)` of 3 **distinct** indices is **good** if `nums[i] == nums[j] == nums[k]`. + +The **distance** of a **good** tuple is `abs(i - j) + abs(j - k) + abs(k - i)`, where `abs(x)` denotes the **absolute value** of `x`. + +Return an integer denoting the **minimum** possible **distance** of a **good** tuple. If no **good** tuples exist, return `-1`. + +**Example 1:** + +**Input:** nums = [1,2,1,1,3] + +**Output:** 6 + +**Explanation:** + +The minimum distance is achieved by the good tuple `(0, 2, 3)`. + +`(0, 2, 3)` is a good tuple because `nums[0] == nums[2] == nums[3] == 1`. Its distance is `abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6`. + +**Example 2:** + +**Input:** nums = [1,1,2,3,2,1,2] + +**Output:** 8 + +**Explanation:** + +The minimum distance is achieved by the good tuple `(2, 4, 6)`. + +`(2, 4, 6)` is a good tuple because `nums[2] == nums[4] == nums[6] == 2`. Its distance is `abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8`. + +**Example 3:** + +**Input:** nums = [1] + +**Output:** \-1 + +**Explanation:** + +There are no good tuples. Therefore, the answer is -1. + +**Constraints:** + +* `1 <= n == nums.length <= 100` +* `1 <= nums[i] <= n` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java b/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java new file mode 100644 index 000000000..8c120eac1 --- /dev/null +++ b/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java @@ -0,0 +1,27 @@ +package g3701_3800.s3741_minimum_distance_between_three_equal_elements_ii; + +// #Medium #Array #Hash_Table #Senior #Weekly_Contest_475 +// #2026_04_26_Time_6_ms_(99.60%)_Space_161.24_MB_(96.23%) + +public class Solution { + public int minimumDistance(int[] nums) { + int n = nums.length; + int ans = Integer.MAX_VALUE; + int[] prev1 = new int[n + 1]; + int[] prev2 = new int[n + 1]; + for (int i = 0; i < n + 1; i++) { + prev1[i] = prev2[i] = -1; + } + + for (int i = 0; i < n; i++) { + int value = nums[i]; + if (prev2[value] != -1) { + ans = Math.min(ans, (i - prev2[value])); + } + prev2[value] = prev1[value]; + prev1[value] = i; + } + if (ans < 100002) return ans * 2; + return -1; + } +} diff --git a/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/readme.md b/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/readme.md new file mode 100644 index 000000000..d3aee5aa3 --- /dev/null +++ b/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/readme.md @@ -0,0 +1,50 @@ +3741\. Minimum Distance Between Three Equal Elements II + +Medium + +You are given an integer array `nums`. + +A tuple `(i, j, k)` of 3 **distinct** indices is **good** if `nums[i] == nums[j] == nums[k]`. + +The **distance** of a **good** tuple is `abs(i - j) + abs(j - k) + abs(k - i)`, where `abs(x)` denotes the **absolute value** of `x`. + +Return an integer denoting the **minimum** possible **distance** of a **good** tuple. If no **good** tuples exist, return `-1`. + +**Example 1:** + +**Input:** nums = [1,2,1,1,3] + +**Output:** 6 + +**Explanation:** + +The minimum distance is achieved by the good tuple `(0, 2, 3)`. + +`(0, 2, 3)` is a good tuple because `nums[0] == nums[2] == nums[3] == 1`. Its distance is `abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6`. + +**Example 2:** + +**Input:** nums = [1,1,2,3,2,1,2] + +**Output:** 8 + +**Explanation:** + +The minimum distance is achieved by the good tuple `(2, 4, 6)`. + +`(2, 4, 6)` is a good tuple because `nums[2] == nums[4] == nums[6] == 2`. Its distance is `abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8`. + +**Example 3:** + +**Input:** nums = [1] + +**Output:** \-1 + +**Explanation:** + +There are no good tuples. Therefore, the answer is -1. + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* `1 <= nums[i] <= n` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java new file mode 100644 index 000000000..e8f2338f9 --- /dev/null +++ b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java @@ -0,0 +1,57 @@ +package g3701_3800.s3742_maximum_path_score_in_a_grid; + +// #Medium #Array #Dynamic_Programming #Matrix #Staff #Weekly_Contest_475 +// #2026_04_26_Time_212_ms_(91.38%)_Space_92.93_MB_(35.06%) + +public class Solution { + public int maxPathScore(int[][] grid, int k) { + int n = grid.length; + int m = grid[0].length; + int mxc = Math.min(k + 1, n + m + 5); + int[][][] dp = new int[n][m][mxc]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + for (int c = 0; c < mxc; c++) { + dp[i][j][c] = -1; + } + } + } + + dp[0][0][0] = 0; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + for (int c = 0; c < mxc; c++) { + if (dp[i][j][c] == -1) { + continue; + } + + // move right + if (j + 1 < m) { + int cost = c + (grid[i][j + 1] > 0 ? 1 : 0); + if (cost < mxc) { + dp[i][j + 1][cost] = + Math.max(dp[i][j + 1][cost], dp[i][j][c] + grid[i][j + 1]); + } + } + + // move down + if (i + 1 < n) { + int cost = c + (grid[i + 1][j] > 0 ? 1 : 0); + if (cost < mxc) { + dp[i + 1][j][cost] = + Math.max(dp[i + 1][j][cost], dp[i][j][c] + grid[i + 1][j]); + } + } + } + } + } + + int ans = -1; + for (int c = 0; c < mxc; c++) { + ans = Math.max(ans, dp[n - 1][m - 1][c]); + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md new file mode 100644 index 000000000..4475a4a48 --- /dev/null +++ b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md @@ -0,0 +1,96 @@ +3742\. Maximum Path Score in a Grid + +Medium + +You are given an `m x n` grid where each cell contains one of the values 0, 1, or 2. You are also given an integer `k`. + +You start from the top-left corner `(0, 0)` and want to reach the bottom-right corner `(m - 1, n - 1)` by moving only **right** or **down**. + +Each cell contributes a specific score and incurs an associated cost, according to their cell values: + +* 0: adds 0 to your score and costs 0. +* 1: adds 1 to your score and costs 1. +* 2: adds 2 to your score and costs 1. + +Return the **maximum** score achievable without exceeding a total cost of `k`, or -1 if no valid path exists. + +**Note:** If you reach the last cell but the total cost exceeds `k`, the path is invalid. + +**Example 1:** + +**Input:** grid = [[0, 1],[2, 0]], k = 1 + +**Output:** 2 + +**Explanation:** + +The optimal path is: + +Cell + +grid[i][j] + +Score + +Total +Score + +Cost + +Total +Cost + +(0, 0) + +0 + +0 + +0 + +0 + +0 + +(1, 0) + +2 + +2 + +2 + +1 + +1 + +(1, 1) + +0 + +0 + +2 + +0 + +1 + +Thus, the maximum possible score is 2. + +**Example 2:** + +**Input:** grid = [[0, 1],[1, 2]], k = 1 + +**Output:** \-1 + +**Explanation:** + +There is no path that reaches cell `(1, 1)` without exceeding cost k. Thus, the answer is -1. + +**Constraints:** + +* `1 <= m, n <= 200` +* 0 <= k <= 103 +* `grid[0][0] == 0` +* `0 <= grid[i][j] <= 2` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java b/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java new file mode 100644 index 000000000..c6a020317 --- /dev/null +++ b/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java @@ -0,0 +1,72 @@ +package g3701_3800.s3743_maximize_cyclic_partition_score; + +// #Hard #Array #Dynamic_Programming #Weekly_Contest_475 #Principal +// #2026_04_26_Time_40_ms_(96.15%)_Space_120.45_MB_(7.69%) + +public class Solution { + public long maximumScore(int[] nums, int k) { + // Find index of minimum element + int j = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < nums[j]) j = i; + } + + // Build array 'a' + int n = nums.length; + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = nums[(j + i) % n]; + } + + // Build array 'b' (rotated and reversed) + int[] b = new int[n]; + for (int i = 0; i < n; i++) { + b[i] = nums[(j + 1 + i) % n]; + } + reverse(b); + + // Compute and return max of f(a, k) and f(b, k) + return Math.max(f(a, k), f(b, k)); + } + + private long f(int[] a, int k) { + int n = a.length; + long[][] dp = new long[k + 1][n + 1]; + + long mn = Long.MAX_VALUE; + long mx = Long.MIN_VALUE; + + // Initialize dp[1][j+1] + for (int j = 0; j < n; j++) { + mn = Math.min(mn, a[j]); + mx = Math.max(mx, a[j]); + dp[1][j + 1] = mx - mn; + } + + long res = dp[1][n]; + + for (int i = 2; i <= k; i++) { + long x = Long.MIN_VALUE, y = Long.MIN_VALUE; + for (int j = i - 1; j < n; j++) { + x = Math.max(x, dp[i - 1][j] - a[j]); + y = Math.max(y, dp[i - 1][j] + a[j]); + dp[i][j + 1] = Math.max(dp[i][j], Math.max(x + a[j], y - a[j])); + } + res = Math.max(res, dp[i][n]); + } + + return res; + } + + private void reverse(int[] arr) { + int left = 0; + int right = arr.length - 1; + while (left < right) { + int temp = arr[left]; + arr[left] = arr[right]; + arr[right] = temp; + left++; + right--; + } + } +} diff --git a/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/readme.md b/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/readme.md new file mode 100644 index 000000000..56f6640a3 --- /dev/null +++ b/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/readme.md @@ -0,0 +1,52 @@ +3743\. Maximize Cyclic Partition Score + +Hard + +You are given a **cyclic** array `nums` and an integer `k`. + +**Partition** `nums` into **at most** `k` non-empty subarrays. As `nums` is cyclic, these subarrays may wrap around from the end of the array back to the beginning. + +The **range** of a subarray is the difference between its **maximum** and **minimum** values. The **score** of a partition is the sum of subarray **ranges**. + +Return the **maximum** possible **score** among all cyclic partitions. + +**Example 1:** + +**Input:** nums = [1,2,3,3], k = 2 + +**Output:** 3 + +**Explanation:** + +* Partition `nums` into `[2, 3]` and `[3, 1]` (wrapped around). +* The range of `[2, 3]` is `max(2, 3) - min(2, 3) = 3 - 2 = 1`. +* The range of `[3, 1]` is `max(3, 1) - min(3, 1) = 3 - 1 = 2`. +* The score is `1 + 2 = 3`. + +**Example 2:** + +**Input:** nums = [1,2,3,3], k = 1 + +**Output:** 2 + +**Explanation:** + +* Partition `nums` into `[1, 2, 3, 3]`. +* The range of `[1, 2, 3, 3]` is `max(1, 2, 3, 3) - min(1, 2, 3, 3) = 3 - 1 = 2`. +* The score is 2. + +**Example 3:** + +**Input:** nums = [1,2,3,3], k = 4 + +**Output:** 3 + +**Explanation:** + +Identical to Example 1, we partition `nums` into `[2, 3]` and `[3, 1]`. Note that `nums` may be partitioned into fewer than `k` subarrays. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* 1 <= nums[i] <= 109 +* `1 <= k <= nums.length` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/Solution.java b/src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/Solution.java new file mode 100644 index 000000000..854c4c2d6 --- /dev/null +++ b/src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/Solution.java @@ -0,0 +1,24 @@ +package g3701_3800.s3745_maximize_expression_of_three_elements; + +// #Easy #Array #Sorting #Greedy #Enumeration #Mid_Level #Weekly_Contest_476 +// #2026_04_26_Time_1_ms_(99.84%)_Space_45.12_MB_(68.56%) + +public class Solution { + public int maximizeExpressionOfThree(int[] nums) { + int max1 = Integer.MIN_VALUE; + int max2 = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + for (int num : nums) { + if (num > max1) { + max2 = max1; + max1 = num; + } else if (num > max2) { + max2 = num; + } + if (num < min) { + min = num; + } + } + return max1 + max2 - min; + } +} diff --git a/src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/readme.md b/src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/readme.md new file mode 100644 index 000000000..8b7d60816 --- /dev/null +++ b/src/main/java/g3701_3800/s3745_maximize_expression_of_three_elements/readme.md @@ -0,0 +1,34 @@ +3745\. Maximize Expression of Three Elements + +Easy + +You are given an integer array `nums`. + +Choose three elements `a`, `b`, and `c` from `nums` at **distinct** indices such that the value of the expression `a + b - c` is maximized. + +Return an integer denoting the **maximum possible value** of this expression. + +**Example 1:** + +**Input:** nums = [1,4,2,5] + +**Output:** 8 + +**Explanation:** + +We can choose `a = 4`, `b = 5`, and `c = 1`. The expression value is `4 + 5 - 1 = 8`, which is the maximum possible. + +**Example 2:** + +**Input:** nums = [-2,0,5,-2,4] + +**Output:** 11 + +**Explanation:** + +We can choose `a = 5`, `b = 4`, and `c = -2`. The expression value is `5 + 4 - (-2) = 11`, which is the maximum possible. + +**Constraints:** + +* `3 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/Solution.java b/src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/Solution.java new file mode 100644 index 000000000..e6f31ded3 --- /dev/null +++ b/src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/Solution.java @@ -0,0 +1,14 @@ +package g3701_3800.s3746_minimum_string_length_after_balanced_removals; + +// #Medium #String #Stack #Counting #Senior #Weekly_Contest_476 +// #2026_04_26_Time_3_ms_(95.94%)_Space_47.34_MB_(25.76%) + +public class Solution { + public int minLengthAfterRemovals(String s) { + int[] hash = new int[2]; + for (char c : s.toCharArray()) { + hash[c - 'a']++; + } + return Math.abs(hash[0] - hash[1]); + } +} diff --git a/src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/readme.md b/src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/readme.md new file mode 100644 index 000000000..d97662b43 --- /dev/null +++ b/src/main/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/readme.md @@ -0,0 +1,44 @@ +3746\. Minimum String Length After Balanced Removals + +Medium + +You are given a string `s` consisting only of the characters `'a'` and `'b'`. + +You are allowed to repeatedly remove **any substring** where the number of `'a'` characters is equal to the number of `'b'` characters. After each removal, the remaining parts of the string are concatenated together without gaps. + +Return an integer denoting the **minimum possible length** of the string after performing any number of such operations. + +**Example 1:** + +**Input:** s = `"aabbab"` + +**Output:** 0 + +**Explanation:** + +The substring `"aabbab"` has three `'a'` and three `'b'`. Since their counts are equal, we can remove the entire string directly. The minimum length is 0. + +**Example 2:** + +**Input:** s = `"aaaa"` + +**Output:** 4 + +**Explanation:** + +Every substring of `"aaaa"` contains only `'a'` characters. No substring can be removed as a result, so the minimum length remains 4. + +**Example 3:** + +**Input:** s = `"aaabb"` + +**Output:** 1 + +**Explanation:** + +First, remove the substring `"ab"`, leaving `"aab"`. Next, remove the new substring `"ab"`, leaving `"a"`. No further removals are possible, so the minimum length is 1. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'a'` or `'b'`. \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java b/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java new file mode 100644 index 000000000..c9584b96c --- /dev/null +++ b/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java @@ -0,0 +1,33 @@ +package g3701_3800.s3747_count_distinct_integers_after_removing_zeros; + +// #Medium #Dynamic_Programming #Math #Staff #Weekly_Contest_476 +// #2026_04_26_Time_1_ms_(100.00%)_Space_42.72_MB_(79.66%) + +public class Solution { + public long countDistinct(long n) { + String digits = Long.toString(n); + int m = digits.length(); + + long[] power9 = new long[m + 1]; + power9[0] = 1; + for (int i = 1; i <= m; i++) { + power9[i] = power9[i - 1] * 9; + } + + long total = 0; + + for (int length = 1; length < m; length++) { + total += power9[length]; + } + + for (int idx = 0; idx < m; idx++) { + int d = digits.charAt(idx) - '0'; + if (d == 0) { + return total; + } + total += (d - 1) * power9[m - idx - 1]; + } + + return total + 1; + } +} diff --git a/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/readme.md b/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/readme.md new file mode 100644 index 000000000..fc8394dc5 --- /dev/null +++ b/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/readme.md @@ -0,0 +1,33 @@ +3747\. Count Distinct Integers After Removing Zeros + +Medium + +You are given a **positive** integer `n`. + +For every integer `x` from 1 to `n`, we write down the integer obtained by removing all zeros from the decimal representation of `x`. + +Return an integer denoting the number of **distinct** integers written down. + +**Example 1:** + +**Input:** n = 10 + +**Output:** 9 + +**Explanation:** + +The integers we wrote down are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1. There are 9 distinct integers (1, 2, 3, 4, 5, 6, 7, 8, 9). + +**Example 2:** + +**Input:** n = 3 + +**Output:** 3 + +**Explanation:** + +The integers we wrote down are 1, 2, 3. There are 3 distinct integers (1, 2, 3). + +**Constraints:** + +* 1 <= n <= 1015 \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/SolutionTest.java b/src/test/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/SolutionTest.java new file mode 100644 index 000000000..715d376f4 --- /dev/null +++ b/src/test/java/g3701_3800/s3737_count_subarrays_with_majority_element_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3737_count_subarrays_with_majority_element_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countMajoritySubarrays() { + assertThat(new Solution().countMajoritySubarrays(new int[] {1, 1, 2}, 1), equalTo(4)); + } + + @Test + void countMajoritySubarrays2() { + assertThat(new Solution().countMajoritySubarrays(new int[] {2, 2}, 1), equalTo(0)); + } + + @Test + void countMajoritySubarrays3() { + assertThat(new Solution().countMajoritySubarrays(new int[] {1, 2, 1, 2}, 1), equalTo(3)); + } +} diff --git a/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java b/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java new file mode 100644 index 000000000..ece28621b --- /dev/null +++ b/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element; + +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[] {1, 2, 3}), equalTo(3)); + } + + @Test + void longestSubarray2() { + assertThat(new Solution().longestSubarray(new int[] {3, 2, 1}), equalTo(2)); + } + + @Test + void longestSubarray3() { + assertThat(new Solution().longestSubarray(new int[] {1, 5, 3, 4, 6}), equalTo(5)); + } +} diff --git a/src/test/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/SolutionTest.java b/src/test/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/SolutionTest.java new file mode 100644 index 000000000..d2150cb30 --- /dev/null +++ b/src/test/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3739_count_subarrays_with_majority_element_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countMajoritySubarrays() { + assertThat(new Solution().countMajoritySubarrays(new int[] {1, 1, 2}, 1), equalTo(4L)); + } + + @Test + void countMajoritySubarrays2() { + assertThat(new Solution().countMajoritySubarrays(new int[] {2, 2}, 1), equalTo(0L)); + } + + @Test + void countMajoritySubarrays3() { + assertThat(new Solution().countMajoritySubarrays(new int[] {1, 2, 1, 2}, 1), equalTo(3L)); + } +} diff --git a/src/test/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/SolutionTest.java b/src/test/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/SolutionTest.java new file mode 100644 index 000000000..b37a726aa --- /dev/null +++ b/src/test/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3740_minimum_distance_between_three_equal_elements_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumDistance() { + assertThat(new Solution().minimumDistance(new int[] {1, 1, 1}), equalTo(4)); + } + + @Test + void minimumDistance2() { + assertThat(new Solution().minimumDistance(new int[] {1, 2, 3, 1, 2, 3}), equalTo(-1)); + } + + @Test + void minimumDistance3() { + assertThat(new Solution().minimumDistance(new int[] {1, 2, 1, 2, 1}), equalTo(8)); + } +} diff --git a/src/test/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/SolutionTest.java b/src/test/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/SolutionTest.java new file mode 100644 index 000000000..698fa427d --- /dev/null +++ b/src/test/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3741_minimum_distance_between_three_equal_elements_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumDistance() { + assertThat(new Solution().minimumDistance(new int[] {1, 1, 1}), equalTo(4)); + } + + @Test + void minimumDistance2() { + assertThat(new Solution().minimumDistance(new int[] {1, 2, 3, 1, 2, 3}), equalTo(-1)); + } + + @Test + void minimumDistance3() { + assertThat(new Solution().minimumDistance(new int[] {1, 2, 1, 2, 1}), equalTo(8)); + } +} diff --git a/src/test/java/g3701_3800/s3742_maximum_path_score_in_a_grid/SolutionTest.java b/src/test/java/g3701_3800/s3742_maximum_path_score_in_a_grid/SolutionTest.java new file mode 100644 index 000000000..1647dc91c --- /dev/null +++ b/src/test/java/g3701_3800/s3742_maximum_path_score_in_a_grid/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3742_maximum_path_score_in_a_grid; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxPathScore() { + assertThat(new Solution().maxPathScore(new int[][] {{0, 1}, {2, 3}}, 1), equalTo(-1)); + } + + @Test + void maxPathScore2() { + assertThat(new Solution().maxPathScore(new int[][] {{0, 1}, {2, 3}}, 2), equalTo(5)); + } + + @Test + void maxPathScore3() { + assertThat(new Solution().maxPathScore(new int[][] {{-1, -2}, {-3, -4}}, 0), equalTo(-1)); + } +} diff --git a/src/test/java/g3701_3800/s3743_maximize_cyclic_partition_score/SolutionTest.java b/src/test/java/g3701_3800/s3743_maximize_cyclic_partition_score/SolutionTest.java new file mode 100644 index 000000000..37f54fb21 --- /dev/null +++ b/src/test/java/g3701_3800/s3743_maximize_cyclic_partition_score/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3743_maximize_cyclic_partition_score; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumScore() { + assertThat(new Solution().maximumScore(new int[] {5, 5, 5}, 2), equalTo(0L)); + } + + @Test + void maximumScore2() { + assertThat(new Solution().maximumScore(new int[] {1, 3, 2}, 1), equalTo(2L)); + } + + @Test + void maximumScore3() { + assertThat(new Solution().maximumScore(new int[] {1, 4, 2}, 2), equalTo(3L)); + } +} diff --git a/src/test/java/g3701_3800/s3745_maximize_expression_of_three_elements/SolutionTest.java b/src/test/java/g3701_3800/s3745_maximize_expression_of_three_elements/SolutionTest.java new file mode 100644 index 000000000..3dfc57688 --- /dev/null +++ b/src/test/java/g3701_3800/s3745_maximize_expression_of_three_elements/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3745_maximize_expression_of_three_elements; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximizeExpressionOfThree() { + assertThat(new Solution().maximizeExpressionOfThree(new int[] {1, 2, 3}), equalTo(4)); + } + + @Test + void maximizeExpressionOfThree2() { + assertThat(new Solution().maximizeExpressionOfThree(new int[] {5, 5, 5}), equalTo(5)); + } + + @Test + void maximizeExpressionOfThree3() { + assertThat(new Solution().maximizeExpressionOfThree(new int[] {-5, -1, -3, 4}), equalTo(8)); + } +} diff --git a/src/test/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/SolutionTest.java b/src/test/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/SolutionTest.java new file mode 100644 index 000000000..057a9b553 --- /dev/null +++ b/src/test/java/g3701_3800/s3746_minimum_string_length_after_balanced_removals/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3746_minimum_string_length_after_balanced_removals; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minLengthAfterRemovals() { + assertThat(new Solution().minLengthAfterRemovals("ab"), equalTo(0)); + } + + @Test + void minLengthAfterRemovals2() { + assertThat(new Solution().minLengthAfterRemovals("aaab"), equalTo(2)); + } + + @Test + void minLengthAfterRemovals3() { + assertThat(new Solution().minLengthAfterRemovals("bbbb"), equalTo(4)); + } +} diff --git a/src/test/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/SolutionTest.java b/src/test/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/SolutionTest.java new file mode 100644 index 000000000..e4e91a351 --- /dev/null +++ b/src/test/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3747_count_distinct_integers_after_removing_zeros; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countDistinct() { + assertThat(new Solution().countDistinct(9L), equalTo(9L)); + } + + @Test + void countDistinct2() { + assertThat(new Solution().countDistinct(20L), equalTo(18L)); + } + + @Test + void countDistinct3() { + assertThat(new Solution().countDistinct(99L), equalTo(90L)); + } +} From 3b0567d7c4e1b224f01542278b0c767ae9abb2a4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:20:41 +0300 Subject: [PATCH 02/10] Rename parameter in countMajoritySubarrays method --- .../Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java b/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java index cb622c812..0c7ca8923 100644 --- a/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java +++ b/src/main/java/g3701_3800/s3739_count_subarrays_with_majority_element_ii/Solution.java @@ -4,14 +4,14 @@ // #Biweekly_Contest_169 #2026_04_26_Time_3_ms_(100.00%)_Space_90.94_MB_(36.19%) public class Solution { - public long countMajoritySubarrays(int[] A, int target) { - int n = A.length; + public long countMajoritySubarrays(int[] nums, int target) { + int n = nums.length; int pre = n + 1; long[] count = new long[2 * n + 2]; long[] acc = new long[2 * n + 2]; long res = 0; count[pre] = acc[pre] = 1; - for (int a : A) { + for (int a : nums) { pre += (a == target ? 1 : -1); acc[pre] = ++count[pre] + acc[pre - 1]; res += acc[pre - 1]; From 77de15083748e4c23b82f4092aa0d7b5ced0e15f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:21:39 +0300 Subject: [PATCH 03/10] Refactor minimumDistance method for clarity --- .../Solution.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java b/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java index 83ffcee06..9aa0d85bd 100644 --- a/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java +++ b/src/main/java/g3701_3800/s3740_minimum_distance_between_three_equal_elements_i/Solution.java @@ -8,17 +8,17 @@ public int minimumDistance(int[] nums) { int len = nums.length; int[] last2 = new int[len]; int res = 200; - for (int i = 0; i < len; i++) { int val = nums[i] - 1; - int pos = i + 1, pack = last2[val]; - int old = pack & 255, cur = pack >> 8; - + int pos = i + 1; + int pack = last2[val]; + int old = pack & 255; + int cur = pack >> 8; last2[val] = cur | (pos << 8); - - if (old > 0) res = Math.min(res, (pos - old) << 1); + if (old > 0) { + res = Math.min(res, (pos - old) << 1); + } } - return res == 200 ? -1 : res; } } From b55454a824059c763cd6a5165990d527da59690c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:22:48 +0300 Subject: [PATCH 04/10] Refactor return statement for minimum distance calculation --- .../Solution.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java b/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java index 8c120eac1..0140d2aa4 100644 --- a/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java +++ b/src/main/java/g3701_3800/s3741_minimum_distance_between_three_equal_elements_ii/Solution.java @@ -12,7 +12,6 @@ public int minimumDistance(int[] nums) { for (int i = 0; i < n + 1; i++) { prev1[i] = prev2[i] = -1; } - for (int i = 0; i < n; i++) { int value = nums[i]; if (prev2[value] != -1) { @@ -21,7 +20,9 @@ public int minimumDistance(int[] nums) { prev2[value] = prev1[value]; prev1[value] = i; } - if (ans < 100002) return ans * 2; + if (ans < 100002) { + return ans * 2; + } return -1; } } From 839a8ec8f2fa839ed1182e6dbc8b6233edf93966 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:25:10 +0300 Subject: [PATCH 05/10] Improve readability and efficiency in maximumScore method --- .../Solution.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java b/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java index c6a020317..6157b2620 100644 --- a/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java +++ b/src/main/java/g3701_3800/s3743_maximize_cyclic_partition_score/Solution.java @@ -8,23 +8,22 @@ public long maximumScore(int[] nums, int k) { // Find index of minimum element int j = 0; for (int i = 0; i < nums.length; i++) { - if (nums[i] < nums[j]) j = i; + if (nums[i] < nums[j]) { + j = i; + } } - // Build array 'a' int n = nums.length; int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nums[(j + i) % n]; } - // Build array 'b' (rotated and reversed) int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = nums[(j + 1 + i) % n]; } reverse(b); - // Compute and return max of f(a, k) and f(b, k) return Math.max(f(a, k), f(b, k)); } @@ -32,21 +31,18 @@ public long maximumScore(int[] nums, int k) { private long f(int[] a, int k) { int n = a.length; long[][] dp = new long[k + 1][n + 1]; - long mn = Long.MAX_VALUE; long mx = Long.MIN_VALUE; - // Initialize dp[1][j+1] for (int j = 0; j < n; j++) { mn = Math.min(mn, a[j]); mx = Math.max(mx, a[j]); dp[1][j + 1] = mx - mn; } - long res = dp[1][n]; - for (int i = 2; i <= k; i++) { - long x = Long.MIN_VALUE, y = Long.MIN_VALUE; + long x = Long.MIN_VALUE; + long y = Long.MIN_VALUE; for (int j = i - 1; j < n; j++) { x = Math.max(x, dp[i - 1][j] - a[j]); y = Math.max(y, dp[i - 1][j] + a[j]); @@ -54,7 +50,6 @@ private long f(int[] a, int k) { } res = Math.max(res, dp[i][n]); } - return res; } From 453b3ed5d3b0627d1cffef77a4fa838f5dcf3297 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:26:20 +0300 Subject: [PATCH 06/10] Refactor condition for short array handling --- .../Solution.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java b/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java index f9fc24816..24cf3ea96 100644 --- a/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java +++ b/src/main/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/Solution.java @@ -6,7 +6,9 @@ public class Solution { public int longestSubarray(int[] nums) { int n = nums.length; - if (n < 3) return n; + if (n < 3) { + return n; + } int i = 0; int len1 = 0; int len2 = 0; @@ -25,8 +27,9 @@ public int longestSubarray(int[] nums) { } len2 = j - i + 1; ans = Math.max(len2 + 1, ans); - if (l <= r || (len1 > 1 && nums[i - 2] <= nums[i])) + if (l <= r || (len1 > 1 && nums[i - 2] <= nums[i])) { ans = Math.max(len1 + len2, ans); + } i = j; } else { len2 = 0; From 777b9979e60bd4f92bb4365363e709b2e713af6a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:30:10 +0300 Subject: [PATCH 07/10] Update readme.md --- .../readme.md | 56 ++----------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md index 4475a4a48..4294a424d 100644 --- a/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md +++ b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/readme.md @@ -26,55 +26,11 @@ Return the **maximum** score achievable without exceeding a total cost of `k`, o The optimal path is: -Cell - -grid[i][j] - -Score - -Total -Score - -Cost - -Total -Cost - -(0, 0) - -0 - -0 - -0 - -0 - -0 - -(1, 0) - -2 - -2 - -2 - -1 - -1 - -(1, 1) - -0 - -0 - -2 - -0 - -1 +| Cell | grid[i][j] | Score | Total Score | Cost | Total Cost | +|--------|------------|-------|-------------|------|------------| +| (0, 0) | 0 | 0 | 0 | 0 | 0 | +| (1, 0) | 2 | 2 | 2 | 1 | 1 | +| (1, 1) | 0 | 0 | 2 | 0 | 1 | Thus, the maximum possible score is 2. @@ -93,4 +49,4 @@ There is no path that reaches cell `(1, 1)` without exceeding cost k. Thus, the * `1 <= m, n <= 200` * 0 <= k <= 103 * `grid[0][0] == 0` -* `0 <= grid[i][j] <= 2` \ No newline at end of file +* `0 <= grid[i][j] <= 2` From 9010443ba39e828254faef188cb122bd07d56aa9 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:31:03 +0300 Subject: [PATCH 08/10] Initialize DP array for maximum path score calculation --- .../s3742_maximum_path_score_in_a_grid/Solution.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java index e8f2338f9..f13cf823e 100644 --- a/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java +++ b/src/main/java/g3701_3800/s3742_maximum_path_score_in_a_grid/Solution.java @@ -9,7 +9,6 @@ public int maxPathScore(int[][] grid, int k) { int m = grid[0].length; int mxc = Math.min(k + 1, n + m + 5); int[][][] dp = new int[n][m][mxc]; - for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int c = 0; c < mxc; c++) { @@ -17,16 +16,13 @@ public int maxPathScore(int[][] grid, int k) { } } } - dp[0][0][0] = 0; - for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int c = 0; c < mxc; c++) { if (dp[i][j][c] == -1) { continue; } - // move right if (j + 1 < m) { int cost = c + (grid[i][j + 1] > 0 ? 1 : 0); @@ -35,7 +31,6 @@ public int maxPathScore(int[][] grid, int k) { Math.max(dp[i][j + 1][cost], dp[i][j][c] + grid[i][j + 1]); } } - // move down if (i + 1 < n) { int cost = c + (grid[i + 1][j] > 0 ? 1 : 0); @@ -47,7 +42,6 @@ public int maxPathScore(int[][] grid, int k) { } } } - int ans = -1; for (int c = 0; c < mxc; c++) { ans = Math.max(ans, dp[n - 1][m - 1][c]); From 2ce69e82ab0bba7428f4603eebf58a7f88cb6246 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:31:47 +0300 Subject: [PATCH 09/10] Refactor countDistinct method in Solution class Refactor countDistinct method to improve readability and efficiency. --- .../Solution.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java b/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java index c9584b96c..244b4c74c 100644 --- a/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java +++ b/src/main/java/g3701_3800/s3747_count_distinct_integers_after_removing_zeros/Solution.java @@ -7,19 +7,15 @@ public class Solution { public long countDistinct(long n) { String digits = Long.toString(n); int m = digits.length(); - long[] power9 = new long[m + 1]; power9[0] = 1; for (int i = 1; i <= m; i++) { power9[i] = power9[i - 1] * 9; } - long total = 0; - for (int length = 1; length < m; length++) { total += power9[length]; } - for (int idx = 0; idx < m; idx++) { int d = digits.charAt(idx) - '0'; if (d == 0) { @@ -27,7 +23,6 @@ public long countDistinct(long n) { } total += (d - 1) * power9[m - idx - 1]; } - return total + 1; } } From 6da917b5818a666b33acee5c79b3a2c7ade3ff66 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Apr 2026 14:34:48 +0300 Subject: [PATCH 10/10] Add test case for longestSubarray method --- .../SolutionTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java b/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java index ece28621b..f93e28135 100644 --- a/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java +++ b/src/test/java/g3701_3800/s3738_longest_non_decreasing_subarray_after_replacing_at_most_one_element/SolutionTest.java @@ -20,4 +20,9 @@ void longestSubarray2() { void longestSubarray3() { assertThat(new Solution().longestSubarray(new int[] {1, 5, 3, 4, 6}), equalTo(5)); } + + @Test + void longestSubarray4() { + assertThat(new Solution().longestSubarray(new int[] {1, 5}), equalTo(2)); + } }