Skip to content

Commit

Permalink
Merge pull request #488 from jdalma/main
Browse files Browse the repository at this point in the history
[์ •ํ˜„์ค€] 7์ฃผ์ฐจ
  • Loading branch information
jdalma authored Sep 28, 2024
2 parents 480e4b8 + c799db0 commit b471b65
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 0 deletions.
46 changes: 46 additions & 0 deletions longest-substring-without-repeating-characters/jdalma.kt
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
}
}
83 changes: 83 additions & 0 deletions number-of-islands/jdalma.kt
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
}
}
36 changes: 36 additions & 0 deletions reverse-linked-list/jdalma.kt
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
)
111 changes: 111 additions & 0 deletions set-matrix-zeroes/jdalma.kt
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)
)
}
}
58 changes: 58 additions & 0 deletions unique-paths/jdalma.kt
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
}
}

0 comments on commit b471b65

Please sign in to comment.