From 9cf683f9f1f8edbee38fceaf2added5b2dc83428 Mon Sep 17 00:00:00 2001 From: Real-Reason Date: Sat, 18 Jan 2025 00:23:01 +0900 Subject: [PATCH 1/2] feat: solve week 6 --- container-with-most-water/Real-Reason.kt | 26 +++++++++ .../Real-Reason.kt | 56 +++++++++++++++++++ valid-parentheses/Real-Reason.kt | 38 +++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 container-with-most-water/Real-Reason.kt create mode 100644 design-add-and-search-words-data-structure/Real-Reason.kt create mode 100644 valid-parentheses/Real-Reason.kt diff --git a/container-with-most-water/Real-Reason.kt b/container-with-most-water/Real-Reason.kt new file mode 100644 index 000000000..affd37d7b --- /dev/null +++ b/container-with-most-water/Real-Reason.kt @@ -0,0 +1,26 @@ +package leetcode_study + +/** + * 시간복잡도 : O(n) + * - 두개의 포인터를 이용하여 n의 길이를 가진 배열을 한 번 확인하므로 O(n) 입니다. + * + * 공간복잡도 : O(1) + * 변수로만 값을 저장하으므로 O(1) 입니다. + * */ +fun maxArea(height: IntArray): Int { + var maxWater = 0 + + var firstPoint = 0 + var secondPoint = height.size - 1 + + while (firstPoint != secondPoint) { + val width = secondPoint - firstPoint + val lessHeight = minOf(height[firstPoint], height[secondPoint]) + maxWater = maxOf(maxWater, width * lessHeight) + + if (height[firstPoint] < height[secondPoint]) firstPoint += 1 + else secondPoint -= 1 + } + + return maxWater +} diff --git a/design-add-and-search-words-data-structure/Real-Reason.kt b/design-add-and-search-words-data-structure/Real-Reason.kt new file mode 100644 index 000000000..163322a9e --- /dev/null +++ b/design-add-and-search-words-data-structure/Real-Reason.kt @@ -0,0 +1,56 @@ +package leetcode_study + +/** + * 단어의 길이 : s + * addWord 호출 횟수 : a + * wildCard ('.') 의 횟수 : w + * + * addWord() + * 시간복잡도 : O(s) + * - 단어의 모든 문자열을 순회하면서 Dictionary 에 추가하므로 시간복잡도는 O(s) 입니다. + * + * 공간복잡도 : O(s*a) + * - a개의 단어가 추가되고, 한 단어의 길이는 s 이므로 공간복잡도는 최대 O(s*a) 입니다. + * + * search() + * 시간복잡도 : O(s * 26^2) + * - 와일드카드가 없었을 경우 시간복잡도는 O(s) 이지만, + * 와일드카드가 있을 때 모든 노드를 탐색하므로 26(알파벳수)개의 경우의 수가 생기며 + * 이는 최대 2번 발생 가능하다고 기재되어 있으므로 O(s * 26^2) 만큼 시간복잡도가 발생합니다. + * */ +class Dictionary(var isEnd: Boolean = false) { + val nextChars = HashMap() +} +private val dictionary = Dictionary() + +fun addWord(word: String) { + var node = dictionary + word.forEach { char -> + if (char !in node.nextChars) { + node.nextChars[char] = Dictionary() + } + node = node.nextChars[char]!! + } + + node.isEnd = true +} + +fun search(word: String): Boolean { + var nodes = mutableListOf(dictionary) + word.forEach { char -> + if (char == '.') { + nodes = nodes.flatMap { it.nextChars.values }.toMutableList() + } + else { + val newNodes = mutableListOf() + nodes.forEach { + if (char in it.nextChars) { + newNodes.add(it.nextChars[char]!!) + } + } + nodes = newNodes + } + } + + return nodes.any { it.isEnd } +} \ No newline at end of file diff --git a/valid-parentheses/Real-Reason.kt b/valid-parentheses/Real-Reason.kt new file mode 100644 index 000000000..cfd3194e4 --- /dev/null +++ b/valid-parentheses/Real-Reason.kt @@ -0,0 +1,38 @@ +package leetcode_study + +/** + * 시간복잡도 : O(n) + * 문자를 하나씩 돌면서 스택에 추가 또는 확인하는 알고리즘이기 때문에 O(n) 입니다. + * + * 공간복잡도 : O(n) + * 문자가 모두 여는 형태의 괄호일 때, 스택의 길이는 최대 n 이므로 O(n) 입니다. + * */ +fun isValid(s: String): Boolean { + val chars = s.toCharArray() + val stack = ArrayDeque() + + val pairs = hashMapOf( + '(' to ')', + '{' to '}', + '[' to ']' + ) + + val openers = setOf('(', '[', '{') + val closers = setOf(')', ']', '}') + + chars.forEach { char -> + when (char) { + in openers -> { + stack.add(char) + } + in closers -> { + if (stack.isEmpty()) return false + + val recentOpener = stack.removeLast() + if (pairs[recentOpener] != char) return false + } + } + } + + return stack.isEmpty() +} From 774aaf7f7eab8cebe09e3ffd29935ea492ab0747 Mon Sep 17 00:00:00 2001 From: Real-Reason Date: Sat, 18 Jan 2025 00:26:57 +0900 Subject: [PATCH 2/2] fix: ktlint --- design-add-and-search-words-data-structure/Real-Reason.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/Real-Reason.kt b/design-add-and-search-words-data-structure/Real-Reason.kt index 163322a9e..28bdd0732 100644 --- a/design-add-and-search-words-data-structure/Real-Reason.kt +++ b/design-add-and-search-words-data-structure/Real-Reason.kt @@ -53,4 +53,4 @@ fun search(word: String): Boolean { } return nodes.any { it.isEnd } -} \ No newline at end of file +}