Skip to content

Commit 0489f69

Browse files
authored
Merge pull request #885 from gmlwls96/main
[gmlwls96] Week 6
2 parents ea2e89a + c034d88 commit 0489f69

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

container-with-most-water/gmlwls96.kt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
/** 시간 : O(n), 공간 : O(1)*/
3+
fun maxArea(height: IntArray): Int {
4+
var maxDiff = 0
5+
var left = 0
6+
var right = height.lastIndex
7+
// left, right값을 순차적으로 조회해서 물높이를 구하고,
8+
// left < right값 보다 작으면 left증가시킨다. 반대는 right 감소
9+
while (left < right) {
10+
maxDiff = max(maxDiff, (right - left) * min(height[left], height[right]))
11+
// 너비 : right - left
12+
// 현재 높이 : min(height[left], height[right])
13+
// 너비 * 현재 높이가 maxDiff 비교하여 더 큰값이 maxDiff가 된다.
14+
if (height[left] < height[right]) {
15+
left++
16+
} else {
17+
right--
18+
}
19+
}
20+
return maxDiff
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Node() {
2+
val map = mutableMapOf<Char, Node?>()
3+
var isEnd = false
4+
}
5+
6+
class WordDictionary() {
7+
8+
val rootNode = Node()
9+
10+
// 시간 : O(n), 공간 : O(n)
11+
fun addWord(word: String) {
12+
var currentNode = rootNode
13+
for (i in word.indices) {
14+
val char = word[i]
15+
if (currentNode.map[char] == null) {
16+
currentNode.map[char] = Node()
17+
}
18+
currentNode = currentNode.map[char]!!
19+
}
20+
currentNode.isEnd = true
21+
}
22+
23+
// 시간 : O(26*n), 공간: O(n)
24+
fun search(word: String): Boolean {
25+
return dfs(
26+
pos = 0,
27+
word = word,
28+
node = rootNode
29+
)
30+
}
31+
32+
fun dfs(pos: Int, word: String, node: Node): Boolean {
33+
var result = false
34+
val char = word[pos]
35+
val isLast = word.lastIndex == pos
36+
node.map.forEach {
37+
if (char == '.' || char == it.key) {
38+
if (isLast) {
39+
result = true
40+
return@forEach
41+
} else {
42+
result = result or dfs(pos + 1, word, it.value!!)
43+
}
44+
}
45+
}
46+
return result
47+
}
48+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
// 시간 : O(n), 공간 : O(n)
3+
// nums를 조회하면서 이전값과 비교하여
4+
// 더 증가하였으면 : 이전 카운트 +1
5+
// 같거나 작으면 : 이전 카운트값
6+
fun lengthOfLIS(nums: IntArray): Int {
7+
val count = IntArray(nums.size)
8+
count[0] = 1
9+
var prev = nums[0]
10+
for (i in 1 until nums.size) {
11+
if (prev < nums[i]) {
12+
count[i] += count[i - 1] + 1
13+
} else {
14+
count[i] = count[i - 1]
15+
}
16+
prev = nums[i]
17+
}
18+
return count.last()
19+
}
20+
}

spiral-matrix/gmlwls96.kt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
// 시간 : O(y*x), 공간 : O(1)
3+
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
4+
val result = mutableListOf<Int>()
5+
if (matrix.isEmpty() || matrix[0].isEmpty()) return result
6+
7+
var top = 0
8+
var bottom = matrix.size - 1
9+
var left = 0
10+
var right = matrix[0].size - 1
11+
12+
while (top <= bottom && left <= right) {
13+
for (i in left..right) result.add(matrix[top][i])
14+
top++
15+
for (i in top..bottom) result.add(matrix[i][right])
16+
right--
17+
if (top <= bottom) {
18+
for (i in right downTo left) result.add(matrix[bottom][i])
19+
bottom--
20+
}
21+
if (left <= right) {
22+
for (i in bottom downTo top) result.add(matrix[i][left])
23+
left++
24+
}
25+
}
26+
27+
return result
28+
}
29+
}

valid-parentheses/gmlwls96.kt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode_study
2+
3+
class Solution {
4+
/** 시간 : O(n), 공간 : O(n) */
5+
fun isValid(s: String): Boolean {
6+
val stack = Stack<Char>()
7+
val openParentheses = "([{"
8+
s.forEach {
9+
if (openParentheses.contains(it)) {
10+
stack.push(it)
11+
} else {
12+
if (stack.isEmpty()) {
13+
return false
14+
}
15+
val top = stack.pop()
16+
if (
17+
top == openParentheses[0] && it != ')' ||
18+
top == openParentheses[1] && it != ']' ||
19+
top == openParentheses[2] && it != '}'
20+
) {
21+
return false
22+
}
23+
}
24+
}
25+
return stack.isEmpty()
26+
}
27+
}

0 commit comments

Comments
 (0)