diff --git a/container-with-most-water/hyunjung-choi.kt b/container-with-most-water/hyunjung-choi.kt new file mode 100644 index 000000000..9ca6cb2b3 --- /dev/null +++ b/container-with-most-water/hyunjung-choi.kt @@ -0,0 +1,22 @@ +/** + * 시간 복잡도: O(n) + * 공간 복잡도: O(1) + */ + +class Solution { + fun maxArea(height: IntArray): Int { + var i = 0 + var j = height.size - 1 + var max = 0 + + while (i < j) { + val h = minOf(height[i], height[j]) + max = maxOf(max, (j - i) * h) + + if (height[i] <= height[j]) i++ + else j-- + } + + return max + } +} diff --git a/design-add-and-search-words-data-structure/hyunjung-choi.kt b/design-add-and-search-words-data-structure/hyunjung-choi.kt new file mode 100644 index 000000000..ac63acef3 --- /dev/null +++ b/design-add-and-search-words-data-structure/hyunjung-choi.kt @@ -0,0 +1,46 @@ +class WordDictionary() { + + class TrieNode { + val children = mutableMapOf() + var isEndOfWord = false + } + + private val root = TrieNode() + + fun addWord(word: String) { + var current = root + + for (char in word) { + if (char !in current.children) { + current.children[char] = TrieNode() + } + current = current.children[char]!! + } + + current.isEndOfWord = true + } + + fun search(word: String): Boolean { + return searchHelper(word, 0, root) + } + + private fun searchHelper(word: String, index: Int, node: TrieNode): Boolean { + if (index == word.length) { + return node.isEndOfWord + } + + val char = word[index] + + return if (char == '.') { + for (child in node.children.values) { + if (searchHelper(word, index + 1, child)) { + return true + } + } + false + } else { + val child = node.children[char] ?: return false + searchHelper(word, index + 1, child) + } + } +} diff --git a/longest-increasing-subsequence/hyunjung-choi.kt b/longest-increasing-subsequence/hyunjung-choi.kt new file mode 100644 index 000000000..ac9a787ae --- /dev/null +++ b/longest-increasing-subsequence/hyunjung-choi.kt @@ -0,0 +1,33 @@ +class Solution { + fun lengthOfLIS(nums: IntArray): Int { + val tails = mutableListOf() + + for (num in nums) { + val pos = binarySearchLeftmost(tails, num) + + if (pos == tails.size) { + tails.add(num) + } else { + tails[pos] = num + } + } + + return tails.size + } + + fun binarySearchLeftmost(list: List, target: Int): Int { + var left = 0 + var right = list.size + + while (left < right) { + val mid = left + (right - left) / 2 + if (list[mid] < target) { + left = mid + 1 + } else { + right = mid + } + } + + return left + } +} diff --git a/valid-parentheses/hyunjung-choi.kt b/valid-parentheses/hyunjung-choi.kt new file mode 100644 index 000000000..2b750b98c --- /dev/null +++ b/valid-parentheses/hyunjung-choi.kt @@ -0,0 +1,27 @@ +/** + * 시간 복잡도: O(n) + * 공간 복잡도: O(n) + */ + +class Solution { + fun isValid(s: String): Boolean { + if (s.length % 2 == 1) return false + + val stack = Stack() + val pairs = mapOf(')' to '(', ']' to '[', '}' to '{') + + for (char in s) { + when { + char in pairs -> { + if (stack.isEmpty() || stack.pop() != pairs[char]) { + return false + } + } + + else -> stack.push(char) + } + } + + return stack.isEmpty() + } +}