Skip to content

Commit a61ca49

Browse files
Merge pull request #1862 from hyunjung-choi/main
[hyunjung-choi] WEEK 06 solutions
2 parents 48443af + 9265fe3 commit a61ca49

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(1)
4+
*/
5+
6+
class Solution {
7+
fun maxArea(height: IntArray): Int {
8+
var i = 0
9+
var j = height.size - 1
10+
var max = 0
11+
12+
while (i < j) {
13+
val h = minOf(height[i], height[j])
14+
max = maxOf(max, (j - i) * h)
15+
16+
if (height[i] <= height[j]) i++
17+
else j--
18+
}
19+
20+
return max
21+
}
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class WordDictionary() {
2+
3+
class TrieNode {
4+
val children = mutableMapOf<Char, TrieNode>()
5+
var isEndOfWord = false
6+
}
7+
8+
private val root = TrieNode()
9+
10+
fun addWord(word: String) {
11+
var current = root
12+
13+
for (char in word) {
14+
if (char !in current.children) {
15+
current.children[char] = TrieNode()
16+
}
17+
current = current.children[char]!!
18+
}
19+
20+
current.isEndOfWord = true
21+
}
22+
23+
fun search(word: String): Boolean {
24+
return searchHelper(word, 0, root)
25+
}
26+
27+
private fun searchHelper(word: String, index: Int, node: TrieNode): Boolean {
28+
if (index == word.length) {
29+
return node.isEndOfWord
30+
}
31+
32+
val char = word[index]
33+
34+
return if (char == '.') {
35+
for (child in node.children.values) {
36+
if (searchHelper(word, index + 1, child)) {
37+
return true
38+
}
39+
}
40+
false
41+
} else {
42+
val child = node.children[char] ?: return false
43+
searchHelper(word, index + 1, child)
44+
}
45+
}
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
fun lengthOfLIS(nums: IntArray): Int {
3+
val tails = mutableListOf<Int>()
4+
5+
for (num in nums) {
6+
val pos = binarySearchLeftmost(tails, num)
7+
8+
if (pos == tails.size) {
9+
tails.add(num)
10+
} else {
11+
tails[pos] = num
12+
}
13+
}
14+
15+
return tails.size
16+
}
17+
18+
fun binarySearchLeftmost(list: List<Int>, target: Int): Int {
19+
var left = 0
20+
var right = list.size
21+
22+
while (left < right) {
23+
val mid = left + (right - left) / 2
24+
if (list[mid] < target) {
25+
left = mid + 1
26+
} else {
27+
right = mid
28+
}
29+
}
30+
31+
return left
32+
}
33+
}

valid-parentheses/hyunjung-choi.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
6+
class Solution {
7+
fun isValid(s: String): Boolean {
8+
if (s.length % 2 == 1) return false
9+
10+
val stack = Stack<Char>()
11+
val pairs = mapOf(')' to '(', ']' to '[', '}' to '{')
12+
13+
for (char in s) {
14+
when {
15+
char in pairs -> {
16+
if (stack.isEmpty() || stack.pop() != pairs[char]) {
17+
return false
18+
}
19+
}
20+
21+
else -> stack.push(char)
22+
}
23+
}
24+
25+
return stack.isEmpty()
26+
}
27+
}

0 commit comments

Comments
 (0)