Connecting Delivery Zones
Zomato operates a complex network of delivery zones. To optimize resource allocation and ensure timely deliveries, it's crucial to understand which zones are interconnected and can effectively share delivery riders. This problem asks you to determine if two specific delivery zones belong to the same interconnected network, given a list of direct connections.
Implement a function canShareResources that takes the total number of delivery zones, a list of direct connections between zones, and a pair of zones to check. Your function should return true if the two specified zones are part of the same interconnected network (meaning resources can be shared between them), and false otherwise.
[zoneA, zoneB] implies that zoneA and zoneB can share resources.zoneA connects to zoneB, and zoneB connects to zoneC, then zoneA, zoneB, and zoneC can all share resources.0 to n-1.Input:
n: An integer representing the total number of delivery zones. Zones are numbered from 0 to n-1.connections: An array of arrays, where each inner array [zoneA, zoneB] denotes a direct connection between zoneA and zoneB.zone1: An integer representing the first zone to check.zone2: An integer representing the second zone to check.Output:
true if zone1 and zone2 can share resources, false otherwise.[A, B] is a connection, B is also connected to A.connections array may contain duplicate connections or connections where zoneA == zoneB. These should be handled gracefully (e.g., ignored or processed without error).zone1 and zone2 are the same zone, they are considered connected.Examples
Example 1
Input:
n = 4, connections = [[0, 1], [2, 3]], zone1 = 0, zone2 = 1
Output:
true
Note:
Zones 0 and 1 are directly connected. Zones 2 and 3 are directly connected. 0 and 1 are in the same network.
Example 2
Input:
n = 4, connections = [[0, 1], [2, 3]], zone1 = 0, zone2 = 2
Output:
false
Note:
Zones 0 and 1 are connected, and zones 2 and 3 are connected. However, there is no path between zone 0 and zone 2. They belong to different networks.
Example 3
Input:
n = 5, connections = [[0, 1], [1, 2], [2, 0], [3, 4]], zone1 = 0, zone2 = 2
Output:
true
Note:
Zones 0, 1, and 2 are all interconnected through direct or indirect connections. Zone 0 and Zone 2 are in the same network.
Constraints
1 <= n <= 10^40 <= connections.length <= 10^50 <= zoneA, zoneB < n for all [zoneA, zoneB] in connections0 <= zone1, zone2 < nO(N + Mα(N)) where N is the number of zones, M is the number of connections, and α is the inverse Ackermann function (nearly constant).O(N).connections list or zone1 == zone2?Loading editor...