Large dam

Company: Komprise

Difficulty: medium

Problem Statement

Large Dam Problem Description A large dam has N floodgates, numbered 0 to N-1 . The control system keeps a record of floodgate operations: each entry in the record opens a pathway between two distinct floodgates, letting water flow directly between them from that day onward. The record is a list of K logs. Each log is [day, door_A, door_B] : on that day , a pathway was opened between floodgate door_A and floodgate door_B . Once a pathway is opened it is never closed, so every pathway recorded on a day at or before some day D is in service on day D . Two floodgates are connected on a given day if water can travel between them, either through a pathway opened directly between them or by passing through other floodgates along pathways that are in service. Find the earliest day on which every floodgate in the dam is connected to every other one. If the floodgates never all become connected, the answer is -1 . Function Description Complete the function solve(N, K, logs) : N : the number of