Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package g3901_4000.s3969_valid_subarrays_with_matching_sum_digits_i;

// #Medium #Array #Hash_Table #Prefix_Sum #Sliding_Window #Enumeration #Senior #Weekly_Contest_507
// #2026_11_03_Time_50_ms_(98.27%)_Space_46.22_MB_(95.95%)

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class Solution {
public int countValidSubarrays(int[] nums, int x) {
int n = nums.length;
long[] pre = new long[n + 1];
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + nums[i];
}
if (x == 0) {
return 0;
}
List<long[]> interval = new ArrayList<>();
for (long p = 1; p <= pre[n]; p *= 10) {
interval.add(new long[] {p * x, p * x + p - 1});
}
List[] isit = new List[10];
for (int r = 0; r < 10; r++) {
isit[r] = new ArrayList<>();
}
long sum = 0;
for (int i = 0; i <= n; i++) {
if (i > 0) {
long k = pre[i];
int r = (int) (((k - x) % 10 + 10) % 10);
List<Long> list = isit[r];
for (long[] j : interval) {
long a = k - j[1];
long b = k - j[0];
if (b < 0) {
continue;
}
int high = upper(list, b);
int low = lower(list, a);
sum += high - low;
}
}
isit[(int) (pre[i] % 10)].add(pre[i]);
}
return (int) sum;
}

private int lower(List<Long> list, long k) {
int l = 0;
int h = list.size();
while (l < h) {
int m = (l + h) >>> 1;
if (list.get(m) < k) {
l = m + 1;
} else {
h = m;
}
}
return l;
}

private int upper(List<Long> list, long k) {
int l = 0;
int h = list.size();
while (l < h) {
int m = (l + h) >>> 1;
if (list.get(m) <= k) {
l = m + 1;
} else {
h = m;
}
}
return l;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3969\. Valid Subarrays With Matching Sum Digits I

Medium

You are given an integer array `nums` and an integer digit `x`.

A non-empty subarrays `nums[l..r]` is considered **valid** if the sum of its elements satisfies both of the following conditions:

* The first digit of the sum is equal to `x`.
* The last digit of the sum is equal to `x`.

Return the number of valid subarrays.

**Example 1:**

**Input:** nums = [1,100,1], x = 1

**Output:** 4

**Explanation:**

The valid subarrays are:

* `nums[0..0]`: `sum = 1`
* `nums[0..1]`: `sum = 1 + 100 = 101`
* `nums[1..2]`: `sum = 100 + 1 = 101`
* `nums[2..2]`: `sum = 1`

Thus, the answer is 4.

**Example 2:**

**Input:** nums = [1], x = 2

**Output:** 0

**Explanation:**

The only subarray is `nums[0..0]` with a sum of 1, which does not satisfy the conditions.

Thus, the answer is 0.

**Constraints:**

* `1 <= nums.length <= 1500`
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* `1 <= x <= 9`
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package g3901_4000.s3970_shortest_path_with_at_most_k_consecutive_identical_characters;

// #Medium #String #Heap_Priority_Queue #Shortest_Path #Graph_Theory #Staff #Weekly_Contest_507
// #2026_11_03_Time_40_ms_(97.73%)_Space_198.21_MB_(98.49%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Solution {
private static class Node {
long w;
int ch;
int city;

public Node(int city, long w, int ch) {
this.city = city;
this.w = w;
this.ch = ch;
}
}

public int shortestPath(int n, int[][] edges, String labelschars, int k) {
int[] chars = new int[n];
char[] labels = labelschars.toCharArray();
Arrays.fill(chars, Integer.MAX_VALUE);
List<List<int[]>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
for (int[] edge : edges) {
adj.get(edge[0]).add(new int[] {edge[1], edge[2]});
}
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> Long.compare(a.w, b.w));
pq.add(new Node(0, 0, 1));
while (!pq.isEmpty()) {
Node nd = pq.poll();
if (nd.ch >= chars[nd.city]) {
continue;
}
chars[nd.city] = nd.ch;
if (nd.city == n - 1) {
return (int) nd.w;
}
addNeighbors(nd, adj, labels, k, pq);
}
return -1;
}

private void addNeighbors(
Node nd, List<List<int[]>> adj, char[] labels, int k, PriorityQueue<Node> pq) {
for (int[] nbr : adj.get(nd.city)) {
int nextCity = nbr[0];
int totalCh = getNextCharCount(nd.ch, labels[nd.city], labels[nextCity]);
if (nd.city != nextCity && totalCh <= k) {
long totalWeight = nd.w + nbr[1];
pq.add(new Node(nextCity, totalWeight, totalCh));
}
}
}

private int getNextCharCount(int ch, char current, char next) {
return current == next ? ch + 1 : 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3970\. Shortest Path With At Most K Consecutive Identical Characters

Medium

You are given an integer `n` representing the number of nodes in a **directed weighted** graph, numbered from 0 to `n - 1`. This is represented by a 2D integer array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> represents a directed edge from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.

You are also given a string `labels` of length `n`, where `labels[i]` is the character assigned to node `i`, and an integer `k`.

Return the **minimum** **total** edge weight of a path from node 0 to node `n - 1` such that the concatenation of the labels of the nodes along the path contains **at most** `k` **consecutive** **identical** characters. If no valid path exists, return -1.

**Example 1:**

**Input:** n = 3, edges = [[0,1,1],[1,2,1],[0,2,3]], labels = "aab", k = 1

**Output:** 3

**Explanation:**

The optimal valid path from node 0 to node 2 is as follows:

* Use `edges[2] = [0, 2, 3]` to reach node 2 with a weight <code>w<sub>i</sub> = 3</code>.

The corresponding concatenation of labels is `"ab"`, which satisfies at most `k = 1` consecutive identical characters. Thus, the answer is 3.

**Example 2:**

**Input:** n = 3, edges = [[0,1,1],[1,2,1],[0,2,3]], labels = "aab", k = 2

**Output:** 2

**Explanation:**

The optimal valid path from node 0 to node 2 is as follows:

* Use `edges[0] = [0, 1, 1]` to reach node 1 with weight <code>w<sub>i</sub> = 1</code>.
* Use `edges[1] = [1, 2, 1]` to reach node 2 with weight <code>w<sub>i</sub> = 1</code>.

The corresponding concatenation of labels is `"aab"`, which satisfies at most `k = 2` consecutive identical characters. Thus, the answer is 2.

**Example 3:**

**Input:** n = 3, edges = [[0,1,1],[1,2,1]], labels = "aaa", k = 2

**Output:** \-1

**Explanation:**

There is no valid path from node 0 to node 2 that satisfies at most `k = 2` consecutive identical characters. Thus, the answer is -1.

**Constraints:**

* <code>1 <= n == labels.length <= 5 * 10<sup>4</sup></code>
* <code>0 <= edges.length <= 5 * 10<sup>4</sup></code>
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code>
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
* <code>u<sub>i</sub> != v<sub>i</sub></code>
* <code>1 <= w<sub>i</sub> <= 10<sup>4</sup></code>
* `labels` consists of lowercase English letters
* `1 <= k <= 50`
63 changes: 63 additions & 0 deletions src/main/java/g3901_4000/s3971_maximum_total_value/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package g3901_4000.s3971_maximum_total_value;

// #Hard #Array #Math #Greedy #Binary_Search #Senior_Staff #Weekly_Contest_507
// #2026_11_03_Time_19_ms_(100.00%)_Space_106.00_MB_(7.14%)

public class Solution {
private static final int MOD = (int) 1e9 + 7;

public int maxTotalValue(int[] value, int[] decay, int m) {
if (getCount(value, decay, m, 0) < m) {
return getSum(value, decay, m, 0);
}
int max = 0;
for (int x : value) {
max = Math.max(max, x);
}
int left = 1;
int right = max;
while (left <= right) {
int mid = left + right >> 1;
if (getCount(value, decay, m, mid) >= m) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return getSum(value, decay, m, right);
}

private long getCount(int[] value, int[] decay, int m, int limit) {
int n = value.length;
long count = 0;
for (int i = 0; i < n; ++i) {
int x = value[i];
int d = decay[i];
if (x >= limit) {
count += (x - limit) / d + 1;
}
if (count >= m) {
break;
}
}
return count;
}

private int getSum(int[] value, int[] decay, long m, int limit) {
int n = value.length;
long sum = 0;
for (int i = 0; i < n; ++i) {
int x = value[i];
int d = decay[i];
if (x <= limit) {
continue;
}
long c = (x - limit - 1) / d + 1L;
sum += c * (x + x - (c - 1) * d);
m -= c;
}
sum >>= 1;
sum += m * limit;
return (int) (sum % MOD);
}
}
67 changes: 67 additions & 0 deletions src/main/java/g3901_4000/s3971_maximum_total_value/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
3971\. Maximum Total Value

Hard

You are given two integer arrays `value` and `decay`, and an integer `m`.

* `value[i]` represents the initial value at index `i`.
* `decay[i]` represents how much the value decreases after each selection of index `i`.

You may select any index **multiple** times. The total number of selections across all indices must not exceed `m`.

If you select index `i` for the <code>t<sup>th</sup></code> time, where `t` is 1-indexed, the value gained is `value[i] - decay[i] * (t - 1)`.

Return the **maximum** total value you can obtain. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** value = [6,5,4], decay = [2,1,1], m = 4

**Output:** 19

**Explanation:**

One optimal sequence of selections is as follows:

* By selecting index 0, the value gained is 6.
* By selecting index 1, the value gained is 5.
* By selecting index 2, the value gained is 4.
* By selecting index 0 again, the value gained is `6 - 2 = 4`.

The total value is `6 + 5 + 4 + 4 = 19`. No other sequence of at most 4 selections gives a higher total value.

**Example 2:**

**Input:** value = [7,2,2], decay = [3,2,1], m = 2

**Output:** 11

**Explanation:**

One optimal sequence of selections is as follows:

* By selecting index 0, the value gained is 7.
* By selecting index 0 again, the value gained is `7 - 3 = 4`.

The total value is `7 + 4 = 11`.

**Example 3:**

**Input:** value = [4,3], decay = [5,4], m = 5

**Output:** 7

**Explanation:**

One optimal sequence of selections is as follows:

* By selecting index 0, the value gained is 4.
* By selecting index 1, the value gained is 3.

The total value is `4 + 3 = 7`.

**Constraints:**

* <code>1 <= value.length == decay.length <= 10<sup>5</sup></code>
* <code>1 <= value[i], decay[i] <= 10<sup>9</sup></code>
* <code>1 <= m <= 10<sup>9</sup></code>
Loading
Loading