Swiggy Delivery Zone Unification
Swiggy operates a vast network of delivery partners and restaurants. To optimize logistics and understand service coverage, it's crucial to identify distinct "delivery zones" – areas where any two locations can be reached from each other through a series of connected points. This problem asks you to help Swiggy by determining how many such independent delivery zones exist given a set of direct connections between various zones.
Implement a function countDeliveryZones that takes the total number of delivery zones (n) and a list of direct connections between them. Your task is to return the total number of distinct, unconnected delivery zones that exist based on these connections.
n: An integer representing the total number of delivery zones, labeled from 0 to n-1.connections: An array of arrays, where each inner array [zoneA, zoneB] indicates a direct, bidirectional connection between zoneA and zoneB.[u, v] implies u and v are directly connected, and this connection is bidirectional.Examples
Example 1
Input:
n = 4, connections = [[0, 1], [2, 3]]
Output:
2
Note:
Zones {0, 1} form one delivery zone, and zones {2, 3} form another. These two zones are distinct.
Example 2
Input:
n = 3, connections = [[0, 1], [1, 2]]
Output:
1
Note:
Zones {0, 1, 2} are all connected, forming a single delivery zone.
Example 3
Input:
n = 5, connections = []
Output:
5
Note:
Without any connections, each of the 5 zones remains distinct.
Constraints
1 <= n <= 10000 <= connections.length <= n * (n - 1) / 20 <= zoneA, zoneB < nzoneA != zoneB (connections do not include self-loops)[u, v] appears at most once (no duplicate connections).Loading editor...