Amazon Alexa SDE Hiring Challenge
Kingdom Connections
Kingdom Connections
There are N provinces [1, 2, …, N]. King Bab wants to create his kingdom. For this, he needs to capture some provinces. Two provinces can be connected by water, land, or by both. A pair of provinces are connected by land and B pairs of provinces are connected by water. Since bob’s navy is very weak, he does not want any of his captured provinces to be connected by water, moreover Bob has a strong army, so he wants to capture every province that is connected to his kingdom by land.
More formally, the following conditions must be satisfied:
- For each captured province every province connected to it by land should also be captured.
- No two provinces captured should be connected by water directly or by and other provinces.
- Any two captured provinces should be connected directly or through other captured provinces.
Task
Output the maximum number of provinces that Bob can capture satisfying the given conditions. If bob can’t capture any province then output 0.
Solution
- Time : O((n + a + b)logn)
- Space : O(n)
1 |
|