From 5d37cb58a14f758052bd3f0412c0408e9f6a430e Mon Sep 17 00:00:00 2001 From: ace kim Date: Sun, 30 Nov 2025 03:10:50 +0800 Subject: [PATCH 1/3] maximum-subarray solution --- combination-sum/acious.kt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 combination-sum/acious.kt diff --git a/combination-sum/acious.kt b/combination-sum/acious.kt new file mode 100644 index 0000000000..9619b4a3fa --- /dev/null +++ b/combination-sum/acious.kt @@ -0,0 +1,25 @@ +class Solution { + fun combinationSum(candidates: IntArray, target: Int): List> { + val result = mutableListOf>() + val current = mutableListOf() + + 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 + } +} From 6114ecd49e639080f9f645d7786d60bfce69cca5 Mon Sep 17 00:00:00 2001 From: ace kim Date: Sat, 6 Dec 2025 04:02:53 +0800 Subject: [PATCH 2/3] sol 4-weeks --- .../acious.kt | 27 ++++++++++++++ maximum-depth-of-binary-tree/acious.kt | 37 +++++++++++++++++++ merge-two-sorted-lists/acious.kt | 31 ++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/acious.kt create mode 100644 maximum-depth-of-binary-tree/acious.kt create mode 100644 merge-two-sorted-lists/acious.kt diff --git a/find-minimum-in-rotated-sorted-array/acious.kt b/find-minimum-in-rotated-sorted-array/acious.kt new file mode 100644 index 0000000000..df55b09a36 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/acious.kt @@ -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] + } +} \ No newline at end of file diff --git a/maximum-depth-of-binary-tree/acious.kt b/maximum-depth-of-binary-tree/acious.kt new file mode 100644 index 0000000000..b31ced33f8 --- /dev/null +++ b/maximum-depth-of-binary-tree/acious.kt @@ -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 사용 + val queue: Queue = 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 + } +} \ No newline at end of file diff --git a/merge-two-sorted-lists/acious.kt b/merge-two-sorted-lists/acious.kt new file mode 100644 index 0000000000..33ddfec865 --- /dev/null +++ b/merge-two-sorted-lists/acious.kt @@ -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 + } +} \ No newline at end of file From ec2f957a0fed3d0d8f602d2eb7b00f8d36e205fa Mon Sep 17 00:00:00 2001 From: ace kim Date: Sat, 6 Dec 2025 04:07:11 +0800 Subject: [PATCH 3/3] sol 4-weeks --- find-minimum-in-rotated-sorted-array/acious.kt | 2 +- maximum-depth-of-binary-tree/acious.kt | 2 +- merge-two-sorted-lists/acious.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/find-minimum-in-rotated-sorted-array/acious.kt b/find-minimum-in-rotated-sorted-array/acious.kt index df55b09a36..b79aa63ee9 100644 --- a/find-minimum-in-rotated-sorted-array/acious.kt +++ b/find-minimum-in-rotated-sorted-array/acious.kt @@ -24,4 +24,4 @@ class Solution { // 반복문이 끝날 때까지 못 찾았거나, 배열이 회전되지 않은 경우 첫 번째 요소가 최소값 return nums[0] } -} \ No newline at end of file +} diff --git a/maximum-depth-of-binary-tree/acious.kt b/maximum-depth-of-binary-tree/acious.kt index b31ced33f8..fe8903e185 100644 --- a/maximum-depth-of-binary-tree/acious.kt +++ b/maximum-depth-of-binary-tree/acious.kt @@ -34,4 +34,4 @@ class Solution { return maxDepth } -} \ No newline at end of file +} diff --git a/merge-two-sorted-lists/acious.kt b/merge-two-sorted-lists/acious.kt index 33ddfec865..35750c5aa6 100644 --- a/merge-two-sorted-lists/acious.kt +++ b/merge-two-sorted-lists/acious.kt @@ -28,4 +28,4 @@ class Solution { return resultStartNode.next } -} \ No newline at end of file +}