Skip to content

Commit a6a01ff

Browse files
authored
Merge pull request #912 from Real-Reason/main
[이유진] Week 06
2 parents 3b3ebf6 + 774aaf7 commit a6a01ff

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode_study
2+
3+
/**
4+
* 시간복잡도 : O(n)
5+
* - 두개의 포인터를 이용하여 n의 길이를 가진 배열을 한 번 확인하므로 O(n) 입니다.
6+
*
7+
* 공간복잡도 : O(1)
8+
* 변수로만 값을 저장하으므로 O(1) 입니다.
9+
* */
10+
fun maxArea(height: IntArray): Int {
11+
var maxWater = 0
12+
13+
var firstPoint = 0
14+
var secondPoint = height.size - 1
15+
16+
while (firstPoint != secondPoint) {
17+
val width = secondPoint - firstPoint
18+
val lessHeight = minOf(height[firstPoint], height[secondPoint])
19+
maxWater = maxOf(maxWater, width * lessHeight)
20+
21+
if (height[firstPoint] < height[secondPoint]) firstPoint += 1
22+
else secondPoint -= 1
23+
}
24+
25+
return maxWater
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode_study
2+
3+
/**
4+
* 단어의 길이 : s
5+
* addWord 호출 횟수 : a
6+
* wildCard ('.') 의 횟수 : w
7+
*
8+
* addWord()
9+
* 시간복잡도 : O(s)
10+
* - 단어의 모든 문자열을 순회하면서 Dictionary 에 추가하므로 시간복잡도는 O(s) 입니다.
11+
*
12+
* 공간복잡도 : O(s*a)
13+
* - a개의 단어가 추가되고, 한 단어의 길이는 s 이므로 공간복잡도는 최대 O(s*a) 입니다.
14+
*
15+
* search()
16+
* 시간복잡도 : O(s * 26^2)
17+
* - 와일드카드가 없었을 경우 시간복잡도는 O(s) 이지만,
18+
* 와일드카드가 있을 때 모든 노드를 탐색하므로 26(알파벳수)개의 경우의 수가 생기며
19+
* 이는 최대 2번 발생 가능하다고 기재되어 있으므로 O(s * 26^2) 만큼 시간복잡도가 발생합니다.
20+
* */
21+
class Dictionary(var isEnd: Boolean = false) {
22+
val nextChars = HashMap<Char, Dictionary>()
23+
}
24+
private val dictionary = Dictionary()
25+
26+
fun addWord(word: String) {
27+
var node = dictionary
28+
word.forEach { char ->
29+
if (char !in node.nextChars) {
30+
node.nextChars[char] = Dictionary()
31+
}
32+
node = node.nextChars[char]!!
33+
}
34+
35+
node.isEnd = true
36+
}
37+
38+
fun search(word: String): Boolean {
39+
var nodes = mutableListOf(dictionary)
40+
word.forEach { char ->
41+
if (char == '.') {
42+
nodes = nodes.flatMap { it.nextChars.values }.toMutableList()
43+
}
44+
else {
45+
val newNodes = mutableListOf<Dictionary>()
46+
nodes.forEach {
47+
if (char in it.nextChars) {
48+
newNodes.add(it.nextChars[char]!!)
49+
}
50+
}
51+
nodes = newNodes
52+
}
53+
}
54+
55+
return nodes.any { it.isEnd }
56+
}

valid-parentheses/Real-Reason.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode_study
2+
3+
/**
4+
* 시간복잡도 : O(n)
5+
* 문자를 하나씩 돌면서 스택에 추가 또는 확인하는 알고리즘이기 때문에 O(n) 입니다.
6+
*
7+
* 공간복잡도 : O(n)
8+
* 문자가 모두 여는 형태의 괄호일 때, 스택의 길이는 최대 n 이므로 O(n) 입니다.
9+
* */
10+
fun isValid(s: String): Boolean {
11+
val chars = s.toCharArray()
12+
val stack = ArrayDeque<Char>()
13+
14+
val pairs = hashMapOf(
15+
'(' to ')',
16+
'{' to '}',
17+
'[' to ']'
18+
)
19+
20+
val openers = setOf('(', '[', '{')
21+
val closers = setOf(')', ']', '}')
22+
23+
chars.forEach { char ->
24+
when (char) {
25+
in openers -> {
26+
stack.add(char)
27+
}
28+
in closers -> {
29+
if (stack.isEmpty()) return false
30+
31+
val recentOpener = stack.removeLast()
32+
if (pairs[recentOpener] != char) return false
33+
}
34+
}
35+
}
36+
37+
return stack.isEmpty()
38+
}

0 commit comments

Comments
 (0)