Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions container-with-most-water/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
46 changes: 46 additions & 0 deletions design-add-and-search-words-data-structure/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class WordDictionary() {

class TrieNode {
val children = mutableMapOf<Char, TrieNode>()
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)
}
}
}
27 changes: 27 additions & 0 deletions valid-parentheses/hyunjung-choi.kt
Original file line number Diff line number Diff line change
@@ -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<Char>()
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()
}
}