Skip to content

Commit b8ab2d0

Browse files
committed
6주차 답안 제출
1 parent 189509b commit b8ab2d0

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

container-with-most-water/jdalma.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.abs
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
typealias Element = Pair<Int,Int>
10+
11+
class `container-with-most-wate` {
12+
13+
/**
14+
* 투 포인터를 활용하여 높이가 낮은 인덱스의 값을 한 칸씩 이동하며 최대 높이를 반환한다.
15+
* TC: O(n), SC: O(1)
16+
*/
17+
fun maxArea(height: IntArray): Int {
18+
var (left, right) = 0 to height.size - 1
19+
var result = 0
20+
21+
while (left < right) {
22+
result = max(result, extent(Element(height[left], left), Element(height[right], right)))
23+
if (height[left] < height[right]) {
24+
left++
25+
} else {
26+
right--
27+
}
28+
}
29+
return result
30+
}
31+
32+
private fun extent(e1: Element, e2: Element): Int {
33+
return min(e1.first, e2.first) * abs(e1.second - e2.second)
34+
}
35+
36+
@Test
37+
fun `입력받은 높이를 표현하는 정수 배열에서 받을 수 있는 최대의 물의 양을 반환한다`() {
38+
maxArea(intArrayOf(1,8,6,2,5,4,8,3,7)) shouldBe 49
39+
}
40+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import leetcode_study.Type.*
5+
import org.junit.jupiter.api.Test
6+
7+
class `design-add-and-search-words-data-structure` {
8+
9+
class Node {
10+
var isEnd: Boolean = false
11+
val next: MutableMap<Char, Node> = mutableMapOf()
12+
}
13+
14+
class WordDictionary {
15+
private val root = Node()
16+
17+
/**
18+
* TC: O(n), SC: O(n)
19+
*/
20+
fun addWord(word: String) {
21+
var now = root
22+
23+
for (index in word.indices) {
24+
val node =
25+
if (now.next.containsKey(word[index])) { now.next[word[index]] }
26+
else Node().also { now.next[word[index]] = it }
27+
node?.let { now = it }
28+
}
29+
now.isEnd = true
30+
}
31+
32+
/**
33+
* TC: O(26^n), SC: O(n)
34+
*/
35+
fun search(word: String): Boolean {
36+
37+
fun dfs(node: Node, word: String, index: Int): Boolean {
38+
return if (index == word.length) node.isEnd
39+
else if (word[index] == '.') {
40+
node.next.values.any { dfs(it, word, index + 1) }
41+
}
42+
else {
43+
node.next[word[index]]?.let { dfs(it, word, index + 1) } ?: false
44+
}
45+
}
46+
47+
return dfs(this.root, word, 0)
48+
}
49+
}
50+
51+
@Test
52+
fun `문자열을 저장하고 검색하는 자료구조를 구현하라`() {
53+
inputCommands(
54+
Command(ADD, "bad"),
55+
Command(ADD, "dad"),
56+
Command(ADD, "mad"),
57+
Command(SEARCH, "pad", false),
58+
Command(SEARCH, "bad", true),
59+
Command(SEARCH, ".ad", true),
60+
Command(SEARCH, "b..", true),
61+
)
62+
inputCommands(
63+
Command(ADD, "at"),
64+
Command(ADD, "and"),
65+
Command(ADD, "an"),
66+
Command(ADD, "add"),
67+
Command(SEARCH, "a", false),
68+
Command(SEARCH, ".at", false),
69+
Command(ADD, "bat"),
70+
Command(SEARCH,".at", true),
71+
Command(SEARCH,"an.", true),
72+
Command(SEARCH,"a.d.", false),
73+
Command(SEARCH,"b.", false),
74+
Command(SEARCH,"a.d", true),
75+
Command(SEARCH,".", false)
76+
)
77+
}
78+
79+
private fun inputCommands(vararg commands: Command) {
80+
val dictionary = WordDictionary()
81+
for (command in commands) {
82+
when(command.type) {
83+
ADD -> dictionary.addWord(command.word)
84+
SEARCH -> dictionary.search(command.word) shouldBe command.expect
85+
}
86+
}
87+
}
88+
}
89+
90+
data class Command(
91+
val type: Type,
92+
val word: String,
93+
val expect : Boolean? = null
94+
)
95+
96+
enum class Type {
97+
ADD, SEARCH;
98+
}

valid-parentheses/jdalma.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import java.util.Deque
6+
import java.util.ArrayDeque
7+
8+
class `valid-parentheses` {
9+
10+
/**
11+
* 괄호의 쌍을 Stack을 활용하여 검증한다.
12+
* TC: O(n), SC: O(n)
13+
*/
14+
fun isValid(s: String): Boolean {
15+
if (s.length % 2 != 0) return false
16+
val parentheses = mapOf(
17+
'(' to ')',
18+
'{' to '}',
19+
'[' to ']'
20+
)
21+
22+
val stack: Deque<Char> = ArrayDeque()
23+
for (char in s) {
24+
if (parentheses.containsKey(char)) {
25+
stack.push(char)
26+
} else if (stack.isEmpty() || parentheses[stack.pop()] != char){
27+
return false
28+
}
29+
}
30+
return stack.isEmpty()
31+
}
32+
33+
@Test
34+
fun `입력한 문자열의 괄호의 열림과 닫힘을 검증한다`() {
35+
isValid("()") shouldBe true
36+
isValid("{()}") shouldBe true
37+
isValid("(){}[]") shouldBe true
38+
39+
isValid("{(}") shouldBe false
40+
isValid("){") shouldBe false
41+
}
42+
}

0 commit comments

Comments
 (0)