-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from EcoFriendlyAppleSu/main
[์นํ๊ฒฝ์ฌ๊ณผ] Week 6
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
design-add-and-search-words-data-structure/EcoFriendlyAppleSu.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |