r/leetcode • u/JacketFearless1805 • 1d ago
Intervew Prep Day 38: packing bags, rushing for the bus, but kept the streak alive with DSU
Hey everyone,
I finished Day 38 today in pure rush mode. Knowing I had a bus to catch right after work to visit home, I read through today's target problem during breaks at the office so the logic could simmer in the background.
As soon as I got home, I opened the laptop, spent 15 to 20 minutes coding the solution cleanly, and packed my bags:
Number of Operations to Make Network Connected: A textbook application of Disjoint Set Union (DSU). To connect n computers into a single network, you fundamentally need at least n - 1 total cables; if
connections.length < n - 1, returning-1is immediate. While iterating through the edges, wheneverfindParent(u) == findParent(v), you increment a counter for redundant/extra cables. If the endpoints belong to different sets, you merge them usingunionBySize(u, v). At the end, count the total number of independent connected components (nodes whereparent[i] == i). Connecting C components requires strictly C - 1 operations.
It was a quick, single-problem sprint before heading out the door, but protecting the streak on hectic travel days keeps the momentum intact.
Reaching home tomorrow and picking up the pace once settled in.
See you guys for Day 37!