Trains the technique from
LeetCode 3607Power Grid MaintenanceThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A farm draws from count wells numbered 1 through count, and pipes lists the pairs of wells a pipe joins. Two wells belong to the same cluster when a chain of pipes runs from one to the other, so a well with no pipes is a cluster on its own. Every well is working to begin with.
Work through orders in turn. An order comes in one of two kinds:
[1, well] calls for water at that well. If the well is working the answer is that well itself. Otherwise the answer is the lowest-numbered working well of its cluster, and -1 when its cluster has none working.[2, well] takes that well out of service. A well already out of service is left as it is, and a well out of service never comes back.Return the answers to the [1, well] orders, in the order they were called.
Example 1
Two clusters, wells 1 and 2 in one and wells 3 and 4 in the other. The first call finds well 1 working. Once it goes out of service the same call falls back on well 2, its only clustermate. Wells 3 and 4 go the same way, and the last call comes after both have gone, so nothing is left in that cluster.
Example 2
Wells 1, 2 and 3 share a cluster. With well 2 out of service the call at it falls back on well 1, and once well 1 has gone too the same call falls back on well 3. After well 3 goes the cluster is empty, while well 5 in the other cluster is untouched and answers itself.
Example 3
No pipes at all, so every well stands alone. A call at a well out of service has nowhere to fall back on, while well 4 is still working and answers itself.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def process_queries(count: int, pipes: list[list[int]], orders: list[list[int]]) -> list[int]:public int[] processQueries(int count, int[][] pipes, int[][] orders)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.