From 6573733316c9dd8ea9d2c8730b0c5500f6c1c0bd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Nov 2026 15:05:32 +0200 Subject: [PATCH 1/6] Added tasks 4015-4021 --- .../Solution.java | 42 +++++ .../s4015_weighted_sum_of_a_tree/readme.md | 70 ++++++++ .../Solution.java | 54 ++++++ .../readme.md | 69 ++++++++ .../s4017_peaks_in_array_ii/Solution.java | 167 ++++++++++++++++++ .../s4017_peaks_in_array_ii/readme.md | 74 ++++++++ .../s4020_elevator_requests_i/Solution.java | 15 ++ .../s4020_elevator_requests_i/readme.md | 51 ++++++ .../Solution.java | 42 +++++ .../readme.md | 42 +++++ .../SolutionTest.java | 23 +++ .../SolutionTest.java | 25 +++ .../s4017_peaks_in_array_ii/SolutionTest.java | 38 ++++ .../SolutionTest.java | 18 ++ .../SolutionTest.java | 18 ++ 15 files changed, 748 insertions(+) create mode 100644 src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/Solution.java create mode 100644 src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/readme.md create mode 100644 src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java create mode 100644 src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/readme.md create mode 100644 src/main/java/g4001_4100/s4017_peaks_in_array_ii/Solution.java create mode 100644 src/main/java/g4001_4100/s4017_peaks_in_array_ii/readme.md create mode 100644 src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java create mode 100644 src/main/java/g4001_4100/s4020_elevator_requests_i/readme.md create mode 100644 src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/Solution.java create mode 100644 src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/readme.md create mode 100644 src/test/java/g4001_4100/s4015_weighted_sum_of_a_tree/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4020_elevator_requests_i/SolutionTest.java create mode 100644 src/test/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/SolutionTest.java diff --git a/src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/Solution.java b/src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/Solution.java new file mode 100644 index 000000000..fd89089fd --- /dev/null +++ b/src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/Solution.java @@ -0,0 +1,42 @@ +package g4001_4100.s4015_weighted_sum_of_a_tree; + +// #Medium #Array #Tree #Senior #Weekly_Contest_514 #Depth_First_Search +// #2026_11_19_Time_5_ms_(100.00%)_Space_107.29_MB_(94.38%) + +public class Solution { + public long weightedSum(int[] parent, int[] nums) { + int n = parent.length; + int[] depth = new int[n]; + depth[0] = 1; + for (int i = 1; i < n; i++) { + if (depth[i] != 0) { + continue; + } + int currDepth = getDepth(parent, depth, i); + int curr = i; + while (depth[curr] == 0) { + depth[curr] = currDepth; + curr = parent[curr]; + currDepth -= 1; + } + } + long res = 0; + int height = 0; + for (int i = 0; i < n; i++) { + height = Math.max(height, depth[i]); + } + for (int i = 0; i < n; i++) { + res += (long) (height - depth[i] + 1) * nums[i]; + } + return res; + } + + private int getDepth(int[] parent, int[] currDepth, int node) { + int add = 0; + while (currDepth[node] == 0) { + node = parent[node]; + add += 1; + } + return currDepth[node] + add; + } +} diff --git a/src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/readme.md b/src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/readme.md new file mode 100644 index 000000000..02dce8494 --- /dev/null +++ b/src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/readme.md @@ -0,0 +1,70 @@ +4015\. Weighted Sum of a Tree + +Medium + +You are given an integer array `parent` of length `n` representing a rooted tree with nodes labeled from 0 to `n - 1`. + +The tree is **rooted** at node 0, so `parent[0] = -1`. For each node `i` where `1 <= i <= n - 1`, `parent[i]` denotes the parent of node `i`. + +You are also given an integer array `nums` of length `n`, where `nums[i]` denotes the value of node `i`. + +The weight of a node `i` at depth `d` is `nums[i] * (h - d + 1)`, where `h` is the height of the tree. + +Return the **sum** of the weights of all nodes in the tree. + +The **depth** of a node is the number of nodes on the path from the root to that node, inclusive, with the root having depth 1. + +The **height** of the tree is the maximum depth among all nodes in the tree. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2026/06/09/t1.png) + +**Input:** parent = [-1,0,0,0,2,2], nums = [5,2,3,1,4,6] + +**Output:** 37 + +**Explanation:** + +The height of the tree is 3. + +| Node | `nums[i]` | Depth (`d`) | Weight | +| --- | --- | --- | --- | +| 0 | 5 | 1 | `5 * (3 - 1 + 1) = 15` | +| 1 | 2 | 2 | `2 * (3 - 2 + 1) = 4` | +| 2 | 3 | 2 | `3 * (3 - 2 + 1) = 6` | +| 3 | 1 | 2 | `1 * (3 - 2 + 1) = 2` | +| 4 | 4 | 3 | `4 * (3 - 3 + 1) = 4` | +| 5 | 6 | 3 | `6 * (3 - 3 + 1) = 6` | + +The sum of all node weights is `15 + 4 + 6 + 2 + 4 + 6 = 37`. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2026/06/09/t2.png) + +**Input:** parent = [-1,0,1,2], nums = [1,2,3,4] + +**Output:** 20 + +**Explanation:** + +The height of the tree is 4. + +| Node | `nums[i]` | Depth (`d`) | Weight | +| --- | --- | --- | --- | +| 0 | 1 | 1 | `1 * (4 - 1 + 1) = 4` | +| 1 | 2 | 2 | `2 * (4 - 2 + 1) = 6` | +| 2 | 3 | 3 | `3 * (4 - 3 + 1) = 6` | +| 3 | 4 | 4 | `4 * (4 - 4 + 1) = 4` | + +The sum of all node weights is `4 + 6 + 6 + 4 = 20`. + +**Constraints:** + +* 1 <= n <= 105 +* `n == parent.length == nums.length` +* `parent[0] == -1` +* `0 <= parent[i] <= n - 1` for all `i` in `[1, n - 1]` +* 1 <= nums[i] <= 106 +* The input is generated such that the array `parent` represents a valid tree rooted at node 0. \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java new file mode 100644 index 000000000..46ab790bf --- /dev/null +++ b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java @@ -0,0 +1,54 @@ +package g4001_4100.s4016_maximum_area_of_two_non_overlapping_square_submatrices; + +// #Medium #Array #Dynamic_Programming #Binary_Search #Matrix #Staff #Weekly_Contest_514 +// #2026_11_19_Time_12_ms_(98.63%)_Space_183.92_MB_(94.52%) + +public class Solution { + public int maxArea(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[][] dp = new int[m][n]; + int[] top = new int[m]; + int[] bottom = new int[m]; + int[] left = new int[n]; + int[] right = new int[n]; + for (int r = 0; r < m; r++) { + for (int c = 0; c < n; c++) { + if (mat[r][c] == 0) { + continue; + } + if (r == 0 || c == 0) { + dp[r][c] = 1; + } else { + dp[r][c] = 1 + Math.min(dp[r - 1][c - 1], Math.min(dp[r - 1][c], dp[r][c - 1])); + } + int k = dp[r][c]; + top[r] = Math.max(top[r], k); + left[c] = Math.max(left[c], k); + bottom[r - k + 1] = Math.max(bottom[r - k + 1], k); + + right[c - k + 1] = Math.max(right[c - k + 1], k); + } + } + for (int r = 1; r < m; r++) { + top[r] = Math.max(top[r], top[r - 1]); + } + for (int c = 1; c < n; c++) { + left[c] = Math.max(left[c], left[c - 1]); + } + for (int r = m - 2; r >= 0; r--) { + bottom[r] = Math.max(bottom[r], bottom[r + 1]); + } + for (int c = n - 2; c >= 0; c--) { + right[c] = Math.max(right[c], right[c + 1]); + } + int best = 0; + for (int r = 0; r < m - 1; r++) { + best = Math.max(best, Math.min(top[r], bottom[r + 1])); + } + for (int c = 0; c < n - 1; c++) { + best = Math.max(best, Math.min(left[c], right[c + 1])); + } + return best * best; + } +} diff --git a/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/readme.md b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/readme.md new file mode 100644 index 000000000..dc9a93551 --- /dev/null +++ b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/readme.md @@ -0,0 +1,69 @@ +4016\. Maximum Area of Two Non-Overlapping Square Submatrices + +Medium + +You are given a 2D integer matrix `mat` of size `m × n`, where: + +* `mat[r][c] == 1` means the cell at row `r` and column `c` is usable. +* `mat[r][c] == 0` means it is not usable. + +Your task is to find **two submatrices** that satisfy the following conditions: + +* Both submatrices must be squares of the same side length `k`. +* The two submatrices must not share any cell. +* Each submatrix can only cover cells where `mat[r][c] == 1`. + +Return the **maximum possible area** of each of the two squares. If it is not possible to choose two such squares, return 0. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2026/06/13/image.png) + +**Input:** mat = [[1,1,1,0],[1,1,1,1],[0,0,1,1]] + +**Output:** 4 + +**Explanation:** + +The largest equal non-overlapping squares have side length `k = 2` with area 4. + +* First square starts at top-left `(0, 0)` and covers cells `(0, 0)`, `(0, 1)`, `(1, 0)`, and `(1, 1)`. +* Second square starts at top-left `(1, 2)` and covers cells `(1, 2)`, `(1, 3)`, `(2, 2)`, and `(2, 3)`. + +Thus, the answer is 4. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2026/06/13/screenshot-2026-06-13-at-83728pm.png) + +**Input:** mat = [[0,1],[1,0]] + +**Output:** 1 + +**Explanation:** + +The largest equal non-overlapping squares have side length `k = 1` with area 1. + +* First square starts at top-left `(0, 1)` and covers cell `(0, 1)`. +* Second square starts at top-left `(1, 0)` and covers cell `(1, 0)`. + +Thus, the answer is 1. + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2026/06/13/screenshot-2026-06-13-at-83751pm.png) + +**Input:** mat = [[0,0],[0,1]] + +**Output:** 0 + +**Explanation:** + +There is only one usable cell, so it is impossible to choose two non-overlapping squares. Thus, the answer is 0. + +**Constraints:** + +* `mat.length == m` +* `mat[i].length == n` +* `1 <= m, n <= 500` +* `mat[i][j]` is either 0 or 1. \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4017_peaks_in_array_ii/Solution.java b/src/main/java/g4001_4100/s4017_peaks_in_array_ii/Solution.java new file mode 100644 index 000000000..a292139d7 --- /dev/null +++ b/src/main/java/g4001_4100/s4017_peaks_in_array_ii/Solution.java @@ -0,0 +1,167 @@ +package g4001_4100.s4017_peaks_in_array_ii; + +// #Hard #Array #Divide_and_Conquer #Segment_Tree #Senior_Staff #Weekly_Contest_514 +// #2026_11_19_Time_139_ms_(92.31%)_Space_193.66_MB_(100.00%) + +public class Solution { + private int[] cnt; + private int[] first; + private int[] last; + private long[] inner; + private int qCnt; + private int qFirst; + private int qLast; + private long qInner; + + public long[] countOfPeaks(int[] nums, int[][] queries) { + int n = nums.length; + int type1Count = 0; + for (int[] q : queries) { + if (q[0] == 1) { + type1Count++; + } + } + long[] ans = new long[type1Count]; + if (n < 3) { + return ans; + } + int m = n - 2; + cnt = new int[4 * m]; + first = new int[4 * m]; + last = new int[4 * m]; + inner = new long[4 * m]; + build(1, 1, m, nums); + int ansIdx = 0; + for (int[] q : queries) { + if (q[0] == 1) { + int l = q[1]; + int r = q[2]; + if (r - l + 1 < 3) { + ans[ansIdx++] = 0L; + } else { + qCnt = 0; + qFirst = -1; + qLast = -1; + qInner = 0L; + query(1, 1, m, l + 1, r - 1); + if (qCnt == 0) { + ans[ansIdx++] = 0L; + } else { + long totalSub = tri((long) r - l + 1); + long leftLen = (long) qFirst - l + 1; + long rightLen = (long) r - qLast + 1; + long nonPeak = (tri(leftLen) - 1L) + qInner + tri(rightLen); + ans[ansIdx++] = totalSub - nonPeak; + } + } + } else { + int idx = q[1]; + nums[idx] = q[2]; + int start = Math.max(1, idx - 1); + int end = Math.min(m, idx + 1); + for (int pos = start; pos <= end; pos++) { + update(1, 1, m, pos, isPeak(nums, pos)); + } + } + } + return ans; + } + + private boolean isPeak(int[] nums, int i) { + return nums[i] > nums[i - 1] && nums[i] > nums[i + 1]; + } + + private long tri(long len) { + return len * (len + 1L) / 2L; + } + + private void build(int node, int tl, int tr, int[] nums) { + if (tl == tr) { + if (isPeak(nums, tl)) { + cnt[node] = 1; + first[node] = tl; + last[node] = tl; + } else { + first[node] = -1; + last[node] = -1; + } + return; + } + int mid = (tl + tr) >>> 1; + build(node << 1, tl, mid, nums); + build(node << 1 | 1, mid + 1, tr, nums); + pull(node); + } + + private void update(int node, int tl, int tr, int pos, boolean peak) { + if (tl == tr) { + if (peak) { + cnt[node] = 1; + first[node] = tl; + last[node] = tl; + } else { + cnt[node] = 0; + first[node] = -1; + last[node] = -1; + } + inner[node] = 0L; + return; + } + int mid = (tl + tr) >>> 1; + if (pos <= mid) { + update(node << 1, tl, mid, pos, peak); + } else { + update(node << 1 | 1, mid + 1, tr, pos, peak); + } + pull(node); + } + + private void pull(int node) { + int lc = node << 1; + int rc = node << 1 | 1; + if (cnt[lc] == 0) { + cnt[node] = cnt[rc]; + first[node] = first[rc]; + last[node] = last[rc]; + inner[node] = inner[rc]; + } else if (cnt[rc] == 0) { + cnt[node] = cnt[lc]; + first[node] = first[lc]; + last[node] = last[lc]; + inner[node] = inner[lc]; + } else { + cnt[node] = cnt[lc] + cnt[rc]; + first[node] = first[lc]; + last[node] = last[rc]; + long gap = (long) first[rc] - last[lc] + 1L; + inner[node] = inner[lc] + inner[rc] + tri(gap) - 1L; + } + } + + private void query(int node, int tl, int tr, int ql, int qr) { + if (ql <= tl && tr <= qr) { + if (cnt[node] == 0) { + return; + } + if (qCnt == 0) { + qCnt = cnt[node]; + qFirst = first[node]; + qLast = last[node]; + qInner = inner[node]; + } else { + long gap = (long) first[node] - qLast + 1L; + qInner += inner[node] + tri(gap) - 1L; + qCnt += cnt[node]; + qLast = last[node]; + } + return; + } + int mid = (tl + tr) >>> 1; + if (ql <= mid) { + query(node << 1, tl, mid, ql, qr); + } + if (qr > mid) { + query(node << 1 | 1, mid + 1, tr, ql, qr); + } + } +} diff --git a/src/main/java/g4001_4100/s4017_peaks_in_array_ii/readme.md b/src/main/java/g4001_4100/s4017_peaks_in_array_ii/readme.md new file mode 100644 index 000000000..0e45f8f81 --- /dev/null +++ b/src/main/java/g4001_4100/s4017_peaks_in_array_ii/readme.md @@ -0,0 +1,74 @@ +4017\. Peaks in Array II + +Hard + +You are given an integer array `nums` of length `n` and a 2D integer array `queries`. + +A **non-empty subarrays** `nums[i..j]` is called a **peak subarray** if: + +* Its length is **at least** 3. +* There exists an index `k` such that `i < k < j` and: + * `nums[k] > nums[k - 1]` + * `nums[k] > nums[k + 1]` + +You have to process queries of two types: + +* [1, li, ri]: Calculate the number of **peak subarrays** fully contained within nums[li..ri]. +* [2, indexi, vali]: Update nums[indexi] to vali. This update applies to all subsequent queries. + +Return an array `answer`, where `answer[i]` is the answer to the ith query of type 1 in the order they appear. + +**Example 1:** + +**Input:** nums = [1,3,2,4], queries = [[1,0,3],[2,1,1],[1,0,3]] + +**Output:** [2,0] + +**Explanation:** + +* Query `[1, 0, 3]`: + * `[1, 3, 2]`: choose `k = 1`. Then `nums[k] = 3`, `nums[k - 1] = 1`, and `nums[k + 1] = 2`. Since `3 > 1` and `3 > 2`, this is a peak subarray. + * `[1, 3, 2, 4]`: choose `k = 1`. Then `nums[k] = 3`, `nums[k - 1] = 1`, and `nums[k + 1] = 2`. Since `3 > 1` and `3 > 2`, this is a peak subarray. +* Query `[2, 1, 1]`: Update `nums[1]` to 1. The array becomes `[1, 1, 2, 4]`. +* Query `[1, 0, 3]`: There are no peak subarrays now. +* Thus, `answer = [2, 0]`. + +**Example 2:** + +**Input:** nums = [9,8,9,8], queries = [[1,1,3],[2,2,1],[1,0,2]] + +**Output:** [1,0] + +**Explanation:** + +* Query `[1, 1, 3]`: + * `nums[1..3] = [8, 9, 8]`: choose `k = 2`. Then `nums[k] = 9`, `nums[k - 1] = 8`, and `nums[k + 1] = 8`. Since `9 > 8` and `9 > 8`, this is a peak subarray. +* Query `[2, 2, 1]`: Update `nums[2]` to 1. The array becomes `[9, 8, 1, 8]`. +* Query `[1, 0, 2]`: There are no peak subarrays. +* Thus, `answer = [1, 0]`. + +**Example 3:** + +**Input:** nums = [3,6,2,7,1], queries = [[1,1,3],[2,3,0],[1,0,4]] + +**Output:** [0,3] + +**Explanation:** + +* Query `[1, 1, 3]`: The only subarray of length at least 3 is `[6, 2, 7]`. Its only possible peak index is `k = 2`, but `nums[2] = 2` is less than both `nums[1] = 6` and `nums[3] = 7`, so it is not a peak subarray. +* Query `[2, 3, 0]`: Update `nums[3]` to 0. The array becomes `[3, 6, 2, 0, 1]`. +* Query `[1, 0, 4]`: + * `[3, 6, 2]`: choose `k = 1`. Then `nums[k] = 6`, `nums[k - 1] = 3`, and `nums[k + 1] = 2`. Since `6 > 3` and `6 > 2`, this is a peak subarray. + * `[3, 6, 2, 0]`: choose `k = 1`. Then `nums[k] = 6`, `nums[k - 1] = 3`, and `nums[k + 1] = 2`. Since `6 > 3` and `6 > 2`, this is a peak subarray. + * `[3, 6, 2, 0, 1]`: choose `k = 1`. Then `nums[k] = 6`, `nums[k - 1] = 3`, and `nums[k + 1] = 2`. Since `6 > 3` and `6 > 2`, this is a peak subarray. +* Thus, `answer = [0, 3]`. + +**Constraints:** + +* 3 <= n == nums.length <= 105 +* 0 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* queries[i] = [1, li, ri] or queries[i] = [2, indexi, vali] +* 0 <= li < ri <= n - 1 +* 0 <= indexi <= n - 1 +* 0 <= vali <= 105 \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java b/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java new file mode 100644 index 000000000..872114857 --- /dev/null +++ b/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java @@ -0,0 +1,15 @@ +package g4001_4100.s4020_elevator_requests_i; + +// #Easy #Array #Simulation #Mid_Level #Biweekly_Contest_189 +// #2026_11_19_Time_1_ms_(99.23%)_Space_44.25_MB_(70.81%) + +public class Solution { + public int elevatorRequests(int n, int[] requests) { + int time = requests[0]; + for (int i = 1; i < requests.length; i++) { + int currDiff = Math.abs(requests[i] - requests[i - 1]); + time += currDiff; + } + return time; + } +} diff --git a/src/main/java/g4001_4100/s4020_elevator_requests_i/readme.md b/src/main/java/g4001_4100/s4020_elevator_requests_i/readme.md new file mode 100644 index 000000000..9018e1bdc --- /dev/null +++ b/src/main/java/g4001_4100/s4020_elevator_requests_i/readme.md @@ -0,0 +1,51 @@ +4020\. Elevator Requests I + +Easy + +You are given an integer `n` denoting the number of floors in a building, where the floors are numbered from 0 to `n - 1`. + +You are also given an integer array `requests`, where `requests` represents the sequence of floor requests. + +An elevator starts at floor 0 and follows these rules: + +* The elevator moves one floor per second. +* The elevator serves requests in the given order. +* If the elevator is already on the requested floor, no movement is needed. +* After serving a request, the elevator immediately starts moving toward the next request. + +Return the **total time** in seconds required to serve all requests. + +**Example 1:** + +**Input:** n = 5, requests = [2,1,4,3] + +**Output:** 7 + +**Explanation:** + +* `requests[0] = 2`: Moving from floor 0 to floor 2 takes 2 seconds. +* `requests[1] = 1`: Moving from floor 2 to floor 1 takes 1 second. +* `requests[2] = 4`: Moving from floor 1 to floor 4 takes 3 seconds. +* `requests[3] = 3`: Moving from floor 4 to floor 3 takes 1 second. + +The total time required is `2 + 1 + 3 + 1 = 7` seconds. + +**Example 2:** + +**Input:** n = 3, requests = [2,0,0] + +**Output:** 4 + +**Explanation:** + +* `requests[0] = 2`: Moving from floor 0 to floor 2 takes 2 seconds. +* `requests[1] = 0`: Moving from floor 2 to floor 0 takes 2 seconds. +* `requests[2] = 0`: No movement is needed. + +The total time required is `2 + 2 + 0 = 4` seconds. + +**Constraints:** + +* `1 <= n <= 100` +* `1 <= requests.length <= 100` +* `0 <= requests[i] <= n - 1` \ No newline at end of file diff --git a/src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/Solution.java b/src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/Solution.java new file mode 100644 index 000000000..5bb1c9f93 --- /dev/null +++ b/src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/Solution.java @@ -0,0 +1,42 @@ +package g4001_4100.s4021_minimum_operations_to_make_a_rotated_palindrome_i; + +// #Medium #String #Math #Enumeration #Staff #Biweekly_Contest_189 +// #2026_11_19_Time_40_ms_(98.85%)_Space_46.02_MB_(33.33%) + +public class Solution { + public int minOperations(String s) { + char[] chars = s.toCharArray(); + int n = s.length(); + int[][] dist = new int[26][26]; + for (int a = 0; a < 26; ++a) { + for (int b = 0; b < 26; ++b) { + int diff = Math.abs(a - b); + dist[a][b] = Math.min(diff, 26 - diff); + } + } + int[] vals = new int[n]; + for (int i = 0; i < n; ++i) { + vals[i] = chars[i] - 97; + } + int mid = n >> 1; + int min = 0; + for (int i = 0; i < mid; ++i) { + min += dist[vals[i]][vals[n - 1 - i]]; + } + int[] diff = new int[n << 1]; + for (int i = 0; i < 2 * n; ++i) { + diff[i] = vals[i % n]; + } + for (int shift = 1; shift < n >> (1 - (n & 1)) && shift < min; ++shift) { + int curr = shift; + for (int i = 0; i < mid; ++i) { + curr += dist[diff[shift + i]][diff[shift + n - 1 - i]]; + if (curr >= min) { + break; + } + } + min = Math.min(min, curr); + } + return min; + } +} diff --git a/src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/readme.md b/src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/readme.md new file mode 100644 index 000000000..48486342a --- /dev/null +++ b/src/main/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/readme.md @@ -0,0 +1,42 @@ +4021\. Minimum Operations to Make a Rotated Palindrome I + +Medium + +You are given a string `s` consisting of lowercase English letters. + +You can perform the following operations any number of times (including zero) and in any order: + +* **Increment**: Choose any index `i` and replace `s[i]` with the next lowercase English letter. The letter after `'z'` is `'a'`. +* **Left rotate**: Move the first character of the string to the end. + +Return the **minimum** number of operations required to make `s` a palindrome. + +**Example 1:** + +**Input:** s = "abc" + +**Output:** 2 + +**Explanation:** + +One optimal solution: + +* Left rotate the string: `"abc" -> "bca"`. +* Increment `'a'` to `'b'`: `"bca" -> "bcb"`. +* `"bcb"` is a palindrome. Thus, the answer is 2. + +**Example 2:** + +**Input:** s = "yb" + +**Output:** 3 + +**Explanation:** + +* Increment the first character three times: `"yb" -> "zb" -> "ab" -> "bb"`. +* `"bb"` is a palindrome. Thus, the answer is 3. + +**Constraints:** + +* `2 <= s.length <= 2000` +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/test/java/g4001_4100/s4015_weighted_sum_of_a_tree/SolutionTest.java b/src/test/java/g4001_4100/s4015_weighted_sum_of_a_tree/SolutionTest.java new file mode 100644 index 000000000..17b9dc1e6 --- /dev/null +++ b/src/test/java/g4001_4100/s4015_weighted_sum_of_a_tree/SolutionTest.java @@ -0,0 +1,23 @@ +package g4001_4100.s4015_weighted_sum_of_a_tree; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void weightedSum() { + assertThat( + new Solution() + .weightedSum(new int[] {-1, 0, 0, 0, 2, 2}, new int[] {5, 2, 3, 1, 4, 6}), + equalTo(37L)); + } + + @Test + void weightedSum2() { + assertThat( + new Solution().weightedSum(new int[] {-1, 0, 1, 2}, new int[] {1, 2, 3, 4}), + equalTo(20L)); + } +} diff --git a/src/test/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/SolutionTest.java b/src/test/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/SolutionTest.java new file mode 100644 index 000000000..d90edf546 --- /dev/null +++ b/src/test/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/SolutionTest.java @@ -0,0 +1,25 @@ +package g4001_4100.s4016_maximum_area_of_two_non_overlapping_square_submatrices; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxArea() { + assertThat( + new Solution().maxArea(new int[][] {{1, 1, 1, 0}, {1, 1, 1, 1}, {0, 0, 1, 1}}), + equalTo(4)); + } + + @Test + void maxArea2() { + assertThat(new Solution().maxArea(new int[][] {{0, 1}, {1, 0}}), equalTo(1)); + } + + @Test + void maxArea3() { + assertThat(new Solution().maxArea(new int[][] {{0, 0}, {0, 1}}), equalTo(0)); + } +} diff --git a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java new file mode 100644 index 000000000..95bef85e9 --- /dev/null +++ b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java @@ -0,0 +1,38 @@ +package g4001_4100.s4017_peaks_in_array_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countOfPeaks() { + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 3, 2, 4}, + new int[][] {{1, 0, 3}, {2, 1, 1}, {1, 0, 3}}), + equalTo(new long[] {2L, 0L})); + } + + @Test + void countOfPeaks2() { + assertThat( + new Solution() + .countOfPeaks( + new int[] {9, 8, 9, 8}, + new int[][] {{1, 1, 3}, {2, 2, 1}, {1, 0, 2}}), + equalTo(new long[] {1L, 0L})); + } + + @Test + void countOfPeaks3() { + assertThat( + new Solution() + .countOfPeaks( + new int[] {3, 6, 2, 7, 1}, + new int[][] {{1, 1, 3}, {2, 3, 0}, {1, 0, 4}}), + equalTo(new long[] {0L, 3L})); + } +} diff --git a/src/test/java/g4001_4100/s4020_elevator_requests_i/SolutionTest.java b/src/test/java/g4001_4100/s4020_elevator_requests_i/SolutionTest.java new file mode 100644 index 000000000..f5dc07eb2 --- /dev/null +++ b/src/test/java/g4001_4100/s4020_elevator_requests_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g4001_4100.s4020_elevator_requests_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void elevatorRequests() { + assertThat(new Solution().elevatorRequests(5, new int[] {2, 1, 4, 3}), equalTo(7)); + } + + @Test + void elevatorRequests2() { + assertThat(new Solution().elevatorRequests(3, new int[] {2, 0, 0}), equalTo(4)); + } +} diff --git a/src/test/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/SolutionTest.java b/src/test/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/SolutionTest.java new file mode 100644 index 000000000..7d1a75379 --- /dev/null +++ b/src/test/java/g4001_4100/s4021_minimum_operations_to_make_a_rotated_palindrome_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g4001_4100.s4021_minimum_operations_to_make_a_rotated_palindrome_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minOperations() { + assertThat(new Solution().minOperations("abc"), equalTo(2)); + } + + @Test + void minOperations2() { + assertThat(new Solution().minOperations("yb"), equalTo(3)); + } +} From 4b0a19fa0581f907e8d65ff3504fa49a4ac15e4d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Nov 2026 15:16:57 +0200 Subject: [PATCH 2/6] Fixed sonar --- .../Solution.java | 4 ++-- .../java/g4001_4100/s4020_elevator_requests_i/Solution.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java index 46ab790bf..e7c737e92 100644 --- a/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java +++ b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java @@ -44,10 +44,10 @@ public int maxArea(int[][] mat) { } int best = 0; for (int r = 0; r < m - 1; r++) { - best = Math.max(best, Math.min(top[r], bottom[r + 1])); + best = Math.champ(best, Math.min(top[r], bottom[r + 1])); } for (int c = 0; c < n - 1; c++) { - best = Math.max(best, Math.min(left[c], right[c + 1])); + best = Math.champ(best, Math.min(left[c], right[c + 1])); } return best * best; } diff --git a/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java b/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java index 872114857..25e7f76f7 100644 --- a/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java +++ b/src/main/java/g4001_4100/s4020_elevator_requests_i/Solution.java @@ -3,6 +3,7 @@ // #Easy #Array #Simulation #Mid_Level #Biweekly_Contest_189 // #2026_11_19_Time_1_ms_(99.23%)_Space_44.25_MB_(70.81%) +@SuppressWarnings("java:S1172") public class Solution { public int elevatorRequests(int n, int[] requests) { int time = requests[0]; From d6162add8be77ad669034090f400bf30c567baf9 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Nov 2026 15:22:19 +0200 Subject: [PATCH 3/6] Fixed sonar --- .../Solution.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java index e7c737e92..fba901f3a 100644 --- a/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java +++ b/src/main/java/g4001_4100/s4016_maximum_area_of_two_non_overlapping_square_submatrices/Solution.java @@ -3,6 +3,7 @@ // #Medium #Array #Dynamic_Programming #Binary_Search #Matrix #Staff #Weekly_Contest_514 // #2026_11_19_Time_12_ms_(98.63%)_Space_183.92_MB_(94.52%) +@SuppressWarnings("java:S6885") public class Solution { public int maxArea(int[][] mat) { int m = mat.length; @@ -44,10 +45,10 @@ public int maxArea(int[][] mat) { } int best = 0; for (int r = 0; r < m - 1; r++) { - best = Math.champ(best, Math.min(top[r], bottom[r + 1])); + best = Math.max(best, Math.min(top[r], bottom[r + 1])); } for (int c = 0; c < n - 1; c++) { - best = Math.champ(best, Math.min(left[c], right[c + 1])); + best = Math.max(best, Math.min(left[c], right[c + 1])); } return best * best; } From 037a3dfbc668fec249ac6422b61f5f1037d0d330 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Nov 2026 15:32:09 +0200 Subject: [PATCH 4/6] Added tests --- .../s4017_peaks_in_array_ii/SolutionTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java index 95bef85e9..53d440105 100644 --- a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java @@ -35,4 +35,83 @@ void countOfPeaks3() { new int[][] {{1, 1, 3}, {2, 3, 0}, {1, 0, 4}}), equalTo(new long[] {0L, 3L})); } + + @Test + void countOfPeaks4() { + // n < 3, no peaks can exist + assertThat( + new Solution().countOfPeaks(new int[] {5, 10}, new int[][] {{1, 0, 1}}), + equalTo(new long[] {0L})); + } + + @Test + void countOfPeaks5() { + assertThat( + new Solution().countOfPeaks(new int[] {7}, new int[][] {{1, 0, 0}}), + equalTo(new long[] {0L})); + } + + @Test + void countOfPeaks6() { + // Range with fewer than 3 elements yields 0 + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 3, 2, 4, 1}, new int[][] {{1, 0, 1}, {1, 2, 3}}), + equalTo(new long[] {0L, 0L})); + } + + @Test + void countOfPeaks7() { + assertThat( + new Solution().countOfPeaks(new int[] {1, 2, 3, 4, 5}, new int[][] {{1, 0, 4}}), + equalTo(new long[] {0L})); + } + + @Test + void countOfPeaks8() { + assertThat( + new Solution().countOfPeaks(new int[] {5, 4, 3, 2, 1}, new int[][] {{1, 0, 4}}), + equalTo(new long[] {0L})); + } + + @Test + void countOfPeaks9() { + // No type-1 queries, result array is empty + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 3, 2, 4, 1}, new int[][] {{2, 1, 10}, {2, 3, 0}}), + equalTo(new long[] {})); + } + + @Test + void countOfPeaks10() { + // Initially no peak in [0,3]; update makes index 2 a peak + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 2, 3, 4}, + new int[][] {{1, 0, 3}, {2, 2, 10}, {1, 0, 3}}), + equalTo(new long[] {0L, 2L})); + } + + @Test + void countOfPeaks11() { + // Initially a peak at index 1; update removes it + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 5, 1, 2}, + new int[][] {{1, 0, 3}, {2, 1, 0}, {1, 0, 3}}), + equalTo(new long[] {2L, 0L})); + } + + @Test + void countOfPeaks12() { + // All equal -> no peaks (peak requires strictly greater) + assertThat( + new Solution().countOfPeaks(new int[] {3, 3, 3, 3, 3}, new int[][] {{1, 0, 4}}), + equalTo(new long[] {0L})); + } } From 01c8e3f69f5c3279c5ba670cd4af75e3aa6650e3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Nov 2026 15:44:21 +0200 Subject: [PATCH 5/6] Added tests --- .../s4017_peaks_in_array_ii/SolutionTest.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java index 53d440105..7e87948b8 100644 --- a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java @@ -77,6 +77,15 @@ void countOfPeaks8() { @Test void countOfPeaks9() { + // Array: 1 5 1 5 1 5 1 -> peaks at indices 1, 3, 5 + assertThat( + new Solution() + .countOfPeaks(new int[] {1, 5, 1, 5, 1, 5, 1}, new int[][] {{1, 0, 6}}), + equalTo(new long[] {13L})); + } + + @Test + void countOfPeaks10() { // No type-1 queries, result array is empty assertThat( new Solution() @@ -86,7 +95,7 @@ void countOfPeaks9() { } @Test - void countOfPeaks10() { + void countOfPeaks11() { // Initially no peak in [0,3]; update makes index 2 a peak assertThat( new Solution() @@ -97,7 +106,7 @@ void countOfPeaks10() { } @Test - void countOfPeaks11() { + void countOfPeaks12() { // Initially a peak at index 1; update removes it assertThat( new Solution() @@ -108,7 +117,29 @@ void countOfPeaks11() { } @Test - void countOfPeaks12() { + void countOfPeaks13() { + // Array: 1 5 1 5 1, peaks at index 1 and 3 + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 5, 1, 5, 1}, + new int[][] {{1, 0, 2}, {1, 2, 4}, {1, 0, 4}}), + equalTo(new long[] {1L, 1L, 5L})); + } + + @Test + void countOfPeaks14() { + // Update the last index, affecting peak at index n-2 + assertThat( + new Solution() + .countOfPeaks( + new int[] {1, 2, 3, 4, 5}, + new int[][] {{1, 0, 4}, {2, 4, 0}, {1, 0, 4}}), + equalTo(new long[] {0L, 3L})); + } + + @Test + void countOfPeaks15() { // All equal -> no peaks (peak requires strictly greater) assertThat( new Solution().countOfPeaks(new int[] {3, 3, 3, 3, 3}, new int[][] {{1, 0, 4}}), From 77d48cac91a7dcd8b996b697c47fddb98ce5227e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Nov 2026 15:53:33 +0200 Subject: [PATCH 6/6] Added tests --- .../s4017_peaks_in_array_ii/SolutionTest.java | 41 +++++-------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java index 7e87948b8..2deebd4a2 100644 --- a/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java +++ b/src/test/java/g4001_4100/s4017_peaks_in_array_ii/SolutionTest.java @@ -77,15 +77,6 @@ void countOfPeaks8() { @Test void countOfPeaks9() { - // Array: 1 5 1 5 1 5 1 -> peaks at indices 1, 3, 5 - assertThat( - new Solution() - .countOfPeaks(new int[] {1, 5, 1, 5, 1, 5, 1}, new int[][] {{1, 0, 6}}), - equalTo(new long[] {13L})); - } - - @Test - void countOfPeaks10() { // No type-1 queries, result array is empty assertThat( new Solution() @@ -95,7 +86,7 @@ void countOfPeaks10() { } @Test - void countOfPeaks11() { + void countOfPeaks10() { // Initially no peak in [0,3]; update makes index 2 a peak assertThat( new Solution() @@ -106,7 +97,7 @@ void countOfPeaks11() { } @Test - void countOfPeaks12() { + void countOfPeaks11() { // Initially a peak at index 1; update removes it assertThat( new Solution() @@ -117,32 +108,20 @@ void countOfPeaks12() { } @Test - void countOfPeaks13() { - // Array: 1 5 1 5 1, peaks at index 1 and 3 + void countOfPeaks12() { + // All equal -> no peaks (peak requires strictly greater) assertThat( - new Solution() - .countOfPeaks( - new int[] {1, 5, 1, 5, 1}, - new int[][] {{1, 0, 2}, {1, 2, 4}, {1, 0, 4}}), - equalTo(new long[] {1L, 1L, 5L})); + new Solution().countOfPeaks(new int[] {3, 3, 3, 3, 3}, new int[][] {{1, 0, 4}}), + equalTo(new long[] {0L})); } @Test - void countOfPeaks14() { - // Update the last index, affecting peak at index n-2 + void countOfPeaks13() { assertThat( new Solution() .countOfPeaks( - new int[] {1, 2, 3, 4, 5}, - new int[][] {{1, 0, 4}, {2, 4, 0}, {1, 0, 4}}), - equalTo(new long[] {0L, 3L})); - } - - @Test - void countOfPeaks15() { - // All equal -> no peaks (peak requires strictly greater) - assertThat( - new Solution().countOfPeaks(new int[] {3, 3, 3, 3, 3}, new int[][] {{1, 0, 4}}), - equalTo(new long[] {0L})); + new int[] {4, 2, 0, 4, 2, 14, 13}, + new int[][] {{1, 5, 6}, {1, 5, 6}, {1, 2, 6}}), + equalTo(new long[] {0L, 0L, 5L})); } }