From 41b9523ef2e98db62d7da1326fc6038324b0d8b7 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 20 Jul 2025 16:18:39 +0300 Subject: [PATCH 01/13] Added tasks 3618-3621 --- .../Solution.java | 46 ++++++++++ .../readme.md | 45 ++++++++++ .../Solution.java | 50 +++++++++++ .../readme.md | 42 +++++++++ .../Solution.java | 59 +++++++++++++ .../s3620_network_recovery_pathways/readme.md | 87 +++++++++++++++++++ .../Solution.java | 71 +++++++++++++++ .../readme.md | 60 +++++++++++++ .../SolutionTest.java | 18 ++++ .../SolutionTest.java | 32 +++++++ .../SolutionTest.java | 32 +++++++ .../SolutionTest.java | 18 ++++ 12 files changed, 560 insertions(+) create mode 100644 src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java create mode 100644 src/main/java/g3601_3700/s3618_split_array_by_prime_indices/readme.md create mode 100644 src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java create mode 100644 src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/readme.md create mode 100644 src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java create mode 100644 src/main/java/g3601_3700/s3620_network_recovery_pathways/readme.md create mode 100644 src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java create mode 100644 src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/readme.md create mode 100644 src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3620_network_recovery_pathways/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/SolutionTest.java diff --git a/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java new file mode 100644 index 000000000..add964e7e --- /dev/null +++ b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java @@ -0,0 +1,46 @@ +package g3601_3700.s3618_split_array_by_prime_indices; + +// #Medium #2025_07_20_Time_3_ms_(100.00%)_Space_62.52_MB_(100.00%) + +public class Solution { + public long splitArray(int[] nums) { + int n = nums.length; + boolean[] isPrime = sieve(n); + long sumA = 0; + long sumB = 0; + for (int i = 0; i < n; i++) { + if (isPrime[i]) { + sumA += nums[i]; + } else { + sumB += nums[i]; + } + } + return Math.abs(sumA - sumB); + } + + // Sieve of Eratosthenes to find all prime indices up to n + private boolean[] sieve(int n) { + boolean[] isPrime = new boolean[n]; + if (n > 2) { + isPrime[2] = true; + } + for (int i = 3; i < n; i += 2) { + isPrime[i] = true; + } + if (n > 2) { + isPrime[2] = true; + } + for (int i = 3; i * i < n; i += 2) { + if (isPrime[i]) { + for (int j = i * i; j < n; j += i * 2) { + isPrime[j] = false; + } + } + } + isPrime[0] = false; + if (n > 1) { + isPrime[1] = false; + } + return isPrime; + } +} diff --git a/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/readme.md b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/readme.md new file mode 100644 index 000000000..e5c8cfdc9 --- /dev/null +++ b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/readme.md @@ -0,0 +1,45 @@ +3618\. Split Array by Prime Indices + +Medium + +You are given an integer array `nums`. + +Split `nums` into two arrays `A` and `B` using the following rule: + +* Elements at **prime** indices in `nums` must go into array `A`. +* All other elements must go into array `B`. + +Return the **absolute** difference between the sums of the two arrays: `|sum(A) - sum(B)|`. + +**Note:** An empty array has a sum of 0. + +**Example 1:** + +**Input:** nums = [2,3,4] + +**Output:** 1 + +**Explanation:** + +* The only prime index in the array is 2, so `nums[2] = 4` is placed in array `A`. +* The remaining elements, `nums[0] = 2` and `nums[1] = 3` are placed in array `B`. +* `sum(A) = 4`, `sum(B) = 2 + 3 = 5`. +* The absolute difference is `|4 - 5| = 1`. + +**Example 2:** + +**Input:** nums = [-1,5,7,0] + +**Output:** 3 + +**Explanation:** + +* The prime indices in the array are 2 and 3, so `nums[2] = 7` and `nums[3] = 0` are placed in array `A`. +* The remaining elements, `nums[0] = -1` and `nums[1] = 5` are placed in array `B`. +* `sum(A) = 7 + 0 = 7`, `sum(B) = -1 + 5 = 4`. +* The absolute difference is `|7 - 4| = 3`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java new file mode 100644 index 000000000..193bd7b2d --- /dev/null +++ b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java @@ -0,0 +1,50 @@ +package g3601_3700.s3619_count_islands_with_total_value_divisible_by_k; + +// #Medium #2025_07_20_Time_16_ms_(96.67%)_Space_70.90_MB_(100.00%) + +public class Solution { + private int m; + private int n; + + public int countIslands(int[][] grid, int k) { + int count = 0; + m = grid.length; + n = grid[0].length; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (grid[i][j] != 0) { + int curr = dfs(i, j, grid); + if (curr % k == 0) { + count++; + } + } + } + } + return count; + } + + private int dfs(int i, int j, int[][] grid) { + if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) { + return Integer.MAX_VALUE; + } + int count = grid[i][j]; + grid[i][j] = 0; + int x = dfs(i + 1, j, grid); + int y = dfs(i, j + 1, grid); + int a = dfs(i - 1, j, grid); + int b = dfs(i, j - 1, grid); + if (x != Integer.MAX_VALUE) { + count += x; + } + if (y != Integer.MAX_VALUE) { + count += y; + } + if (a != Integer.MAX_VALUE) { + count += a; + } + if (b != Integer.MAX_VALUE) { + count += b; + } + return count; + } +} diff --git a/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/readme.md b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/readme.md new file mode 100644 index 000000000..841a4bc2c --- /dev/null +++ b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/readme.md @@ -0,0 +1,42 @@ +3619\. Count Islands With Total Value Divisible by K + +Medium + +You are given an `m x n` matrix `grid` and a positive integer `k`. An **island** is a group of **positive** integers (representing land) that are **4-directionally** connected (horizontally or vertically). + +The **total value** of an island is the sum of the values of all cells in the island. + +Return the number of islands with a total value **divisible by** `k`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png) + +**Input:** grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5 + +**Output:** 2 + +**Explanation:** + +The grid contains four islands. The islands highlighted in blue have a total value that is divisible by 5, while the islands highlighted in red do not. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png) + +**Input:** grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3 + +**Output:** 6 + +**Explanation:** + +The grid contains six islands, each with a total value that is divisible by 3. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `1 <= m, n <= 1000` +* 1 <= m * n <= 105 +* 0 <= grid[i][j] <= 106 +* 1 <= k <= 106 \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java new file mode 100644 index 000000000..4c54db551 --- /dev/null +++ b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java @@ -0,0 +1,59 @@ +package g3601_3700.s3620_network_recovery_pathways; + +// #Hard #2025_07_20_Time_18_ms_(100.00%)_Space_120.24_MB_(100.00%) + +import java.util.ArrayList; + +@SuppressWarnings("unchecked") +public class Solution { + private int ans = -1; + private int d; + private long k = 0; + + public int findMaxPathScore(int[][] edges, boolean[] online, long k) { + int n = online.length; + this.k = k; + this.d = n - 1; + ArrayList

[] g = new ArrayList[n]; + for (int i = 0; i < n; ++i) { + g[i] = new ArrayList<>(); + } + for (int[] i : edges) { + if (online[i[0]] && online[i[1]]) { + g[i[0]].add(new P(i[1], i[2])); + } + } + dfs(g, 0, 0L, Integer.MAX_VALUE); + return ans; + } + + private void dfs(ArrayList

[] g, int s, long tc, int max) { + if (s == d) { + if (ans == -1) { + ans = max; + } else { + ans = Math.max(ans, max); + } + return; + } + for (P i : g[s]) { + long cost = tc + i.c; + if (ans != -1 && ans >= max) { + return; + } + if (cost <= k) { + dfs(g, i.d, cost, Math.min(max, i.c)); + } + } + } + + private static final class P { + int d; + int c; + + P(int d, int c) { + this.d = d; + this.c = c; + } + } +} diff --git a/src/main/java/g3601_3700/s3620_network_recovery_pathways/readme.md b/src/main/java/g3601_3700/s3620_network_recovery_pathways/readme.md new file mode 100644 index 000000000..d8aab05a3 --- /dev/null +++ b/src/main/java/g3601_3700/s3620_network_recovery_pathways/readme.md @@ -0,0 +1,87 @@ +3620\. Network Recovery Pathways + +Hard + +You are given a directed acyclic graph of `n` nodes numbered from 0 to `n − 1`. This is represented by a 2D array `edges` of length `m`, where edges[i] = [ui, vi, costi] indicates a one‑way communication from node ui to node vi with a recovery cost of costi. + +Some nodes may be offline. You are given a boolean array `online` where `online[i] = true` means node `i` is online. Nodes 0 and `n − 1` are always online. + +A path from 0 to `n − 1` is **valid** if: + +* All intermediate nodes on the path are online. +* The total recovery cost of all edges on the path does not exceed `k`. + +For each valid path, define its **score** as the minimum edge‑cost along that path. + +Return the **maximum** path score (i.e., the largest **minimum**\-edge cost) among all valid paths. If no valid path exists, return -1. + +**Example 1:** + +**Input:** edges = [[0,1,5],[1,3,10],[0,2,3],[2,3,4]], online = [true,true,true,true], k = 10 + +**Output:** 3 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/06/06/graph-10.png) + +* The graph has two possible routes from node 0 to node 3: + + 1. Path `0 → 1 → 3` + + * Total cost = `5 + 10 = 15`, which exceeds k (`15 > 10`), so this path is invalid. + + 2. Path `0 → 2 → 3` + + * Total cost = `3 + 4 = 7 <= k`, so this path is valid. + + * The minimum edge‐cost along this path is `min(3, 4) = 3`. + +* There are no other valid paths. Hence, the maximum among all valid path‐scores is 3. + + +**Example 2:** + +**Input:** edges = [[0,1,7],[1,4,5],[0,2,6],[2,3,6],[3,4,2],[2,4,6]], online = [true,true,true,false,true], k = 12 + +**Output:** 6 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/06/06/graph-11.png) + +* Node 3 is offline, so any path passing through 3 is invalid. + +* Consider the remaining routes from 0 to 4: + + 1. Path `0 → 1 → 4` + + * Total cost = `7 + 5 = 12 <= k`, so this path is valid. + + * The minimum edge‐cost along this path is `min(7, 5) = 5`. + + 2. Path `0 → 2 → 3 → 4` + + * Node 3 is offline, so this path is invalid regardless of cost. + + 3. Path `0 → 2 → 4` + + * Total cost = `6 + 6 = 12 <= k`, so this path is valid. + + * The minimum edge‐cost along this path is `min(6, 6) = 6`. + +* Among the two valid paths, their scores are 5 and 6. Therefore, the answer is 6. + + +**Constraints:** + +* `n == online.length` +* 2 <= n <= 5 * 104 +* `0 <= m == edges.length <=` min(105, n * (n - 1) / 2) +* edges[i] = [ui, vi, costi] +* 0 <= ui, vi < n +* ui != vi +* 0 <= costi <= 109 +* 0 <= k <= 5 * 1013 +* `online[i]` is either `true` or `false`, and both `online[0]` and `online[n − 1]` are `true`. +* The given graph is a directed acyclic graph. \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java new file mode 100644 index 000000000..c16d3eef1 --- /dev/null +++ b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java @@ -0,0 +1,71 @@ +package g3601_3700.s3621_number_of_integers_with_popcount_depth_equal_to_k_i; + +// #Hard #2025_07_20_Time_1_ms_(100.00%)_Space_41.10_MB_(100.00%) + +public class Solution { + private static final int MX_LN = 61; + private static final long[][] SLCT = new long[MX_LN][MX_LN]; + private static final int[] POP_HGHT = new int[MX_LN]; + private static boolean strt = false; + + private void setup() { + if (strt) { + return; + } + for (int i = 0; i < MX_LN; i++) { + SLCT[i][0] = SLCT[i][i] = 1; + for (int j = 1; j < i; j++) { + SLCT[i][j] = SLCT[i - 1][j - 1] + SLCT[i - 1][j]; + } + } + POP_HGHT[1] = 0; + for (int v = 2; v < MX_LN; v++) { + POP_HGHT[v] = 1 + POP_HGHT[Long.bitCount(v)]; + } + strt = true; + } + + private long countNumbers(long upperLimit, int setBits) { + if (setBits == 0) { + return 1; + } + long count = 0; + int used = 0; + int len = 0; + for (long x = upperLimit; x > 0; x >>= 1) { + len++; + } + for (int pos = len - 1; pos >= 0; pos--) { + if (((upperLimit >> pos) & 1) == 1) { + if (setBits - used <= pos) { + count += SLCT[pos][setBits - used]; + } + used++; + if (used > setBits) { + break; + } + } + } + if (Long.bitCount(upperLimit) == setBits) { + count++; + } + return count; + } + + public long popcountDepth(long tillNumber, int depthQuery) { + setup(); + if (depthQuery == 0) { + return tillNumber >= 1 ? 1 : 0; + } + long total = 0; + for (int ones = 1; ones < MX_LN; ones++) { + if (POP_HGHT[ones] == depthQuery - 1) { + total += countNumbers(tillNumber, ones); + } + } + if (depthQuery == 1) { + total -= 1; + } + return total; + } +} diff --git a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/readme.md b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/readme.md new file mode 100644 index 000000000..bc2f988ae --- /dev/null +++ b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/readme.md @@ -0,0 +1,60 @@ +3621\. Number of Integers With Popcount-Depth Equal to K I + +Hard + +You are given two integers `n` and `k`. + +For any positive integer `x`, define the following sequence: + +* p0 = x +* pi+1 = popcount(pi) for all `i >= 0`, where `popcount(y)` is the number of set bits (1's) in the binary representation of `y`. + +This sequence will eventually reach the value 1. + +The **popcount-depth** of `x` is defined as the **smallest** integer `d >= 0` such that pd = 1. + +For example, if `x = 7` (binary representation `"111"`). Then, the sequence is: `7 → 3 → 2 → 1`, so the popcount-depth of 7 is 3. + +Your task is to determine the number of integers in the range `[1, n]` whose popcount-depth is **exactly** equal to `k`. + +Return the number of such integers. + +**Example 1:** + +**Input:** n = 4, k = 1 + +**Output:** 2 + +**Explanation:** + +The following integers in the range `[1, 4]` have popcount-depth exactly equal to 1: + +| x | Binary | Sequence | +|---|--------|------------| +| 2 | `"10"` | `2 → 1` | +| 4 | `"100"`| `4 → 1` | + +Thus, the answer is 2. + +**Example 2:** + +**Input:** n = 7, k = 2 + +**Output:** 3 + +**Explanation:** + +The following integers in the range `[1, 7]` have popcount-depth exactly equal to 2: + +| x | Binary | Sequence | +|---|---------|----------------| +| 3 | `"11"` | `3 → 2 → 1` | +| 5 | `"101"` | `5 → 2 → 1` | +| 6 | `"110"` | `6 → 2 → 1` | + +Thus, the answer is 3. + +**Constraints:** + +* 1 <= n <= 1015 +* `0 <= k <= 5` \ No newline at end of file diff --git a/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java b/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java new file mode 100644 index 000000000..c25fb463a --- /dev/null +++ b/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java @@ -0,0 +1,18 @@ +package g3601_3700.s3618_split_array_by_prime_indices; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void splitArray() { + assertThat(new Solution().splitArray(new int[] {2, 3, 4}), equalTo(1L)); + } + + @Test + void splitArray2() { + assertThat(new Solution().splitArray(new int[] {-1, 5, 7, 0}), equalTo(3L)); + } +} diff --git a/src/test/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/SolutionTest.java b/src/test/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/SolutionTest.java new file mode 100644 index 000000000..40c0caaff --- /dev/null +++ b/src/test/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/SolutionTest.java @@ -0,0 +1,32 @@ +package g3601_3700.s3619_count_islands_with_total_value_divisible_by_k; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countIslands() { + assertThat( + new Solution() + .countIslands( + new int[][] { + {0, 2, 1, 0, 0}, + {0, 5, 0, 0, 5}, + {0, 0, 1, 0, 0}, + {0, 1, 4, 7, 0}, + {0, 2, 0, 0, 8} + }, + 5), + equalTo(2)); + } + + @Test + void countIslands2() { + assertThat( + new Solution() + .countIslands(new int[][] {{3, 0, 3, 0}, {0, 3, 0, 3}, {3, 0, 3, 0}}, 3), + equalTo(6)); + } +} diff --git a/src/test/java/g3601_3700/s3620_network_recovery_pathways/SolutionTest.java b/src/test/java/g3601_3700/s3620_network_recovery_pathways/SolutionTest.java new file mode 100644 index 000000000..42327109b --- /dev/null +++ b/src/test/java/g3601_3700/s3620_network_recovery_pathways/SolutionTest.java @@ -0,0 +1,32 @@ +package g3601_3700.s3620_network_recovery_pathways; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findMaxPathScore() { + assertThat( + new Solution() + .findMaxPathScore( + new int[][] {{0, 1, 5}, {1, 3, 10}, {0, 2, 3}, {2, 3, 4}}, + new boolean[] {true, true, true, true}, + 10L), + equalTo(3)); + } + + @Test + void findMaxPathScore2() { + assertThat( + new Solution() + .findMaxPathScore( + new int[][] { + {0, 1, 7}, {1, 4, 5}, {0, 2, 6}, {2, 3, 6}, {3, 4, 2}, {2, 4, 6} + }, + new boolean[] {true, true, true, false, true}, + 12L), + equalTo(6)); + } +} diff --git a/src/test/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/SolutionTest.java b/src/test/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/SolutionTest.java new file mode 100644 index 000000000..417af8b56 --- /dev/null +++ b/src/test/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3601_3700.s3621_number_of_integers_with_popcount_depth_equal_to_k_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void popcountDepth() { + assertThat(new Solution().popcountDepth(4L, 1), equalTo(2L)); + } + + @Test + void popcountDepth2() { + assertThat(new Solution().popcountDepth(7L, 2), equalTo(3L)); + } +} From 2949e49c201a212848dca7e85008911d0188516e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 20 Jul 2025 16:26:32 +0300 Subject: [PATCH 02/13] Fixed sonar --- .../Solution.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java index c16d3eef1..4dd82cf0a 100644 --- a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java +++ b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java @@ -4,23 +4,23 @@ public class Solution { private static final int MX_LN = 61; - private static final long[][] SLCT = new long[MX_LN][MX_LN]; - private static final int[] POP_HGHT = new int[MX_LN]; - private static boolean strt = false; + private final long[][] slct = new long[MX_LN][MX_LN]; + private final int[] popHeight = new int[MX_LN]; + private boolean strt = false; private void setup() { if (strt) { return; } for (int i = 0; i < MX_LN; i++) { - SLCT[i][0] = SLCT[i][i] = 1; + slct[i][0] = slct[i][i] = 1; for (int j = 1; j < i; j++) { - SLCT[i][j] = SLCT[i - 1][j - 1] + SLCT[i - 1][j]; + slct[i][j] = slct[i - 1][j - 1] + slct[i - 1][j]; } } - POP_HGHT[1] = 0; + popHeight[1] = 0; for (int v = 2; v < MX_LN; v++) { - POP_HGHT[v] = 1 + POP_HGHT[Long.bitCount(v)]; + popHeight[v] = 1 + popHeight[Long.bitCount(v)]; } strt = true; } @@ -38,7 +38,7 @@ private long countNumbers(long upperLimit, int setBits) { for (int pos = len - 1; pos >= 0; pos--) { if (((upperLimit >> pos) & 1) == 1) { if (setBits - used <= pos) { - count += SLCT[pos][setBits - used]; + count += slct[pos][setBits - used]; } used++; if (used > setBits) { @@ -59,7 +59,7 @@ public long popcountDepth(long tillNumber, int depthQuery) { } long total = 0; for (int ones = 1; ones < MX_LN; ones++) { - if (POP_HGHT[ones] == depthQuery - 1) { + if (popHeight[ones] == depthQuery - 1) { total += countNumbers(tillNumber, ones); } } From f7ca9f52d8c960f08d945df733419bacb2854931 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 20 Jul 2025 16:31:21 +0300 Subject: [PATCH 03/13] Added tests --- .../SolutionTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java b/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java index c25fb463a..6cad36abc 100644 --- a/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java +++ b/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java @@ -15,4 +15,24 @@ void splitArray() { void splitArray2() { assertThat(new Solution().splitArray(new int[] {-1, 5, 7, 0}), equalTo(3L)); } + + @Test + void splitArray3() { + assertThat( + new Solution() + .splitArray( + new int[] { + -54818575, + 801071518, + 745054848, + -415289833, + 161564441, + 706292027, + 306478283, + 943480367, + 222076810, + 992619933 + }), + equalTo(1535784865L)); + } } From 2417856b786cdd998cbc8248fa526a9f5d808fa6 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 20 Jul 2025 16:34:54 +0300 Subject: [PATCH 04/13] Improved task --- .../Solution.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java index 4dd82cf0a..a544c32e9 100644 --- a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java +++ b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java @@ -6,12 +6,8 @@ public class Solution { private static final int MX_LN = 61; private final long[][] slct = new long[MX_LN][MX_LN]; private final int[] popHeight = new int[MX_LN]; - private boolean strt = false; - private void setup() { - if (strt) { - return; - } + public Solution() { for (int i = 0; i < MX_LN; i++) { slct[i][0] = slct[i][i] = 1; for (int j = 1; j < i; j++) { @@ -22,7 +18,6 @@ private void setup() { for (int v = 2; v < MX_LN; v++) { popHeight[v] = 1 + popHeight[Long.bitCount(v)]; } - strt = true; } private long countNumbers(long upperLimit, int setBits) { @@ -53,7 +48,6 @@ private long countNumbers(long upperLimit, int setBits) { } public long popcountDepth(long tillNumber, int depthQuery) { - setup(); if (depthQuery == 0) { return tillNumber >= 1 ? 1 : 0; } From c0f9d8fde87a16e66c5db7b7e540e1e096074127 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 20 Jul 2025 16:37:31 +0300 Subject: [PATCH 05/13] Fixed test --- .../s3618_split_array_by_prime_indices/SolutionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java b/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java index 6cad36abc..b7d9b890f 100644 --- a/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java +++ b/src/test/java/g3601_3700/s3618_split_array_by_prime_indices/SolutionTest.java @@ -33,6 +33,6 @@ void splitArray3() { 222076810, 992619933 }), - equalTo(1535784865L)); + equalTo(449455001L)); } } From d718e361988ff6c29f195cc689de812f6f9fe93c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 20 Jul 2025 21:41:37 +0300 Subject: [PATCH 06/13] Added tasks 3622-3625 --- .../Solution.java | 17 +++ .../readme.md | 36 +++++ .../Solution.java | 29 ++++ .../readme.md | 45 ++++++ .../Solution.java | 107 ++++++++++++++ .../readme.md | 86 +++++++++++ .../Solution.java | 138 ++++++++++++++++++ .../readme.md | 42 ++++++ .../SolutionTest.java | 18 +++ .../SolutionTest.java | 23 +++ .../SolutionTest.java | 38 +++++ .../SolutionTest.java | 23 +++ 12 files changed, 602 insertions(+) create mode 100644 src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java create mode 100644 src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/readme.md create mode 100644 src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java create mode 100644 src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/readme.md create mode 100644 src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java create mode 100644 src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/readme.md create mode 100644 src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java create mode 100644 src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/readme.md create mode 100644 src/test/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3623_count_number_of_trapezoids_i/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/SolutionTest.java create mode 100644 src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java diff --git a/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java new file mode 100644 index 000000000..70fcdbed4 --- /dev/null +++ b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java @@ -0,0 +1,17 @@ +package g3601_3700.s3622_check_divisibility_by_digit_sum_and_product; + +// #Easy #2025_07_20_Time_0_ms_(100.00%)_Space_40.53_MB_(_%) + +public class Solution { + public boolean checkDivisibility(int n) { + int x = n; + int sum = 0; + int mul = 1; + while (x != 0) { + sum += x % 10; + mul *= x % 10; + x = x / 10; + } + return n % (sum + mul) == 0; + } +} diff --git a/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/readme.md b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/readme.md new file mode 100644 index 000000000..c033e3f13 --- /dev/null +++ b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/readme.md @@ -0,0 +1,36 @@ +3622\. Check Divisibility by Digit Sum and Product + +Easy + +You are given a positive integer `n`. Determine whether `n` is divisible by the **sum** of the following two values: + +* The **digit sum** of `n` (the sum of its digits). + +* The **digit** **product** of `n` (the product of its digits). + + +Return `true` if `n` is divisible by this sum; otherwise, return `false`. + +**Example 1:** + +**Input:** n = 99 + +**Output:** true + +**Explanation:** + +Since 99 is divisible by the sum (9 + 9 = 18) plus product (9 \* 9 = 81) of its digits (total 99), the output is true. + +**Example 2:** + +**Input:** n = 23 + +**Output:** false + +**Explanation:** + +Since 23 is not divisible by the sum (2 + 3 = 5) plus product (2 \* 3 = 6) of its digits (total 11), the output is false. + +**Constraints:** + +* 1 <= n <= 106 \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java new file mode 100644 index 000000000..a5d576c6f --- /dev/null +++ b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java @@ -0,0 +1,29 @@ +package g3601_3700.s3623_count_number_of_trapezoids_i; + +// #Medium #2025_07_20_Time_30_ms_(99.91%)_Space_100.40_MB_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int countTrapezoids(int[][] points) { + int mod = 1_000_000_007; + long inv = 500_000_004L; + Map map = new HashMap<>(points.length); + for (int[] p : points) { + map.merge(p[1], 1, Integer::sum); + } + long sum = 0L; + long sumPairs = 0L; + for (int c : map.values()) { + if (c > 1) { + long pairs = ((long) c * (c - 1) / 2) % mod; + sum = (sum + pairs) % mod; + sumPairs = (sumPairs + pairs * pairs % mod) % mod; + } + } + long res = (sum * sum % mod - sumPairs + mod) % mod; + res = (res * inv) % mod; + return (int) res; + } +} diff --git a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/readme.md b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/readme.md new file mode 100644 index 000000000..2debd6da0 --- /dev/null +++ b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/readme.md @@ -0,0 +1,45 @@ +3623\. Count Number of Trapezoids I + +Medium + +You are given a 2D integer array `points`, where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane. + +A **horizontal** **trapezoid** is a convex quadrilateral with **at least one pair** of horizontal sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have the same slope. + +Return the _number of unique_ **_horizontal_ _trapezoids_** that can be formed by choosing any four distinct points from `points`. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** points = [[1,0],[2,0],[3,0],[2,2],[3,2]] + +**Output:** 3 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-6.png) ![](https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-7.png) ![](https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-8.png) + +There are three distinct ways to pick four points that form a horizontal trapezoid: + +* Using points `[1,0]`, `[2,0]`, `[3,2]`, and `[2,2]`. +* Using points `[2,0]`, `[3,0]`, `[3,2]`, and `[2,2]`. +* Using points `[1,0]`, `[3,0]`, `[3,2]`, and `[2,2]`. + +**Example 2:** + +**Input:** points = [[0,0],[1,0],[0,1],[2,1]] + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-5.png) + +There is only one horizontal trapezoid that can be formed. + +**Constraints:** + +* 4 <= points.length <= 105 +* –108 <= xi, yi <= 108 +* All points are pairwise distinct. \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java new file mode 100644 index 000000000..916478585 --- /dev/null +++ b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java @@ -0,0 +1,107 @@ +package g3601_3700.s3624_number_of_integers_with_popcount_depth_equal_to_k_ii; + +// #Hard #2025_07_20_Time_27_ms_(96.92%)_Space_125.98_MB_(100.00%) + +import java.util.ArrayList; + +public class Solution { + private static final int[] DEPTH_TABLE = new int[65]; + + static { + DEPTH_TABLE[1] = 0; + for (int i = 2; i <= 64; ++i) { + DEPTH_TABLE[i] = 1 + DEPTH_TABLE[Integer.bitCount(i)]; + } + } + + private int computeDepth(long number) { + if (number == 1) { + return 0; + } + return 1 + DEPTH_TABLE[Long.bitCount(number)]; + } + + public int[] popcountDepth(long[] nums, long[][] queries) { + int len = nums.length; + int maxDepth = 6; + FenwickTree[] trees = new FenwickTree[maxDepth]; + for (int d = 0; d < maxDepth; ++d) { + trees[d] = new FenwickTree(); + trees[d].build(len); + } + for (int i = 0; i < len; ++i) { + int depth = computeDepth(nums[i]); + if (depth < maxDepth) { + trees[depth].update(i + 1, 1); + } + } + ArrayList ansList = new ArrayList<>(); + for (long[] query : queries) { + int type = (int) query[0]; + if (type == 1) { + int left = (int) query[1]; + int right = (int) query[2]; + int depth = (int) query[3]; + if (depth >= 0 && depth < maxDepth) { + ansList.add(trees[depth].queryRange(left + 1, right + 1)); + } else { + ansList.add(0); + } + } else if (type == 2) { + int index = (int) query[1]; + long newVal = query[2]; + int oldDepth = computeDepth(nums[index]); + if (oldDepth < maxDepth) { + trees[oldDepth].update(index + 1, -1); + } + nums[index] = newVal; + int newDepth = computeDepth(newVal); + if (newDepth < maxDepth) { + trees[newDepth].update(index + 1, 1); + } + } + } + int[] ansArray = new int[ansList.size()]; + for (int i = 0; i < ansList.size(); i++) { + ansArray[i] = ansList.get(i); + } + return ansArray; + } + + private static class FenwickTree { + private int[] tree; + private int size; + + public FenwickTree() { + this.size = 0; + } + + public void build(int n) { + this.size = n; + this.tree = new int[size + 1]; + } + + public void update(int index, int value) { + while (index <= size) { + tree[index] += value; + index += index & (-index); + } + } + + public int query(int index) { + int result = 0; + while (index > 0) { + result += tree[index]; + index -= index & (-index); + } + return result; + } + + public int queryRange(int left, int right) { + if (left > right) { + return 0; + } + return query(right) - query(left - 1); + } + } +} diff --git a/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/readme.md b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/readme.md new file mode 100644 index 000000000..50904c084 --- /dev/null +++ b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/readme.md @@ -0,0 +1,86 @@ +3624\. Number of Integers With Popcount-Depth Equal to K II + +Hard + +You are given an integer array `nums`. + +For any positive integer `x`, define the following sequence: + +* p0 = x +* pi+1 = popcount(pi) for all `i >= 0`, where `popcount(y)` is the number of set bits (1's) in the binary representation of `y`. + +This sequence will eventually reach the value 1. + +The **popcount-depth** of `x` is defined as the **smallest** integer `d >= 0` such that pd = 1. + +For example, if `x = 7` (binary representation `"111"`). Then, the sequence is: `7 → 3 → 2 → 1`, so the popcount-depth of 7 is 3. + +You are also given a 2D integer array `queries`, where each `queries[i]` is either: + +* `[1, l, r, k]` - **Determine** the number of indices `j` such that `l <= j <= r` and the **popcount-depth** of `nums[j]` is equal to `k`. +* `[2, idx, val]` - **Update** `nums[idx]` to `val`. + +Return an integer array `answer`, where `answer[i]` is the number of indices for the ith query of type `[1, l, r, k]`. + +**Example 1:** + +**Input:** nums = [2,4], queries = [[1,0,1,1],[2,1,1],[1,0,1,0]] + +**Output:** [2,1] + +**Explanation:** + +| `i` | `queries[i]` | `nums` | binary(`nums`) | popcount-
depth | `[l, r]` | `k` | Valid
`nums[j]` | updated
`nums` | Answer | +|-----|--------------|----------|----------------|---------------------|----------|-----|---------------------|--------------------|---------| +| 0 | [1,0,1,1] | [2,4] | [10, 100] | [1, 1] | [0, 1] | 1 | [0, 1] | — | 2 | +| 1 | [2,1,1] | [2,4] | [10, 100] | [1, 1] | — | — | — | [2,1] | — | +| 2 | [1,0,1,0] | [2,1] | [10, 1] | [1, 0] | [0, 1] | 0 | [1] | — | 1 | + +Thus, the final `answer` is `[2, 1]`. + +**Example 2:** + +**Input:** nums = [3,5,6], queries = [[1,0,2,2],[2,1,4],[1,1,2,1],[1,0,1,0]] + +**Output:** [3,1,0] + +**Explanation:** + +| `i` | `queries[i]` | `nums` | binary(`nums`) | popcount-
depth | `[l, r]` | `k` | Valid
`nums[j]` | updated
`nums` | Answer | +|-----|----------------|----------------|-----------------------|---------------------|----------|-----|---------------------|--------------------|---------| +| 0 | [1,0,2,2] | [3, 5, 6] | [11, 101, 110] | [2, 2, 2] | [0, 2] | 2 | [0, 1, 2] | — | 3 | +| 1 | [2,1,4] | [3, 5, 6] | [11, 101, 110] | [2, 2, 2] | — | — | — | [3, 4, 6] | — | +| 2 | [1,1,2,1] | [3, 4, 6] | [11, 100, 110] | [2, 1, 2] | [1, 2] | 1 | [1] | — | 1 | +| 3 | [1,0,1,0] | [3, 4, 6] | [11, 100, 110] | [2, 1, 2] | [0, 1] | 0 | [] | — | 0 | + +Thus, the final `answer` is `[3, 1, 0]`. + +**Example 3:** + +**Input:** nums = [1,2], queries = [[1,0,1,1],[2,0,3],[1,0,0,1],[1,0,0,2]] + +**Output:** [1,0,1] + +**Explanation:** + +| `i` | `queries[i]` | `nums` | binary(`nums`) | popcount-
depth | `[l, r]` | `k` | Valid
`nums[j]` | updated
`nums` | Answer | +|-----|----------------|------------|----------------|---------------------|----------|-----|--------------------|--------------------|---------| +| 0 | [1,0,1,1] | [1, 2] | [1, 10] | [0, 1] | [0, 1] | 1 | [1] | — | 1 | +| 1 | [2,0,3] | [1, 2] | [1, 10] | [0, 1] | — | — | — | [3, 2] | | +| 2 | [1,0,0,1] | [3, 2] | [11, 10] | [2, 1] | [0, 0] | 1 | [] | — | 0 | +| 3 | [1,0,0,2] | [3, 2] | [11, 10] | [2, 1] | [0, 0] | 2 | [0] | — | 1 | + +Thus, the final `answer` is `[1, 0, 1]`. + +**Constraints:** + +* 1 <= n == nums.length <= 105 +* 1 <= nums[i] <= 1015 +* 1 <= queries.length <= 105 +* `queries[i].length == 3` or `4` + * `queries[i] == [1, l, r, k]` or, + * `queries[i] == [2, idx, val]` + * `0 <= l <= r <= n - 1` + * `0 <= k <= 5` + * `0 <= idx <= n - 1` + * 1 <= val <= 1015 \ No newline at end of file diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java new file mode 100644 index 000000000..d677abcec --- /dev/null +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java @@ -0,0 +1,138 @@ +package g3601_3700.s3625_count_number_of_trapezoids_ii; + +// #Hard #2025_07_20_Time_406_ms_(96.92%)_Space_153.72_MB_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + private static class Slope { + int dx; + int dy; + + Slope(int dx, int dy) { + this.dx = dx; + this.dy = dy; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Slope)) { + return false; + } + Slope s = (Slope) o; + return dx == s.dx && dy == s.dy; + } + + @Override + public int hashCode() { + return dx * 1000003 ^ dy; + } + } + + private static class Pair { + int a; + int b; + + Pair(int a, int b) { + this.a = a; + this.b = b; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Pair)) { + return false; + } + Pair p = (Pair) o; + return a == p.a && b == p.b; + } + + @Override + public int hashCode() { + return a * 1000003 ^ b; + } + } + + public int countTrapezoids(int[][] points) { + int n = points.length; + Map> slopeLines = new HashMap<>(); + Map> midpointSlopes = new HashMap<>(); + for (int i = 0; i < n; i++) { + int x1 = points[i][0]; + int y1 = points[i][1]; + for (int j = i + 1; j < n; j++) { + int x2 = points[j][0]; + int y2 = points[j][1]; + int dx = x2 - x1; + int dy = y2 - y1; + int g = gcd(Math.abs(dx), Math.abs(dy)); + dx /= g; + dy /= g; + if (dx < 0 || (dx == 0 && dy < 0)) { + dx = -dx; + dy = -dy; + } + int nx = -dy; + int ny = dx; + long lineId = (long) nx * x1 + (long) ny * y1; + Slope slopeKey = new Slope(dx, dy); + slopeLines + .computeIfAbsent(slopeKey, k -> new HashMap<>()) + .merge(lineId, 1, Integer::sum); + int mx = x1 + x2; + int my = y1 + y2; + Pair mid = new Pair(mx, my); + midpointSlopes + .computeIfAbsent(mid, k -> new HashMap<>()) + .merge(slopeKey, 1, Integer::sum); + } + } + long trapezoidsRaw = 0; + for (Map lines : slopeLines.values()) { + if (lines.size() < 2) { + continue; + } + long s = 0; + long s2 = 0; + for (int c : lines.values()) { + s += c; + s2 += (long) c * c; + } + trapezoidsRaw += (s * s - s2) / 2; + } + long parallelograms = 0; + for (Map mp : midpointSlopes.values()) { + if (mp.size() < 2) { + continue; + } + long s = 0; + long s2 = 0; + for (int c : mp.values()) { + s += c; + s2 += (long) c * c; + } + parallelograms += (s * s - s2) / 2; + } + long res = trapezoidsRaw - parallelograms; + if (res < 0) { + res = 0; + } + return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) res; + } + + private int gcd(int a, int b) { + while (b != 0) { + int t = a % b; + a = b; + b = t; + } + return a == 0 ? 1 : a; + } +} diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/readme.md b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/readme.md new file mode 100644 index 000000000..bda582e70 --- /dev/null +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/readme.md @@ -0,0 +1,42 @@ +3625\. Count Number of Trapezoids II + +Hard + +You are given a 2D integer array `points` where points[i] = [xi, yi] represents the coordinates of the ith point on the Cartesian plane. + +Return _the number of unique_ _trapezoids_ that can be formed by choosing any four distinct points from `points`. + +A **trapezoid** is a convex quadrilateral with **at least one pair** of parallel sides. Two lines are parallel if and only if they have the same slope. + +**Example 1:** + +**Input:** points = [[-3,2],[3,0],[2,3],[3,2],[2,-3]] + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-4.png) ![](https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-3.png) + +There are two distinct ways to pick four points that form a trapezoid: + +* The points `[-3,2], [2,3], [3,2], [2,-3]` form one trapezoid. +* The points `[2,3], [3,2], [3,0], [2,-3]` form another trapezoid. + +**Example 2:** + +**Input:** points = [[0,0],[1,0],[0,1],[2,1]] + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-5.png) + +There is only one trapezoid which can be formed. + +**Constraints:** + +* `4 <= points.length <= 500` +* –1000 <= xi, yi <= 1000 +* All points are pairwise distinct. \ No newline at end of file diff --git a/src/test/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/SolutionTest.java b/src/test/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/SolutionTest.java new file mode 100644 index 000000000..2deacc56e --- /dev/null +++ b/src/test/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/SolutionTest.java @@ -0,0 +1,18 @@ +package g3601_3700.s3622_check_divisibility_by_digit_sum_and_product; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void checkDivisibility() { + assertThat(new Solution().checkDivisibility(99), equalTo(true)); + } + + @Test + void checkDivisibility2() { + assertThat(new Solution().checkDivisibility(23), equalTo(false)); + } +} diff --git a/src/test/java/g3601_3700/s3623_count_number_of_trapezoids_i/SolutionTest.java b/src/test/java/g3601_3700/s3623_count_number_of_trapezoids_i/SolutionTest.java new file mode 100644 index 000000000..7543d19b2 --- /dev/null +++ b/src/test/java/g3601_3700/s3623_count_number_of_trapezoids_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3601_3700.s3623_count_number_of_trapezoids_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countTrapezoids() { + assertThat( + new Solution() + .countTrapezoids(new int[][] {{1, 0}, {2, 0}, {3, 0}, {2, 2}, {3, 2}}), + equalTo(3)); + } + + @Test + void countTrapezoids2() { + assertThat( + new Solution().countTrapezoids(new int[][] {{0, 0}, {1, 0}, {0, 1}, {2, 1}}), + equalTo(1)); + } +} diff --git a/src/test/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/SolutionTest.java b/src/test/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/SolutionTest.java new file mode 100644 index 000000000..50226360b --- /dev/null +++ b/src/test/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/SolutionTest.java @@ -0,0 +1,38 @@ +package g3601_3700.s3624_number_of_integers_with_popcount_depth_equal_to_k_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void popcountDepth() { + assertThat( + new Solution() + .popcountDepth( + new long[] {2, 4}, + new long[][] {{1, 0, 1, 1}, {2, 1, 1}, {1, 0, 1, 0}}), + equalTo(new int[] {2, 1})); + } + + @Test + void popcountDepth2() { + assertThat( + new Solution() + .popcountDepth( + new long[] {3, 5, 6}, + new long[][] {{1, 0, 2, 2}, {2, 1, 4}, {1, 1, 2, 1}, {1, 0, 1, 0}}), + equalTo(new int[] {3, 1, 0})); + } + + @Test + void popcountDepth3() { + assertThat( + new Solution() + .popcountDepth( + new long[] {1, 2}, + new long[][] {{1, 0, 1, 1}, {2, 0, 3}, {1, 0, 0, 1}, {1, 0, 0, 2}}), + equalTo(new int[] {1, 0, 1})); + } +} diff --git a/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java b/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java new file mode 100644 index 000000000..dc3be00b4 --- /dev/null +++ b/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3601_3700.s3625_count_number_of_trapezoids_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countTrapezoids() { + assertThat( + new Solution() + .countTrapezoids(new int[][] {{-3, 2}, {3, 0}, {2, 3}, {3, 2}, {2, -3}}), + equalTo(2)); + } + + @Test + void countTrapezoids2() { + assertThat( + new Solution().countTrapezoids(new int[][] {{0, 0}, {1, 0}, {0, 1}, {2, 1}}), + equalTo(1)); + } +} From db537e23e0af63eb658542df04253d84a8f09e82 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 21 Jul 2025 07:53:36 +0300 Subject: [PATCH 07/13] Updated task --- .../s3625_count_number_of_trapezoids_ii/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java index d677abcec..ed55f2eb7 100644 --- a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3625_count_number_of_trapezoids_ii; -// #Hard #2025_07_20_Time_406_ms_(96.92%)_Space_153.72_MB_(100.00%) +// #Hard #2025_07_21_Time_354_ms_(100.00%)_Space_131.52_MB_(52.31%) import java.util.HashMap; import java.util.Map; From 051fe4f4c8e7600c303aaaaa5e41726e3fbf8885 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 21 Jul 2025 08:03:28 +0300 Subject: [PATCH 08/13] Fixed sonar --- .../s3623_count_number_of_trapezoids_i/Solution.java | 6 +++--- .../Solution.java | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java index a5d576c6f..3339f8ec7 100644 --- a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java +++ b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java @@ -15,9 +15,9 @@ public int countTrapezoids(int[][] points) { } long sum = 0L; long sumPairs = 0L; - for (int c : map.values()) { - if (c > 1) { - long pairs = ((long) c * (c - 1) / 2) % mod; + for (Integer num : map.values()) { + if (num > 1) { + long pairs = ((long) num * (num - 1) / 2) % mod; sum = (sum + pairs) % mod; sumPairs = (sumPairs + pairs * pairs % mod) % mod; } diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java index ed55f2eb7..1ef386ac4 100644 --- a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java @@ -101,9 +101,9 @@ public int countTrapezoids(int[][] points) { } long s = 0; long s2 = 0; - for (int c : lines.values()) { - s += c; - s2 += (long) c * c; + for (Integer line : lines.values()) { + s += line; + s2 += (long) line * line; } trapezoidsRaw += (s * s - s2) / 2; } @@ -114,9 +114,9 @@ public int countTrapezoids(int[][] points) { } long s = 0; long s2 = 0; - for (int c : mp.values()) { - s += c; - s2 += (long) c * c; + for (Integer num : mp.values()) { + s += num; + s2 += (long) num * num; } parallelograms += (s * s - s2) / 2; } From bb192ddd93aff7503ceeccfd3a1476ce54b7e5b8 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 21 Jul 2025 08:06:55 +0300 Subject: [PATCH 09/13] Added test --- .../SolutionTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java b/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java index dc3be00b4..0dafdbff6 100644 --- a/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java +++ b/src/test/java/g3601_3700/s3625_count_number_of_trapezoids_ii/SolutionTest.java @@ -20,4 +20,21 @@ void countTrapezoids2() { new Solution().countTrapezoids(new int[][] {{0, 0}, {1, 0}, {0, 1}, {2, 1}}), equalTo(1)); } + + @Test + void countTrapezoids3() { + assertThat( + new Solution() + .countTrapezoids( + new int[][] { + {71, -89}, + {-75, -89}, + {-9, 11}, + {-24, -89}, + {-51, -89}, + {-77, -89}, + {42, 11} + }), + equalTo(10)); + } } From b153a11268a0662be5bfc13bf9bd3f854255e461 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 21 Jul 2025 08:15:40 +0300 Subject: [PATCH 10/13] Improved task --- .../s3625_count_number_of_trapezoids_ii/Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java index 1ef386ac4..1bcfa6ccc 100644 --- a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java @@ -121,9 +121,6 @@ public int countTrapezoids(int[][] points) { parallelograms += (s * s - s2) / 2; } long res = trapezoidsRaw - parallelograms; - if (res < 0) { - res = 0; - } return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) res; } From be0f557a587826385a8caeb40b60cac647582210 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 22 Jul 2025 10:39:56 +0300 Subject: [PATCH 11/13] Updated tags --- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 121 ++++++++++++------ .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- .../Solution.java | 2 +- 8 files changed, 89 insertions(+), 46 deletions(-) diff --git a/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java index add964e7e..69f9fa988 100644 --- a/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java +++ b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3618_split_array_by_prime_indices; -// #Medium #2025_07_20_Time_3_ms_(100.00%)_Space_62.52_MB_(100.00%) +// #Medium #Biweekly_Contest_161 #2025_07_22_Time_3_ms_(100.00%)_Space_62.42_MB_(16.27%) public class Solution { public long splitArray(int[] nums) { diff --git a/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java index 193bd7b2d..b97c91982 100644 --- a/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java +++ b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3619_count_islands_with_total_value_divisible_by_k; -// #Medium #2025_07_20_Time_16_ms_(96.67%)_Space_70.90_MB_(100.00%) +// #Medium #Biweekly_Contest_161 #2025_07_22_Time_16_ms_(96.65%)_Space_71.27_MB_(46.65%) public class Solution { private int m; diff --git a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java index 4c54db551..9a4edb065 100644 --- a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java +++ b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java @@ -1,59 +1,102 @@ package g3601_3700.s3620_network_recovery_pathways; -// #Hard #2025_07_20_Time_18_ms_(100.00%)_Space_120.24_MB_(100.00%) +// #Hard #Biweekly_Contest_161 #2025_07_22_Time_158_ms_(64.00%)_Space_130.14_MB_(14.77%) import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; -@SuppressWarnings("unchecked") public class Solution { - private int ans = -1; - private int d; - private long k = 0; - - public int findMaxPathScore(int[][] edges, boolean[] online, long k) { - int n = online.length; - this.k = k; - this.d = n - 1; - ArrayList

[] g = new ArrayList[n]; + private List topologicalSort(int n, List> g) { + int[] indeg = new int[n]; for (int i = 0; i < n; ++i) { - g[i] = new ArrayList<>(); - } - for (int[] i : edges) { - if (online[i[0]] && online[i[1]]) { - g[i[0]].add(new P(i[1], i[2])); + for (int adj_node : g.get(i)) { + indeg[adj_node]++; } } - dfs(g, 0, 0L, Integer.MAX_VALUE); - return ans; - } - - private void dfs(ArrayList

[] g, int s, long tc, int max) { - if (s == d) { - if (ans == -1) { - ans = max; - } else { - ans = Math.max(ans, max); + Queue q = new LinkedList<>(); + List ts = new ArrayList<>(); + for (int i = 0; i < n; ++i) { + if (indeg[i] == 0) { + q.offer(i); } - return; } - for (P i : g[s]) { - long cost = tc + i.c; - if (ans != -1 && ans >= max) { - return; + while (!q.isEmpty()) { + int u = q.poll(); + ts.add(u); + for (int v : g.get(u)) { + indeg[v]--; + if (indeg[v] == 0) { + q.offer(v); + } } - if (cost <= k) { - dfs(g, i.d, cost, Math.min(max, i.c)); + } + return ts; + } + + private boolean check(int x, int n, List> adj, List ts, boolean[] online, long k) { + long[] d = new long[n]; + Arrays.fill(d, Long.MAX_VALUE); // Represents INF + d[0] = 0; + for (int u : ts) { + if (d[u] != Long.MAX_VALUE) { // If d[u] is reachable + for (int[] p : adj.get(u)) { + int v = p[0]; + int c = p[1]; + if (c < x || !online[v]) { + continue; + } + if (d[u] + c < d[v]) { + d[v] = d[u] + c; + } + } } } + return d[n - 1] <= k; } - private static final class P { - int d; - int c; + public int findMaxPathScore(int[][] edges, boolean[] online, long k) { + int n = online.length; + // Adjacency list for graph with edge weights + List> adj = new ArrayList<>(); + for (int i = 0; i < n; i++) { + adj.add(new ArrayList<>()); + } + // Adjacency list for topological sort (unweighted graph) + List> g = new ArrayList<>(); + for (int i = 0; i < n; i++) { + g.add(new ArrayList<>()); + } + for (int[] e : edges) { + int u = e[0]; + int v = e[1]; + int c = e[2]; + adj.get(u).add(new int[]{v, c}); + g.get(u).add(v); + } + + List ts = topologicalSort(n, g); - P(int d, int c) { - this.d = d; - this.c = c; + // Call the helper method + if (!check(0, n, adj, ts, online, k)) { + return -1; + } + + int l = 0; + int h = 0; + for (int[] e : edges) { + h = Math.max(h, e[2]); + } + while (l < h) { + int md = l + (h - l + 1) / 2; + if (check(md, n, adj, ts, online, k)) { + l = md; + } else { + h = md - 1; + } } + return l; } } diff --git a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java index a544c32e9..f3c8287cf 100644 --- a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java +++ b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3621_number_of_integers_with_popcount_depth_equal_to_k_i; -// #Hard #2025_07_20_Time_1_ms_(100.00%)_Space_41.10_MB_(100.00%) +// #Hard #Biweekly_Contest_161 #2025_07_22_Time_9_ms_(70.67%)_Space_44.98_MB_(38.42%) public class Solution { private static final int MX_LN = 61; diff --git a/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java index 70fcdbed4..57d71544c 100644 --- a/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java +++ b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3622_check_divisibility_by_digit_sum_and_product; -// #Easy #2025_07_20_Time_0_ms_(100.00%)_Space_40.53_MB_(_%) +// #Easy #Weekly_Contest_459 #2025_07_22_Time_0_ms_(100.00%)_Space_41.36_MB_(8.47%) public class Solution { public boolean checkDivisibility(int n) { diff --git a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java index 3339f8ec7..f7f7b00bd 100644 --- a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java +++ b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3623_count_number_of_trapezoids_i; -// #Medium #2025_07_20_Time_30_ms_(99.91%)_Space_100.40_MB_(100.00%) +// #Medium #Weekly_Contest_459 #2025_07_22_Time_27_ms_(100.00%)_Space_101.17_MB_(64.13%) import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java index 916478585..270f76796 100644 --- a/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java +++ b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3624_number_of_integers_with_popcount_depth_equal_to_k_ii; -// #Hard #2025_07_20_Time_27_ms_(96.92%)_Space_125.98_MB_(100.00%) +// #Hard #Weekly_Contest_459 #2025_07_22_Time_26_ms_(97.90%)_Space_99.87_MB_(97.09%) import java.util.ArrayList; diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java index 1bcfa6ccc..3f85e9fbd 100644 --- a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3625_count_number_of_trapezoids_ii; -// #Hard #2025_07_21_Time_354_ms_(100.00%)_Space_131.52_MB_(52.31%) +// #Hard #Weekly_Contest_459 #2025_07_22_Time_372_ms_(97.30%)_Space_154.52_MB_(31.53%) import java.util.HashMap; import java.util.Map; From 4911304bdfade680754dd70532cda14b29f65218 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 22 Jul 2025 10:42:58 +0300 Subject: [PATCH 12/13] Fixed style --- .../Solution.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java index 9a4edb065..a1fae1099 100644 --- a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java +++ b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java @@ -12,8 +12,8 @@ public class Solution { private List topologicalSort(int n, List> g) { int[] indeg = new int[n]; for (int i = 0; i < n; ++i) { - for (int adj_node : g.get(i)) { - indeg[adj_node]++; + for (int adjNode : g.get(i)) { + indeg[adjNode]++; } } Queue q = new LinkedList<>(); @@ -36,12 +36,14 @@ private List topologicalSort(int n, List> g) { return ts; } - private boolean check(int x, int n, List> adj, List ts, boolean[] online, long k) { + private boolean check( + int x, int n, List> adj, List ts, boolean[] online, long k) { long[] d = new long[n]; - Arrays.fill(d, Long.MAX_VALUE); // Represents INF + Arrays.fill(d, Long.MAX_VALUE); d[0] = 0; for (int u : ts) { - if (d[u] != Long.MAX_VALUE) { // If d[u] is reachable + // If d[u] is reachable + if (d[u] != Long.MAX_VALUE) { for (int[] p : adj.get(u)) { int v = p[0]; int c = p[1]; @@ -64,7 +66,6 @@ public int findMaxPathScore(int[][] edges, boolean[] online, long k) { for (int i = 0; i < n; i++) { adj.add(new ArrayList<>()); } - // Adjacency list for topological sort (unweighted graph) List> g = new ArrayList<>(); for (int i = 0; i < n; i++) { g.add(new ArrayList<>()); @@ -73,17 +74,13 @@ public int findMaxPathScore(int[][] edges, boolean[] online, long k) { int u = e[0]; int v = e[1]; int c = e[2]; - adj.get(u).add(new int[]{v, c}); + adj.get(u).add(new int[] {v, c}); g.get(u).add(v); } - List ts = topologicalSort(n, g); - - // Call the helper method if (!check(0, n, adj, ts, online, k)) { return -1; } - int l = 0; int h = 0; for (int[] e : edges) { From 68e78d10f819b88a1150a080857809e44014d8a0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 22 Jul 2025 21:22:54 +0300 Subject: [PATCH 13/13] Updated tags --- .../s3618_split_array_by_prime_indices/Solution.java | 3 ++- .../Solution.java | 3 ++- .../g3601_3700/s3620_network_recovery_pathways/Solution.java | 3 ++- .../Solution.java | 3 ++- .../Solution.java | 2 +- .../s3623_count_number_of_trapezoids_i/Solution.java | 3 ++- .../Solution.java | 3 ++- .../s3625_count_number_of_trapezoids_ii/Solution.java | 3 ++- 8 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java index 69f9fa988..100a396c5 100644 --- a/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java +++ b/src/main/java/g3601_3700/s3618_split_array_by_prime_indices/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3618_split_array_by_prime_indices; -// #Medium #Biweekly_Contest_161 #2025_07_22_Time_3_ms_(100.00%)_Space_62.42_MB_(16.27%) +// #Medium #Array #Math #Number_Theory #Biweekly_Contest_161 +// #2025_07_22_Time_3_ms_(100.00%)_Space_62.61_MB_(10.13%) public class Solution { public long splitArray(int[] nums) { diff --git a/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java index b97c91982..ee2704f1b 100644 --- a/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java +++ b/src/main/java/g3601_3700/s3619_count_islands_with_total_value_divisible_by_k/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3619_count_islands_with_total_value_divisible_by_k; -// #Medium #Biweekly_Contest_161 #2025_07_22_Time_16_ms_(96.65%)_Space_71.27_MB_(46.65%) +// #Medium #Array #Matrix #Union_Find #Biweekly_Contest_161 #Depth_First_Search +// #Breadth_First_Search #2025_07_22_Time_16_ms_(96.65%)_Space_70.96_MB_(50.08%) public class Solution { private int m; diff --git a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java index a1fae1099..288799452 100644 --- a/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java +++ b/src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3620_network_recovery_pathways; -// #Hard #Biweekly_Contest_161 #2025_07_22_Time_158_ms_(64.00%)_Space_130.14_MB_(14.77%) +// #Hard #Array #Dynamic_Programming #Binary_Search #Heap_Priority_Queue #Graph #Topological_Sort +// #Shortest_Path #Biweekly_Contest_161 #2025_07_22_Time_151_ms_(66.08%)_Space_108.87_MB_(63.77%) import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java index f3c8287cf..6a49b7154 100644 --- a/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java +++ b/src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3621_number_of_integers_with_popcount_depth_equal_to_k_i; -// #Hard #Biweekly_Contest_161 #2025_07_22_Time_9_ms_(70.67%)_Space_44.98_MB_(38.42%) +// #Hard #Dynamic_Programming #Math #Combinatorics #Biweekly_Contest_161 +// #2025_07_22_Time_9_ms_(70.67%)_Space_44.76_MB_(55.42%) public class Solution { private static final int MX_LN = 61; diff --git a/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java index 57d71544c..2d7025f46 100644 --- a/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java +++ b/src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product/Solution.java @@ -1,6 +1,6 @@ package g3601_3700.s3622_check_divisibility_by_digit_sum_and_product; -// #Easy #Weekly_Contest_459 #2025_07_22_Time_0_ms_(100.00%)_Space_41.36_MB_(8.47%) +// #Easy #Math #Weekly_Contest_459 #2025_07_22_Time_0_ms_(100.00%)_Space_40.91_MB_(44.64%) public class Solution { public boolean checkDivisibility(int n) { diff --git a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java index f7f7b00bd..37f4bc048 100644 --- a/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java +++ b/src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3623_count_number_of_trapezoids_i; -// #Medium #Weekly_Contest_459 #2025_07_22_Time_27_ms_(100.00%)_Space_101.17_MB_(64.13%) +// #Medium #Array #Hash_Table #Math #Geometry #Weekly_Contest_459 +// #2025_07_22_Time_30_ms_(99.92%)_Space_100.93_MB_(64.40%) import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java index 270f76796..aa6b25e77 100644 --- a/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java +++ b/src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3624_number_of_integers_with_popcount_depth_equal_to_k_ii; -// #Hard #Weekly_Contest_459 #2025_07_22_Time_26_ms_(97.90%)_Space_99.87_MB_(97.09%) +// #Hard #Array #Segment_Tree #Weekly_Contest_459 +// #2025_07_22_Time_27_ms_(96.44%)_Space_125.92_MB_(24.76%) import java.util.ArrayList; diff --git a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java index 3f85e9fbd..1fb61b968 100644 --- a/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java +++ b/src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii/Solution.java @@ -1,6 +1,7 @@ package g3601_3700.s3625_count_number_of_trapezoids_ii; -// #Hard #Weekly_Contest_459 #2025_07_22_Time_372_ms_(97.30%)_Space_154.52_MB_(31.53%) +// #Hard #Array #Hash_Table #Math #Geometry #Weekly_Contest_459 +// #2025_07_22_Time_347_ms_(99.10%)_Space_131.20_MB_(46.85%) import java.util.HashMap; import java.util.Map;