Skip to content

[이유진] Week 06 #912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions container-with-most-water/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -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
}
56 changes: 56 additions & 0 deletions design-add-and-search-words-data-structure/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -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<Char, Dictionary>()
}
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<Dictionary>()
nodes.forEach {
if (char in it.nextChars) {
newNodes.add(it.nextChars[char]!!)
}
}
nodes = newNodes
}
}

return nodes.any { it.isEnd }
}
38 changes: 38 additions & 0 deletions valid-parentheses/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -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<Char>()

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()
}
Loading