Water Connection Problem
Pattern: Candy and assign cookies
Each house has at most one pipe out and one pipe in, so the pipes form simple chains. A house with a pipe out and none in starts a chain (a tank); follow it to the end (a tap). The water that can flow is the narrowest pipe on the way.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
There are n houses and pipes a[i] → b[i] with diameter d[i]; each house has at most one pipe leaving and one
arriving. A tank goes at every house with a pipe out and none in, and a tap at the end of its chain. Return
[tank, tap, smallest diameter on the way] for every tank, in increasing order of tank. The catalogue has no practice link for this one.
Think first
- What shape do the pipes make?
- How do you know where a chain starts?
- What limits the flow along a chain?
Hints
Brute force, and why it fails
For every pair of houses, check whether one chain links them: O(n²).
The key insight
With in and out pointers stored in arrays, each chain is walked once from its start, so the whole job is linear.
Dry run
| tanks | 2 3 5 |
|---|
Houses 1 to 9; pipes 7→4 (98), 5→9 (72), 4→6 (10), 2→8 (22), 9→7 (17), 3→1 (66).
The plan
next[a] = b, width[a] = d, hasIn[b] = true
for h in 1..n: if next[h] and not hasIn[h]:
walk to the end, keeping the smallest width; record [h, end, smallest]
Complexity
- Time
O(n + p)- Space
O(n)
Each pipe is followed once.
Edge cases
- No pipes: no tanks.
- A single pipe.
- Houses with no pipes are ignored.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public List<List<Integer>> solve(int n, int[] a, int[] b, int[] d) {
int[] next = new int[n + 1], width = new int[n + 1];
boolean[] hasIn = new boolean[n + 1];
for (int i = 0; i < a.length; i++) {
next[a[i]] = b[i]; // a pipe from a[i] to b[i]
width[a[i]] = d[i];
hasIn[b[i]] = true;
}
List<List<Integer>> out = new ArrayList<>();
for (int h = 1; h <= n; h++) {
if (next[h] == 0 || hasIn[h]) continue; // a tank: pipe out, none in
int cur = h, min = Integer.MAX_VALUE;
while (next[cur] != 0) { // follow the chain to its tap
min = Math.min(min, width[cur]);
cur = next[cur];
}
out.add(Arrays.asList(h, cur, min)); // the narrowest pipe limits the flow
}
return out;
}
}C++
class Solution {
public:
vector<vector<int>> solve(int n, vector<int>& a, vector<int>& b, vector<int>& d) {
vector<int> next(n + 1, 0), width(n + 1, 0);
vector<bool> hasIn(n + 1, false);
for (size_t i = 0; i < a.size(); i++) {
next[a[i]] = b[i]; // a pipe from a[i] to b[i]
width[a[i]] = d[i];
hasIn[b[i]] = true;
}
vector<vector<int>> out;
for (int h = 1; h <= n; h++) {
if (next[h] == 0 || hasIn[h]) continue; // a tank: pipe out, none in
int cur = h, mn = INT_MAX;
while (next[cur] != 0) { // follow the chain to its tap
mn = min(mn, width[cur]);
cur = next[cur];
}
out.push_back(vector<int>{h, cur, mn}); // the narrowest pipe limits the flow
}
return out;
}
};Python
class Solution:
def solve(self, n: int, a: list[int], b: list[int], d: list[int]) -> list[list[int]]:
nxt, width, has_in = {}, {}, set()
for x, y, w in zip(a, b, d):
nxt[x], width[x] = y, w # a pipe from x to y
has_in.add(y)
out = []
for h in range(1, n + 1):
if h in nxt and h not in has_in: # a tank: pipe out, none in
cur, mn = h, float("inf")
while cur in nxt: # follow the chain to its tap
mn = min(mn, width[cur])
cur = nxt[cur]
out.append([h, cur, mn]) # the narrowest pipe limits the flow
return outCommon mistakes
- Starting a walk from houses that have an incoming pipe.
- Reporting the widest pipe instead of the narrowest.