Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions find-minimum-in-rotated-sorted-array/jdalma.kt
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var (low, high) = 0 to nums.size - 1
kotlin에서 pair를 만드는 표현이 참 특이하네요 ㅎㅎ


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
}
}
Comment on lines +9 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이진탐색 로직이 깔끔하고 좋습니다 ㅎㅎ
저도 현준님 풀이에 영감을 얻어서 제 풀이를 좀 더 다듬을 수 있었습니다 :)
https://github.com/DaleStudy/leetcode-study/pull/520/files#diff-d02da092874d1658e6fb39bb2d7221b29a6fe6804b26e2c460bdd0e2243dd373

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전에 obzva님이 전달해주신 이진 검색 정리 게시글을 본게 도움 많이 되었습니다! 감사합니다 ㅎㅎ


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
}
}
45 changes: 45 additions & 0 deletions linked-list-cycle/jdalma.kt
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
}
}