Skip to content

Commit cc1ca5e

Browse files
committed
2문제 추가
1 parent f46ce28 commit cc1ca5e

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
7+
class `longest-substring-without-repeating-characters` {
8+
9+
fun lengthOfLongestSubstring(s: String): Int {
10+
if (s.length <= 1) return s.length
11+
return usingSet(s)
12+
}
13+
14+
/**
15+
* 1. 사용한 문자를 Set에 저장하여 확인하고, 중복된다면 해당 문자의 위치까지 모든 문자를 제거한다.
16+
* TC: O(n), SC: O(n)
17+
*/
18+
private fun usingSet(s: String): Int {
19+
var left = 0
20+
val used = mutableSetOf<Char>()
21+
var maxLength = 0
22+
23+
for (right in s.indices) {
24+
if (!used.contains(s[right])) {
25+
maxLength = max(right - left + 1, maxLength)
26+
used.add(s[right])
27+
} else {
28+
while (used.contains(s[right])) {
29+
used.remove(s[left])
30+
left++
31+
}
32+
used.add(s[right])
33+
}
34+
}
35+
36+
return maxLength
37+
}
38+
39+
@Test
40+
fun `입력받은 문자열의 반복되는 문자가 없는 가장 긴 부분 문자열의 길이를 반환한다`() {
41+
lengthOfLongestSubstring("ababc") shouldBe 3
42+
lengthOfLongestSubstring("bbbbb") shouldBe 1
43+
lengthOfLongestSubstring("abcabcbb") shouldBe 3
44+
lengthOfLongestSubstring("pwwkew") shouldBe 3
45+
}
46+
}

number-of-islands/jdalma.kt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `number-of-islands` {
7+
8+
private data class Position(
9+
val x: Int,
10+
val y: Int
11+
) {
12+
13+
operator fun plus(other: Position) = Position(this.x + other.x, this.y + other.y)
14+
15+
fun isNotOutOfIndexed(board: Array<CharArray>) =
16+
this.x < board.size && this.x >= 0 && this.y < board[0].size && this.y >= 0
17+
18+
companion object {
19+
val MOVES: List<Position> = listOf(
20+
Position(-1, 0),
21+
Position(0, 1),
22+
Position(1, 0),
23+
Position(0, -1),
24+
)
25+
}
26+
}
27+
28+
/**
29+
* BFS를 사용한 탐색
30+
* TC: O(n * m), SC: O(n * m)
31+
*/
32+
fun numIslands(grid: Array<CharArray>): Int {
33+
val (row, col) = grid.size to grid.first().size
34+
val visited = Array(row) { BooleanArray(col) }
35+
var result = 0
36+
37+
for (x in 0 until row) {
38+
for (y in 0 until col) {
39+
if (!visited[x][y] && grid[x][y] == '1') {
40+
visited[x][y] = true
41+
bfs(x, y, grid, visited)
42+
result++
43+
}
44+
}
45+
}
46+
return result
47+
}
48+
49+
private fun bfs(x: Int, y: Int, grid: Array<CharArray>, visited: Array<BooleanArray>) {
50+
val queue = ArrayDeque<Position>().apply {
51+
this.add(Position(x, y))
52+
}
53+
54+
while (queue.isNotEmpty()) {
55+
val now = queue.removeFirst()
56+
for (move in Position.MOVES) {
57+
val moved = now + move
58+
if (moved.isNotOutOfIndexed(grid) && grid[moved.x][moved.y] == '1' && !visited[moved.x][moved.y]) {
59+
visited[moved.x][moved.y] = true
60+
queue.add(moved)
61+
}
62+
}
63+
}
64+
}
65+
66+
67+
@Test
68+
fun `문자 이차원배열을 입력받으면 1로 이루어진 영역의 수를 반환한다`() {
69+
numIslands(arrayOf(
70+
charArrayOf('1','1','1','1','0'),
71+
charArrayOf('1','1','0','1','0'),
72+
charArrayOf('1','1','0','0','0'),
73+
charArrayOf('0','0','0','0','0')
74+
)) shouldBe 1
75+
76+
numIslands(arrayOf(
77+
charArrayOf('1','1','0','0','0'),
78+
charArrayOf('1','1','0','0','0'),
79+
charArrayOf('0','0','1','0','0'),
80+
charArrayOf('0','0','0','1','1')
81+
)) shouldBe 3
82+
}
83+
}

0 commit comments

Comments
 (0)