Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Solution: Reorder Routes to Make All Paths Lead to the City Zero
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

You are given n cities labeled from 0 to n-1 and n-1 roads, which form a tree structure. Each road is directed, meaning it has a specific direction. These roads are represented by the array connections, where connections[i] = [a, b] indicates a road from city a to city b.

Change the direction of some roads so that every city can reach city 0.

Return the minimum number of edges that need to be reversed.

Examples

Example 1

  • Input: n = 5, connections = [[0, 1], [1, 2], [2, 3], [4, 3]]
  • Expected Output: 3

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible