diff --git "a/solutions/yesul/0203/[\353\254\270\354\240\23433]\352\260\204\353\213\250\355\225\234\354\234\240\353\213\210\354\230\250\355\214\214\354\235\270\353\223\234\354\225\214\352\263\240\353\246\254\354\246\230\352\265\254\355\230\204\355\225\230\352\270\260.js" "b/solutions/yesul/0203/[\353\254\270\354\240\23433]\352\260\204\353\213\250\355\225\234\354\234\240\353\213\210\354\230\250\355\214\214\354\235\270\353\223\234\354\225\214\352\263\240\353\246\254\354\246\230\352\265\254\355\230\204\355\225\230\352\270\260.js" new file mode 100644 index 0000000..3a6af31 --- /dev/null +++ "b/solutions/yesul/0203/[\353\254\270\354\240\23433]\352\260\204\353\213\250\355\225\234\354\234\240\353\213\210\354\230\250\355\214\214\354\235\270\353\223\234\354\225\214\352\263\240\353\246\254\354\246\230\352\265\254\355\230\204\355\225\230\352\270\260.js" @@ -0,0 +1,39 @@ +function solution(k, operations) { + const parent = Array.from({ length: k }, (_, index) => index); + + function find(x) { + if (parent[x] === x) { + return x; + } + + return parent[x] = find(parent[x]); + } + + function union(x, y) { + const rootX = find(x); + const rootY = find(y); + + if (rootX !== rootY) { + if (rootY < rootX) { + parent[rootX] = rootY; + } else { + parent[rootY] = rootX; + } + } + } + + for (const [operation, ...args] of operations) { + if (operation === 'u') { + union(...args); + } else if (operation === 'f') { + find(...args); + } + } + + // 집합의 개수 세기 + return parent.filter((val, idx) => val === idx).length; + +} + +console.log(solution(3, [['u', 0, 1], ['u', 1, 2], ['f', 2]])) // 1 +console.log(solution(4, [['u', 0, 1], ['u', 2, 3], ['f', 0]])) // 2 diff --git "a/solutions/yesul/0203/[\353\254\270\354\240\23434]\355\217\260\354\274\223\353\252\254.js" "b/solutions/yesul/0203/[\353\254\270\354\240\23434]\355\217\260\354\274\223\353\252\254.js" new file mode 100644 index 0000000..030c257 --- /dev/null +++ "b/solutions/yesul/0203/[\353\254\270\354\240\23434]\355\217\260\354\274\223\353\252\254.js" @@ -0,0 +1,10 @@ +function solution(nums) { + const max = nums.length / 2; + const types = new Set(nums); + + return Math.min(types.size, max); +} + +console.log(solution([3, 1, 2, 3])) // 2 +console.log(solution([3, 3, 3, 2, 2, 4])) // 3 +console.log(solution([3, 3, 3, 2, 2, 2])) // 2 \ No newline at end of file diff --git "a/solutions/yesul/0203/[\353\254\270\354\240\23435]\354\204\254\354\227\260\352\262\260\355\225\230\352\270\260.js" "b/solutions/yesul/0203/[\353\254\270\354\240\23435]\354\204\254\354\227\260\352\262\260\355\225\230\352\270\260.js" new file mode 100644 index 0000000..02f39c7 --- /dev/null +++ "b/solutions/yesul/0203/[\353\254\270\354\240\23435]\354\204\254\354\227\260\352\262\260\355\225\230\352\270\260.js" @@ -0,0 +1,35 @@ +function solution(n, costs) { + const parent = Array.from({ length: n }, (_, i) => i); + + function find(x) { + if (parent[x] === x) { + return x; + } + + return parent[x] = find(parent[x]); + } + + function union(x, y) { + const rootX = find(x); + const rootY = find(y); + if (rootX !== rootY) { + parent[rootY] = rootX; + return true; + } + return false; + + } + + // 가장 적은 다리 건설 비용 + costs.sort((a, b) => a[2] - b[2]); + + let total = 0; + + for (const [from, to, cost] of costs) { + if (union(from, to)) { + total += cost; + } + } + + return total; +} \ No newline at end of file