-
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 #521 from jdalma/main
[์ ํ์ค] 9์ฃผ์ฐจ
- Loading branch information
Showing
4 changed files
with
227 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,34 @@ | ||
class `find-minimum-in-rotated-sorted-array` { | ||
|
||
/** | ||
* TC: O(log N), SC: O(1) | ||
*/ | ||
fun findMin(nums: IntArray): Int { | ||
var (low, high) = 0 to nums.size - 1 | ||
|
||
while (low + 1 < high) { | ||
val mid = (low + high) / 2 | ||
if (nums[mid - 1] > nums[mid]) { | ||
return nums[mid] | ||
} | ||
if (nums[mid] < nums[high]) { | ||
high = mid | ||
} | ||
else { | ||
low = mid | ||
} | ||
} | ||
|
||
return min(nums[low], nums[high]) | ||
} | ||
|
||
@Test | ||
fun `์ ๋ ฅ๋ฐ์ ์ ์ ๋ฐฐ์ด์ ์ต์ ์์๋ฅผ ๋ฐํํ๋ค`() { | ||
findMin(intArrayOf(4,5,6,7,0,1,2)) shouldBe 0 | ||
findMin(intArrayOf(2,3,0,1)) shouldBe 0 | ||
findMin(intArrayOf(2,3,1)) shouldBe 1 | ||
findMin(intArrayOf(2,1,3)) shouldBe 1 | ||
findMin(intArrayOf(2,3,4,5,1)) shouldBe 1 | ||
findMin(intArrayOf(11,13,15,17)) shouldBe 11 | ||
} | ||
} |
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,45 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `linked-list-cycle` { | ||
|
||
data class ListNode(var `val`: Int) { | ||
var next: ListNode? = null | ||
} | ||
|
||
/** | ||
* TC: O(n), SC: O(1) | ||
*/ | ||
fun hasCycle(head: ListNode?): Boolean { | ||
if (head == null) return false | ||
|
||
var slow = head | ||
var fast = head | ||
|
||
while (fast?.next != null) { | ||
slow = slow?.next | ||
fast = fast.next?.next | ||
|
||
if (slow == fast) return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
@Test | ||
fun `์ ๋ ฅ๋ฐ์ ๋ ธ๋์ ์ฌ์ดํด์ด ์กด์ฌํ๋ค๋ฉด ์ฐธ์ ๋ฐํํ๋ค`() { | ||
val three = ListNode(3) | ||
val two = ListNode(2) | ||
val zero = ListNode(0) | ||
val four = ListNode(4) | ||
|
||
three.next = two | ||
two.next = zero | ||
zero.next = four | ||
four.next = two | ||
|
||
hasCycle(three) shouldBe true | ||
} | ||
} |
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,73 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import kotlin.math.max | ||
|
||
class `merge-intervals` { | ||
|
||
data class CustomRange( | ||
var start: Int, | ||
var end: Int | ||
) { | ||
fun isMergePossible(s: Int) = s <= this.end | ||
} | ||
|
||
/** | ||
* TC: O(n log n), SC: O(n) | ||
*/ | ||
fun merge(intervals: Array<IntArray>): Array<IntArray> { | ||
val sorted = intervals.sortedWith { i1, i2 -> i1[0].compareTo(i2[0]) } | ||
val result = mutableListOf<CustomRange>() | ||
|
||
var tmp = sorted.first().let { | ||
CustomRange(it[0], it[1]) | ||
} | ||
result.add(tmp) | ||
for (interval in sorted) { | ||
if (tmp.isMergePossible(interval[0])) { | ||
tmp.end = max(interval[1], tmp.end) | ||
} else { | ||
tmp = CustomRange(interval[0], interval[1]) | ||
result.add(tmp) | ||
} | ||
} | ||
|
||
return result.map { intArrayOf(it.start, it.end) } | ||
.toTypedArray() | ||
} | ||
|
||
@Test | ||
fun `2์ฐจ์ ๋ฐฐ์ด์ ์์์ธ start์ end๋งํผ ๋ณํฉํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค`() { | ||
merge( | ||
arrayOf( | ||
intArrayOf(2,6), | ||
intArrayOf(8,10), | ||
intArrayOf(15,18), | ||
intArrayOf(1,3) | ||
) | ||
) shouldBe arrayOf( | ||
intArrayOf(1,6), | ||
intArrayOf(8,10), | ||
intArrayOf(15,18) | ||
) | ||
|
||
merge( | ||
arrayOf( | ||
intArrayOf(1,4), | ||
intArrayOf(0,4) | ||
) | ||
) shouldBe arrayOf( | ||
intArrayOf(0,4) | ||
) | ||
|
||
merge( | ||
arrayOf( | ||
intArrayOf(1,4), | ||
intArrayOf(0,1) | ||
) | ||
) shouldBe arrayOf( | ||
intArrayOf(0,4) | ||
) | ||
} | ||
} |
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,75 @@ | ||
package leetcode_study | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class `pacific-atlantic-water-flow` { | ||
|
||
private val dirs = listOf( | ||
intArrayOf(0, 1), | ||
intArrayOf(0, -1), | ||
intArrayOf(1, 0), | ||
intArrayOf(-1, 0) | ||
) | ||
|
||
/** | ||
* TC: O(n * m), SC: O(n * m) | ||
*/ | ||
fun pacificAtlantic(heights: Array<IntArray>): List<List<Int>> { | ||
val (row, col) = heights.size to heights.first().size | ||
val pacific = Array(row) { BooleanArray(col) } | ||
val atlantic = Array(row) { BooleanArray(col) } | ||
|
||
for (index in 0 until row) { | ||
dfs(heights, pacific, Int.MIN_VALUE, index, 0) | ||
dfs(heights, atlantic, Int.MIN_VALUE, index, col - 1) | ||
} | ||
|
||
for (index in 0 until col) { | ||
dfs(heights, pacific, Int.MIN_VALUE, 0, index) | ||
dfs(heights, atlantic, Int.MIN_VALUE, row - 1, index) | ||
} | ||
|
||
val result = mutableListOf<List<Int>>() | ||
for (i in 0 until row) { | ||
for (j in 0 until col) { | ||
if (pacific[i][j] && atlantic[i][j]) { | ||
result.add(listOf(i, j)) | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private fun dfs(heights: Array<IntArray>, visited: Array<BooleanArray>, height: Int, r: Int, c: Int) { | ||
val (row, col) = heights.size to heights.first().size | ||
if (r < 0 || r >= row || c < 0 || c >= col || visited[r][c] || heights[r][c] < height) | ||
return | ||
|
||
visited[r][c] = true | ||
for (dir in dirs) { | ||
dfs(heights, visited, heights[r][c], r + dir[0], c + dir[1]) | ||
} | ||
} | ||
|
||
@Test | ||
fun `ํํ์๊ณผ ๋์์์ ๋ชจ๋ ํ๋ฅผ ์ ์๋ ์ ์ ์์น๋ฅผ ๋ฐํํ๋ผ`() { | ||
pacificAtlantic( | ||
arrayOf( | ||
intArrayOf(1,2,2,3,5), | ||
intArrayOf(3,2,3,4,4), | ||
intArrayOf(2,4,5,3,1), | ||
intArrayOf(6,7,1,4,5), | ||
intArrayOf(5,1,1,2,4) | ||
) | ||
) shouldBe arrayOf( | ||
intArrayOf(0,4), | ||
intArrayOf(1,3), | ||
intArrayOf(1,4), | ||
intArrayOf(2,2), | ||
intArrayOf(3,0), | ||
intArrayOf(3,1), | ||
intArrayOf(4,0) | ||
) | ||
} | ||
} |