-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from jdalma/main
[์ ํ์ค] 7์ฃผ์ฐจ
- Loading branch information
Showing
5 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import kotlin.math.max | ||
|
||
class `longest-substring-without-repeating-characters` { | ||
|
||
fun lengthOfLongestSubstring(s: String): Int { | ||
if (s.length <= 1) return s.length | ||
return usingSet(s) | ||
} | ||
|
||
/** | ||
* 1. ์ฌ์ฉํ ๋ฌธ์๋ฅผ Set์ ์ ์ฅํ์ฌ ํ์ธํ๊ณ , ์ค๋ณต๋๋ค๋ฉด ํด๋น ๋ฌธ์์ ์์น๊น์ง ๋ชจ๋ ๋ฌธ์๋ฅผ ์ ๊ฑฐํ๋ค. | ||
* TC: O(n), SC: O(n) | ||
*/ | ||
private fun usingSet(s: String): Int { | ||
var left = 0 | ||
val used = mutableSetOf<Char>() | ||
var maxLength = 0 | ||
|
||
for (right in s.indices) { | ||
if (!used.contains(s[right])) { | ||
maxLength = max(right - left + 1, maxLength) | ||
used.add(s[right]) | ||
} else { | ||
while (used.contains(s[right])) { | ||
used.remove(s[left]) | ||
left++ | ||
} | ||
used.add(s[right]) | ||
} | ||
} | ||
|
||
return maxLength | ||
} | ||
|
||
@Test | ||
fun `์ ๋ ฅ๋ฐ์ ๋ฌธ์์ด์ ๋ฐ๋ณต๋๋ ๋ฌธ์๊ฐ ์๋ ๊ฐ์ฅ ๊ธด ๋ถ๋ถ ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋ค`() { | ||
lengthOfLongestSubstring("ababc") shouldBe 3 | ||
lengthOfLongestSubstring("bbbbb") shouldBe 1 | ||
lengthOfLongestSubstring("abcabcbb") shouldBe 3 | ||
lengthOfLongestSubstring("pwwkew") shouldBe 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `number-of-islands` { | ||
|
||
private data class Position( | ||
val x: Int, | ||
val y: Int | ||
) { | ||
|
||
operator fun plus(other: Position) = Position(this.x + other.x, this.y + other.y) | ||
|
||
fun isNotOutOfIndexed(board: Array<CharArray>) = | ||
this.x < board.size && this.x >= 0 && this.y < board[0].size && this.y >= 0 | ||
|
||
companion object { | ||
val MOVES: List<Position> = listOf( | ||
Position(-1, 0), | ||
Position(0, 1), | ||
Position(1, 0), | ||
Position(0, -1), | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* BFS๋ฅผ ์ฌ์ฉํ ํ์ | ||
* TC: O(n * m), SC: O(n * m) | ||
*/ | ||
fun numIslands(grid: Array<CharArray>): Int { | ||
val (row, col) = grid.size to grid.first().size | ||
val visited = Array(row) { BooleanArray(col) } | ||
var result = 0 | ||
|
||
for (x in 0 until row) { | ||
for (y in 0 until col) { | ||
if (!visited[x][y] && grid[x][y] == '1') { | ||
visited[x][y] = true | ||
bfs(x, y, grid, visited) | ||
result++ | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private fun bfs(x: Int, y: Int, grid: Array<CharArray>, visited: Array<BooleanArray>) { | ||
val queue = ArrayDeque<Position>().apply { | ||
this.add(Position(x, y)) | ||
} | ||
|
||
while (queue.isNotEmpty()) { | ||
val now = queue.removeFirst() | ||
for (move in Position.MOVES) { | ||
val moved = now + move | ||
if (moved.isNotOutOfIndexed(grid) && grid[moved.x][moved.y] == '1' && !visited[moved.x][moved.y]) { | ||
visited[moved.x][moved.y] = true | ||
queue.add(moved) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun `๋ฌธ์ ์ด์ฐจ์๋ฐฐ์ด์ ์ ๋ ฅ๋ฐ์ผ๋ฉด 1๋ก ์ด๋ฃจ์ด์ง ์์ญ์ ์๋ฅผ ๋ฐํํ๋ค`() { | ||
numIslands(arrayOf( | ||
charArrayOf('1','1','1','1','0'), | ||
charArrayOf('1','1','0','1','0'), | ||
charArrayOf('1','1','0','0','0'), | ||
charArrayOf('0','0','0','0','0') | ||
)) shouldBe 1 | ||
|
||
numIslands(arrayOf( | ||
charArrayOf('1','1','0','0','0'), | ||
charArrayOf('1','1','0','0','0'), | ||
charArrayOf('0','0','1','0','0'), | ||
charArrayOf('0','0','0','1','1') | ||
)) shouldBe 3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `reverse-linked-list` { | ||
|
||
/** | ||
* TC : O(n), SC: O(1) | ||
*/ | ||
fun reverseList(head: ListNode?): ListNode? { | ||
var prev: ListNode? = null | ||
var next: ListNode? = null | ||
var curr: ListNode? = head | ||
|
||
while (curr != null) { | ||
next = curr.next | ||
curr.next = prev | ||
prev = curr | ||
curr = next | ||
} | ||
|
||
return prev | ||
} | ||
|
||
@Test | ||
fun `์ ๋ ฅ ๋ฐ์ ๋จ์ผ ๋ฆฌ์คํธ๋ฅผ ๋ฐ์ ํ๊ณ ๋ฐ์ ํ ๋ชฉ๋ก์ ๋ฐํํ๋ค`() { | ||
reverseList(ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))) | ||
shouldBe(ListNode(5, ListNode(4, ListNode(3, ListNode(2, ListNode(1)))))) | ||
} | ||
} | ||
|
||
class ListNode( | ||
var `val`: Int, | ||
var next: ListNode? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import kotlin.math.max | ||
|
||
class `set-matrix-zeroes` { | ||
|
||
private data class Position( | ||
val x: Int, | ||
val y: Int | ||
) | ||
|
||
fun setZeroes(matrix: Array<IntArray>): Unit { | ||
usingFlag(matrix) | ||
} | ||
|
||
/** | ||
* 0์ผ๋ก ๋ณ๊ฒฝํด์ผ ํ ์ด๊ณผ ํ์ Set์ ๋ด์ ์ฒ๋ฆฌํ๋ค. | ||
* TC: O(n * m) SC: O(n + m) | ||
*/ | ||
private fun usingSet(matrix: Array<IntArray>) { | ||
val zeroRows = mutableSetOf<Int>() | ||
val zeroCols = mutableSetOf<Int>() | ||
for (i in matrix.indices) { | ||
for (j in matrix.first().indices) { | ||
if (matrix[i][j] == 0) { | ||
zeroRows.add(i) | ||
zeroCols.add(j) | ||
} | ||
} | ||
} | ||
|
||
for (row in zeroRows) { | ||
for (col in matrix.first().indices) { | ||
matrix[row][col] = 0 | ||
} | ||
} | ||
|
||
for (col in zeroCols) { | ||
for (row in matrix.indices) { | ||
matrix[row][col] = 0 | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 0์ผ๋ก ๋ณ๊ฒฝํด์ผ ํ ์ด๊ณผ ํ์ matrix 0๋ฒ์งธ ํ๊ณผ 0๋ฒ์งธ ์ด ๊ทธ๋ฆฌ๊ณ ๋ ๊ฐ์ flag๋ก ์ฒ๋ฆฌํ์ฌ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ ํ๋ค. | ||
* TC: O(n * m) SC: O(1) | ||
*/ | ||
private fun usingFlag(matrix: Array<IntArray>) { | ||
var (rowFlag, colFlag) = false to false | ||
for (i in matrix.indices) { | ||
for (j in matrix.first().indices) { | ||
if (matrix[i][j] == 0) { | ||
if (i == 0) rowFlag = true | ||
if (j == 0) colFlag = true | ||
matrix[0][j] = 0 | ||
matrix[i][0] = 0 | ||
} | ||
} | ||
} | ||
|
||
for (i in 1 until matrix.size) { | ||
for (j in 1 until matrix.first().size) { | ||
if (matrix[i][0] == 0 || matrix[0][j] == 0) { | ||
matrix[i][j] = 0 | ||
} | ||
} | ||
} | ||
|
||
if (rowFlag) { | ||
for (i in matrix.first().indices) { | ||
matrix[0][i] = 0 | ||
} | ||
} | ||
|
||
if (colFlag) { | ||
for (element in matrix) { | ||
element[0] = 0 | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `์์๊ฐ 0์ด๋ผ๋ฉด ํด๋น ํ๊ณผ ์ด์ ๋ชจ๋ 0์ผ๋ก ์์ ํ๋ค`() { | ||
val actual1 = arrayOf( | ||
intArrayOf(1,1,1), | ||
intArrayOf(1,0,1), | ||
intArrayOf(1,1,1) | ||
) | ||
setZeroes(actual1) | ||
actual1 shouldBe arrayOf( | ||
intArrayOf(1,0,1), | ||
intArrayOf(0,0,0), | ||
intArrayOf(1,0,1) | ||
) | ||
|
||
val actual2 = arrayOf( | ||
intArrayOf(0,1,2,0), | ||
intArrayOf(3,4,5,2), | ||
intArrayOf(1,3,1,5) | ||
) | ||
setZeroes(actual2) | ||
actual2 shouldBe arrayOf( | ||
intArrayOf(0,0,0,0), | ||
intArrayOf(0,4,5,0), | ||
intArrayOf(0,3,1,0) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
/** | ||
* 0,0 ์์ ์๋ ๋๋ ์ค๋ฅธ์ชฝ์ผ๋ก๋ง ์ด๋ ๊ฐ๋ฅํ๋ค. | ||
*/ | ||
class `unique-paths` { | ||
|
||
fun uniquePaths(row: Int, col: Int): Int { | ||
return if (row <= 1 || col <= 1) 1 | ||
else usingArray(row, col) | ||
} | ||
|
||
/** | ||
* TC: O(n * m), SC: O(n * m) | ||
*/ | ||
private fun usingGrid(row: Int, col: Int): Int { | ||
val grid = Array(row) { IntArray(col) } | ||
(0 until row).forEach { grid[it][0] = 1 } | ||
(0 until col).forEach { grid[0][it] = 1 } | ||
|
||
for (i in 1 until row) { | ||
for (j in 1 until col) { | ||
grid[i][j] = grid[i - 1][j] + grid[i][j - 1] | ||
} | ||
} | ||
|
||
return grid[row - 1][col - 1] | ||
} | ||
|
||
/** | ||
* ์ด์ ๋ผ์ธ์ ๋ฐฐ์ด๋ง ๊ธฐ์ตํ์ฌ๋ ๋๋ฏ๋ก ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์๋์ ๊ฐ์ด ์ค์ผ ์ ์๋ค. | ||
* TC: O(n * m), SC: O(m) | ||
*/ | ||
private fun usingArray(row: Int, col: Int): Int { | ||
var dp = IntArray(col) | ||
|
||
for (i in 0 until row) { | ||
val tmp = IntArray(col) | ||
for (j in 0 until col) { | ||
if (i == 0 && j == 0) tmp[j] = 1 | ||
else if (j > 0) tmp[j] = dp[j] + tmp[j - 1] | ||
else tmp[j] = dp[j] | ||
} | ||
dp = tmp | ||
} | ||
|
||
return dp.last() | ||
} | ||
|
||
@Test | ||
fun `์ผ์ชฝ ์๋จ ๋ชจ์๋ฆฌ์์ ์ค๋ฅธ์ชฝ ์๋จ ๋ชจ์๋ฆฌ๋ก ๋๋ฌํ ์ ์๋ ๊ณ ์ ๊ฒฝ๋ก์ ์๋ฅผ ๋ฐํํ๋ค`() { | ||
uniquePaths(3, 7) shouldBe 28 | ||
uniquePaths(3, 2) shouldBe 3 | ||
} | ||
} |