Shortest Path With At Most K Consecutive Identical Characters Problem
Description
LeetCode Problem 3970.
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 edges[i] = [u_i, v_i, w_i] represents a directed edge from node u_i to node v_i with weight w_i.
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.
Note: Nodes and edges may be revisited.
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 w_i = 3.
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 w_i = 1.
- Use edges[1] = [1, 2, 1] to reach node 2 with weight w_i = 1.
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:
1 <= n == labels.length <= 5 * 10^40 <= edges.length <= 5 * 10^4edges[i] == [u_i, v_i, w_i]0 <= u_i, v_i <= n - 1u_i != v_i1 <= w_i <= 10^4labelsconsists of lowercase English letters1 <= k <= 50
Solution
Approach: Dijkstra on (node, run length)
Whether a step is allowed depends on how many equal labels we have just seen in a row, so a node alone is not enough to describe where we are. Use the state (u, cur): we stand at node u and the current run of identical labels, ending at u, has length cur. The cost of a state is the total edge weight used to get there, and dist[u][cur] stores the best known value. The start is dist[0][1] = 0, since the label of node 0 is a run of length 1.
From (u, cur), an edge to v with weight w leads to:
(v, 1)iflabels[v] != labels[u](a new run starts), or(v, cur + 1)if the labels are equal. This is only allowed whencur + 1 <= k.
All weights are positive, so Dijkstra's algorithm with a min-heap works. Pop the cheapest state; skip it if a better distance is already recorded. The first time we pop the target node n - 1, its distance is the answer, whatever the run length. If the heap empties first, return -1. Nodes and edges may repeat, and the state graph handles that naturally.
Complexity: O(k * (n + m) * log(n * k)) time and O(n * k + m) space, where m is the number of edges.
from heapq import heappop, heappush
from math import inf
class Solution:
def shortestPath(self, n: int, edges: List[List[int]], labels: str, k: int) -> int:
g = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
dist = [[inf] * (k + 1) for _ in range(n)]
dist[0][1] = 0
pq = [(0, 0, 1)]
while pq:
d, u, cur = heappop(pq)
if u == n - 1:
return d
if d > dist[u][cur]:
continue
for v, w in g[u]:
nxt = 1 if labels[v] != labels[u] else cur + 1
nd = d + w
if nxt <= k and nd < dist[v][nxt]:
dist[v][nxt] = nd
heappush(pq, (nd, v, nxt))
return -1