|
| 1 | +package leetcode_study |
| 2 | + |
| 3 | +import io.kotest.matchers.shouldBe |
| 4 | +import org.junit.jupiter.api.Test |
| 5 | +import kotlin.math.max |
| 6 | + |
| 7 | +class `longest-consecutive-sequence` { |
| 8 | + fun longestConsecutive(nums: IntArray): Int { |
| 9 | + if (nums.isEmpty()) return 0 |
| 10 | + return usingUnionFind(nums) |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * 1. λ°°μ΄μ μ λ ¬νμ¬ μμλλ‘ μννλ©° μ°μ μμ΄ κΈΈμ΄λ₯Ό νμΈνλ€. |
| 15 | + * TC: O(n * log(n)), SC: O(1) |
| 16 | + */ |
| 17 | + private fun usingSort(nums: IntArray): Int { |
| 18 | + nums.sort() |
| 19 | + |
| 20 | + var (length, maxLength) = 1 to 0 |
| 21 | + for (index in 0 until nums.size - 1) { |
| 22 | + if (nums[index] == nums[index + 1]) { |
| 23 | + continue |
| 24 | + } else if (nums[index] + 1 == nums[index + 1]) { |
| 25 | + length++ |
| 26 | + } else { |
| 27 | + maxLength = max(length, maxLength) |
| 28 | + length = 1 |
| 29 | + } |
| 30 | + } |
| 31 | + return max(length, maxLength) |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * 2. Setμ μλ£κ΅¬μ‘°λ₯Ό νμ©νμ¬ κ°μ₯ μμ κ°λΆν° whileλ¬Έμ ν΅ν μ΅λ μ¦κ° κ°μ λ°ννλ€. |
| 36 | + * TC: O(n), SC: O(n) |
| 37 | + */ |
| 38 | + private fun usingSet(nums: IntArray): Int { |
| 39 | + val numberSet = nums.toSet() |
| 40 | + var maxLength = 0 |
| 41 | + |
| 42 | + for (number in nums) { |
| 43 | + if (numberSet.contains(number - 1)) { |
| 44 | + continue |
| 45 | + } |
| 46 | + var length = 1 |
| 47 | + while (numberSet.contains(number + length)) { |
| 48 | + length++ |
| 49 | + } |
| 50 | + maxLength = max(maxLength, length) |
| 51 | + } |
| 52 | + |
| 53 | + return maxLength |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * 3. Union-Find |
| 58 | + * TC: O(n), SC: O(n) |
| 59 | + */ |
| 60 | + private fun usingUnionFind(nums: IntArray): Int { |
| 61 | + val nodes = mutableMapOf<Int, Int>() |
| 62 | + val dsu = DSU(nums.size) |
| 63 | + |
| 64 | + for ((i,n) in nums.withIndex()) { |
| 65 | + if (n in nodes) continue |
| 66 | + |
| 67 | + nodes[n - 1]?.let { dsu.union(i, it) } |
| 68 | + nodes[n + 1]?.let { dsu.union(i, it) } |
| 69 | + |
| 70 | + nodes[n] = i |
| 71 | + } |
| 72 | + |
| 73 | + return dsu.maxLength() |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `μ
λ ₯λ°μ μ μ λ°°μ΄μ μ΅λ μ°μ μμ΄ κΈΈμ΄λ₯Ό λ°ννλ€`() { |
| 78 | + longestConsecutive(intArrayOf()) shouldBe 0 |
| 79 | + longestConsecutive(intArrayOf(100,4,200,1,3,2)) shouldBe 4 |
| 80 | + longestConsecutive(intArrayOf(11,23,12,13,14,21)) shouldBe 4 |
| 81 | + longestConsecutive(intArrayOf(0,3,7,2,5,8,4,6,0,1)) shouldBe 9 |
| 82 | + longestConsecutive(intArrayOf(11,64,43,12,13,10,9,8,7)) shouldBe 7 |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class DSU(val n: Int) { |
| 87 | + private val parent = IntArray(n) { it } |
| 88 | + private val size = IntArray(n) { 1 } |
| 89 | + |
| 90 | + private fun find(x: Int): Int { |
| 91 | + if (parent[x] != x) |
| 92 | + parent[x] = find(parent[x]) |
| 93 | + return parent[x] |
| 94 | + } |
| 95 | + |
| 96 | + fun union(x: Int, y: Int) { |
| 97 | + val root = find(x) |
| 98 | + val child = find(y) |
| 99 | + if(root != child) { |
| 100 | + parent[child] = root |
| 101 | + size[root] += size[child] |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fun maxLength(): Int { |
| 106 | + var res = 0 |
| 107 | + for (i in parent.indices) { |
| 108 | + if (parent[i] == i) |
| 109 | + res = maxOf(res, size[i]) |
| 110 | + } |
| 111 | + return res |
| 112 | + } |
| 113 | +} |
0 commit comments