Skip to content

Commit

Permalink
Merge pull request #900 from EcoFriendlyAppleSu/main
Browse files Browse the repository at this point in the history
[์นœํ™˜๊ฒฝ์‚ฌ๊ณผ] Week 6
  • Loading branch information
EcoFriendlyAppleSu authored Jan 19, 2025
2 parents 50a6e9e + 12348f9 commit f39ef72
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
59 changes: 59 additions & 0 deletions container-with-most-water/EcoFriendlyAppleSu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package leetcode_study

/*
* ์ฃผ์–ด์ง„ ๋†’์ด์—์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
* Brute force๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ ธ๊ฐ€๋ฉฐ ์ตœ๋Œ€ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•.
* ์ฃผ์–ด์ง„ ๋†’์ด(n)์˜ ๊ฐœ์ˆ˜๋Š” 2 ๋ณด๋‹ค ํฌ๊ฑฐ๊ฐ€ ๊ฐ™๊ณ  10^4 ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ.
* ์ด์ค‘ Loop์œผ๋กœ ํ•ด๊ฒฐํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ (10^8 ์ดํ•˜๋กœ ํ•ด๊ฒฐํ•ด์•ผ ์ œํ•œ ์‹œ๊ฐ„ ์•ˆ์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ)
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
* -> ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋†’์ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰: O(n^2)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* -> ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์Œ.
* */
fun maxArea01(height: IntArray): Int {
var maxValue = 0
for (i in 0 until height.size) {
for (j in i + 1 until height.size) {
// ๋„ˆ๋น„๋Š” ๋‘ ์„ ๋ถ„ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
val width = j - i
// ๋†’์ด๋Š” ๋‘ ์„ ๋ถ„ ์ค‘ ์ž‘์€ ๊ฐ’
val containerHeight = Math.min(height[i], height[j])
// ๋ฉด์  ๊ณ„์‚ฐ
val area = width * containerHeight
// ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
maxValue = Math.max(maxValue, area)
}
}
return maxValue
}

/*
* ์ด์ค‘ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด.
* ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์กฐ๊ฑด๊ณผ ์š”์†Œ
* -> ๋†’์ด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์™ผ์ชฝ๊ฐ’๊ณผ ์˜ค๋ฅธ์ชฝ ๊ฐ’์—์„œ ๋‘ ๊ฐ’ ์ค‘ ์ž‘์€ ๊ฐ’์ด ๋†’์ด๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ.
* -> ์˜ค๋ฅธ์ชฝ์˜ ๊ฐ’์€ ์™ผ์ชฝ ๊ฐ’๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์—†์Œ.
* -> ๋„ˆ๋น„ ๊ฐ’์€ ์˜ค๋ฅธ์ชฝ ์ธ๋ฑ์Šค์—์„œ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ๋บ€ ๊ฐ’์ž„.
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* -> ์ฃผ์–ด์ง„ ๋†’์ด ๋ฐฐ์—ด์—์„œ ์–‘์ชฝ ๋ ๊ฐ’์„ ์ฆ๊ฐ/๊ฐ€๊ฐ ํ•ด๊ฐ€๋ฉฐ ๋ฐ˜๋ณต ์ง„ํ–‰: O(n)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* -> ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์Œ.
* */
fun maxArea02(height: IntArray): Int {
var maxValue = 0
var left = 0
var right = height.size - 1
while (left <= right) {
val width = right - left
val containerHeight = Math.min(height[left], height[right])
val area = width * containerHeight
maxValue = Math.max(maxValue, area)
if (height[left] < height[right]) {
left++
} else {
right--
}
}
return maxValue
}
61 changes: 61 additions & 0 deletions design-add-and-search-words-data-structure/EcoFriendlyAppleSu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package leetcode_study


/*
* ๋‹จ์–ด ์‚ฌ์ „์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ. (์ฐพ๋Š” ๋‹จ์–ด๋Š” wildcard๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Œ)
* https://www.youtube.com/watch?v=TohdsR58i3Q ๋™์˜์ƒ ์ฐธ์กฐ
* trie ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(L * 26)
* -> addWord() ์˜ ๊ฒฝ์šฐ, ๊ฐ ๋…ธ๋“œ๋Š” ์ตœ๋Œ€ 26๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ L์ด๋ผ๋ฉด O(L)์˜ ์‹œ๊ฐ„ ์†Œ์š”: O(L)
* -> searchWord() ์˜ ๊ฒฝ์šฐ, ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž์‹์˜ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ 26(์•ŒํŒŒ๋ฒณ ์ˆ˜) ๋ฒˆ์˜ ํƒ์ƒ‰ ์ง„ํ–‰: O(L * 26)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n * L)
* -> n ๊ฐœ์˜ ๋‹จ์–ด, ํ‰๊ท ์  ๋‹จ์–ด ๊ธธ์ด๊ฐ€ L์ผ ๊ฒฝ์šฐ ํŠธ๋ฆฌ ๋…ธ๋“œ์— ์ €์žฅ๋œ ๋…ธ๋“œ์˜ ์ˆ˜๋Š” O(n * L)
* */
class WordDictionary() {
// Trie ๋…ธ๋“œ ํด๋ž˜์Šค
private class TrieNode {
val children: MutableMap<Char, TrieNode> = mutableMapOf()
var endOfWord: Boolean = false
}

private val root = TrieNode()

// ๋‹จ์–ด๋ฅผ ํŠธ๋ผ์ด์— ์ถ”๊ฐ€
fun addWord(word: String) {
var currentNode = root
for (char in word) {
if (!currentNode.children.containsKey(char)) {
currentNode.children[char] = TrieNode() // ํ•ด๋‹น ๋ฌธ์ž์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ƒ์„ฑ
}
currentNode = currentNode.children[char]!! // point out next trie node
}
currentNode.endOfWord = 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.endOfWord // ๋‹จ์–ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๋‹จ์–ด์˜ ๋์ธ์ง€ ํ™•์ธ
}

val char = word[index]

if (char == '.') { // '.'์ด๋ฉด ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ์— ๋Œ€ํ•ด ํƒ์ƒ‰
for (childNode in node.children.values) {
if (searchHelper(word, index + 1, childNode)) {
return true
}
}
return false // '.'์„ ์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ ์ผ์น˜ํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด false
} else {
// ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•˜๋Š” ์ž์‹ ๋…ธ๋“œ๋กœ ๊ณ„์† ํƒ์ƒ‰
val childNode = node.children[char] ?: return false
return searchHelper(word, index + 1, childNode)
}
}
}
32 changes: 32 additions & 0 deletions valid-parentheses/EcoFriendlyAppleSu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package leetcode_study

/*
* Pair Bracket ํŒ๋ณ„ ๋ฌธ์ œ
* ์Šคํƒ์˜ LIFO์˜ ์„ฑ์งˆ์„ ์‚ฌ์šฉํ•ด ๋ฌธ์ œ ํ•ด๊ฒฐ
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* -> ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต ์ง„ํ–‰: O(n)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
* -> ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๊ณต๊ฐ„ ํ•„์š”: O(n)
* */
fun isValid(s: String): Boolean {
val stack = mutableListOf<Char>()

for (element in s) {
if (element == ')' || element == ']' || element == '}') {
if (stack.isEmpty()) {
return false
} else if (stack.last() == '(' && element == ')') {
stack.removeAt(stack.lastIndex)
} else if (stack.last() == '[' && element == ']') {
stack.removeAt(stack.lastIndex)
} else if (stack.last() == '{' && element == '}') {
stack.removeAt(stack.lastIndex)
} else {
return false
}
} else {
stack.add(element)
}
}
return stack.isEmpty()
}

0 comments on commit f39ef72

Please sign in to comment.