Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions combination-sum/acious.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
val result = mutableListOf<List<Int>>()
val current = mutableListOf<Int>()

fun dfs(start: Int, total: Int) {
if (total > target) return

if (total == target) {
result.add(ArrayList(current))
return
}

for (i in start until candidates.size) {
val num = candidates[i]
current.add(num)
dfs(i, total + num)
current.removeAt(current.size - 1)
}
}

dfs(0, 0)
return result
}
}
27 changes: 27 additions & 0 deletions find-minimum-in-rotated-sorted-array/acious.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
fun findMin(nums: IntArray): Int {
var low = 1
var high = nums.size - 1

while (low <= high) {
// 코틀린에서 Int끼리의 나눗셈은 자동으로 소수점을 버립니다 (Floor)
val mid = (low + high) / 2

// 회전된 지점(변곡점)을 찾은 경우: 바로 리턴
if (nums[mid - 1] > nums[mid]) {
return nums[mid]
}

// 왼쪽 부분이 정렬되어 있다면, 최소값은 오른쪽에 있음
if (nums[0] < nums[mid]) {
low = mid + 1
} else {
// 오른쪽 부분이 정렬되어 있다면(혹은 변곡점이 왼쪽에 있다면), 왼쪽으로 이동
high = mid - 1
}
}

// 반복문이 끝날 때까지 못 찾았거나, 배열이 회전되지 않은 경우 첫 번째 요소가 최소값
return nums[0]
}
}
37 changes: 37 additions & 0 deletions maximum-depth-of-binary-tree/acious.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun maxDepth(root: TreeNode?): Int {
if (root == null) return 0

// 굳이 Nullable을 담을 필요 없으므로 Queue<TreeNode> 사용
val queue: Queue<TreeNode> = LinkedList()
queue.add(root)

var maxDepth = 0

while (queue.isNotEmpty()) {
val levelSize = queue.size // 현재 레벨에 있는 노드의 개수
maxDepth++ // 레벨 진입 시 깊이 1 증가

// 현재 레벨에 있는 노드들을 몽땅 꺼내고, 다음 레벨(자식들)을 넣음
repeat(levelSize) {
val curNode = queue.poll() // LinkedList에서는 remove()보다 poll()이 안전

// 자식 노드가 있으면 큐에 추가 (다음 레벨 예비군)
curNode.left?.let { queue.add(it) }
curNode.right?.let { queue.add(it) }
}
}

return maxDepth
}
}
31 changes: 31 additions & 0 deletions merge-two-sorted-lists/acious.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? {
var curOneNode = list1
var curTwoNode = list2

val resultStartNode = ListNode(999)
var resultCurNode = resultStartNode

while (curOneNode != null && curTwoNode != null) {

if (curOneNode.`val` <= curTwoNode.`val`) {
resultCurNode.next = curOneNode
curOneNode = curOneNode.next
} else {
resultCurNode.next = curTwoNode
curTwoNode = curTwoNode.next
}

resultCurNode = resultCurNode.next!!
}

if (curOneNode != null) {
resultCurNode.next = curOneNode
}
if (curTwoNode != null) {
resultCurNode.next = curTwoNode
}

return resultStartNode.next
}
}