목록union (2)
N
https://leetcode.com/problems/longest-consecutive-sequence/ Longest Consecutive Sequence - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Union-Find와 Map을 활용 parent라는 map을 선언. nums[i]를 순회하면서 key와 value를 모두 자기 자신인 nums[i]로 초기화한다. 이후 연속된 수의 부모를 하나로 합치는 작업을 진행. nums[i]를 순회하면서 unionPa..
https://programmers.co.kr/learn/courses/30/lessons/42861?language=javascript 코딩테스트 연습 - 섬 연결하기 4 [[0,1,1],[0,2,2],[1,2,5],[1,3,1],[2,3,8]] 4 programmers.co.kr union & find를 이용한 문제 풀이. 우선 parent를 저장하는 n 크기의 배열을 선언하여 자기 자신을 가리키도록 한다. 이후 costs를 비용의 오름차순으로 정렬. for문으로 costs를 순회. costs[0]은 출발점, costs[1]은 도착점이므로 unionFind 함수에 각 노드 번호를 전달해 parent를 찾는다. 만약 n번의 노드의 부모가 n이라면 그대로 n을 리턴해주고, 그렇지 않다면 루트의 번호를 계..