All practice tasks

Coding (Algo)

Count Cheapest Delivery Routes

Dijkstra · Hard

A routing report needs the number of distinct cheapest routes, not just their shared cost. Parallel connections are separate choices. Strictly positive weights ensure shortest routes cannot contain a cycle and that route counts are complete when propagated to a more distant node.

Count distinct directed routes from source to target whose total weight is minimal while treating parallel edges as distinct choices.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ n: integer, edges: [integer, integer, integer][], source: integer, target: integer }

Fields

  • n is the node count and edges contains directed [from,to,weight] triples. Parallel edges are distinct input entries; self-loops are allowed.
  • All weights are non-negative integers unless the task explicitly requires positive weights.
  • source is the starting node. source = -1 means no starting node and yields no reachable nodes.
  • For this task every weight is strictly positive; zero-weight cycles are outside the contract.
  • target is the destination node. target = -1 means no destination and yields no route.

Output

Return value

  • Return an exact integer count, not the minimum cost. Unreachable or missing endpoints return zero.
  • A valid source equal to target has one empty shortest route. Distinct edge sequences count separately.

Constraints

  • 0 <= n <= 50000; 0 <= edges.length <= 200000; endpoints are valid node labels; 0 <= weight <= 1000000.
  • source and target, when used, are either -1 or valid node labels. All distance and slack results fit a safe JSON integer.
  • 1 <= weight <= 1000000. The number of shortest routes to every node is at most Number.MAX_SAFE_INTEGER.

Examples

Example 1

{
  "edges": [
    [
      0,
      1,
      1
    ],
    [
      0,
      1,
      1
    ],
    [
      1,
      2,
      1
    ],
    [
      0,
      2,
      2
    ]
  ],
  "n": 3,
  "source": 0,
  "target": 2
}

Returns

3

Example 2

{
  "edges": [],
  "n": 1,
  "source": 0,
  "target": 0
}

Returns

1

Required complexity

O(n + m log(m + 1)) time and O(n + m) auxiliary space

What this task practices

Count Cheapest Delivery Routes is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as hard difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.