Trains the technique from
LeetCode 1584Min Cost to Connect All PointsThis 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 groundskeeper is running irrigation pipe between the sprinkler valves sunk into a flat field. Valve i sits at points[i] = [x_i, y_i], measured in metres from the pump house. No two valves share a position.
Trenches may only follow the paving joints, which run strictly north-south or strictly east-west, so a trench laid straight between valve i and valve j swallows |east_i - east_j| + |north_i - north_j| metres of pipe. Any pair of valves may be trenched together.
Water flows from one valve to another whenever some chain of trenches links them. The groundskeeper wants every valve on one linked network. Return the smallest number of metres of pipe that a set of trenches achieving this can use.
Example 1
Trenching valve 0 to valve 1 takes 4 metres, valve 0 to valve 2 takes 5, and valve 2 to valve 3 takes 6, which is 15 metres and leaves all four valves on one network.
Example 2
Running valve 0 to valve 1, valve 1 to valve 2 and valve 2 to valve 3 costs 6 metres each, for 18 metres, and every valve is reachable from every other.
Example 3
The single valve is already a network on its own, so no trench is dug.
Example 4
All four valves stand on the same paving joint. Linking each valve to the next one up the line costs 3 + 4 + 5 = 12 metres.
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 min_cost_connect_points(points: list[list[int]]) -> int:public int minCostConnectPoints(int[][] points)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.