From f3cdc7b38fd93b6a2bbfe3d495da6579b89e78e0 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 22 Dec 2024 17:24:25 +0900 Subject: [PATCH 1/6] [Week3](gmlwls96) two-sum --- two-sum/gmlwls96.kt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 two-sum/gmlwls96.kt diff --git a/two-sum/gmlwls96.kt b/two-sum/gmlwls96.kt new file mode 100644 index 000000000..9f304bb14 --- /dev/null +++ b/two-sum/gmlwls96.kt @@ -0,0 +1,5 @@ +class Solution { + fun twoSum(nums: IntArray, target: Int): IntArray { + + } +} From 471cb9d4c43715564bc5df358d1a0c50453c8a9f Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 22 Dec 2024 18:33:40 +0900 Subject: [PATCH 2/6] [Week3](gmlwls96) two-sum code. --- two-sum/gmlwls96.kt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/two-sum/gmlwls96.kt b/two-sum/gmlwls96.kt index 9f304bb14..5d7091c50 100644 --- a/two-sum/gmlwls96.kt +++ b/two-sum/gmlwls96.kt @@ -1,5 +1,29 @@ class Solution { + // 시간 : O(logN), 공간(2N) fun twoSum(nums: IntArray, target: Int): IntArray { + val sortNums = List(nums.size) { listOf(nums[it], it) }.sortedBy { it[0] } + // 1. list( list('값', 'index')) 형태의 list를 만들고 값을 기준으로 정렬한다. + var i = 0 + var j = sortNums.lastIndex + // 2. 2포인터 방식으로 두 값을 합했을때 target이 되는 값을 찾는다. + while (i < j) { + val sum = sortNums[i][0] + sortNums[j][0] + when { + sum == target -> { // target과 sum이 일치할시 바로 return. + return intArrayOf( + min(sortNums[i][1], sortNums[j][1]), + max(sortNums[i][1], sortNums[j][1]) + ) + } + sum < target -> { // sum이 target보다 값이 작은경우 i를 한칸씩 더한다. + i++ + } + sum > target -> { // sum이 target보다 값이 큰경우 j를 한칸씩 내린다. + j-- + } + } + } + return intArrayOf() } } From 26b6991c80785ae518363ff6bf7b40373ff10dd7 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Sun, 22 Dec 2024 21:23:30 +0900 Subject: [PATCH 3/6] [Week3](gmlwls96) reverse-bits --- reverse-bits/gmlwls96.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 reverse-bits/gmlwls96.kt diff --git a/reverse-bits/gmlwls96.kt b/reverse-bits/gmlwls96.kt new file mode 100644 index 000000000..7f12a7552 --- /dev/null +++ b/reverse-bits/gmlwls96.kt @@ -0,0 +1,14 @@ +class Solution { + // you need treat n as an unsigned value + fun reverseBits(n: Int): Int { + var bitString = Integer.toBinaryString(n) + bitString = CharArray(32 - bitString.length) { '0' }.concatToString() + bitString + var result = 0 + var scale = 1 + bitString.forEach { + result += it.digitToInt() * scale + scale *= 2 + } + return result + } +} From 6e058a54ed79beab264fdf805256d8d775df95f5 Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Mon, 23 Dec 2024 22:21:00 +0900 Subject: [PATCH 4/6] [Week3](gmlwls96) Product of array except self --- product-of-array-except-self/gmlwls96.kt | 20 ++++++++++++++++++++ two-sum/gmlwls96.kt | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 product-of-array-except-self/gmlwls96.kt diff --git a/product-of-array-except-self/gmlwls96.kt b/product-of-array-except-self/gmlwls96.kt new file mode 100644 index 000000000..21c2f227c --- /dev/null +++ b/product-of-array-except-self/gmlwls96.kt @@ -0,0 +1,20 @@ +class Solution { + // 시간 : O(2n) = O(n) ,공간 : O(1) + fun productExceptSelf(nums: IntArray): IntArray { + val answer = IntArray(nums.size) { 1 } + + var n = 1 + for (i in 0 until nums.lastIndex) { + n *= nums[i] + answer[i + 1] = n + } + println(answer.toList()) + + n = 1 + for (i in nums.lastIndex downTo 1) { + n *= nums[i] + answer[i - 1] *= n + } + return answer + } +} diff --git a/two-sum/gmlwls96.kt b/two-sum/gmlwls96.kt index 5d7091c50..27b6dd147 100644 --- a/two-sum/gmlwls96.kt +++ b/two-sum/gmlwls96.kt @@ -1,5 +1,5 @@ class Solution { - // 시간 : O(logN), 공간(2N) + // 시간 : O(NlogN)-정렬하는데 드는 시간복잡도., 공간(2N) fun twoSum(nums: IntArray, target: Int): IntArray { val sortNums = List(nums.size) { listOf(nums[it], it) }.sortedBy { it[0] } // 1. list( list('값', 'index')) 형태의 list를 만들고 값을 기준으로 정렬한다. From e990fe7c01e9472e0b3897fde829359c74f0f10b Mon Sep 17 00:00:00 2001 From: gmlwls96 Date: Tue, 24 Dec 2024 16:27:40 +0900 Subject: [PATCH 5/6] [Week3](gmlwls96) Combination Sum --- combination-sum/gmlwls96.kt | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 combination-sum/gmlwls96.kt diff --git a/combination-sum/gmlwls96.kt b/combination-sum/gmlwls96.kt new file mode 100644 index 000000000..c923dc8fe --- /dev/null +++ b/combination-sum/gmlwls96.kt @@ -0,0 +1,50 @@ +class Solution { + // 시간 : O(c^t), 공간 : O(t) + // 알고리즘 : dfs + val answerList = mutableSetOf>() + + fun combinationSum(candidates: IntArray, target: Int): List> { + candidates.sort() + combination( + candidates = candidates, + target = target, + current = 0, + currentList = listOf() + ) + return answerList.toList() + } + + private fun combination( + candidates: IntArray, + target: Int, + current: Int, + currentList: List + ) { + candidates.forEach { // candidates를 한개씩 꺼내 + val sum = current + it // 현재값을 더했을때 + when { + sum == target -> { // sum이 target과 동일한 값이면 answer 에 추가. + answerList.add( + currentList.toMutableList().apply { + add(it) + sort() + } + ) + } + + sum < target -> { // sum이 모자르면 다른 조합을 찾기 위해 재귀 호출. + combination( + candidates = candidates, + target = target, + current = sum, + currentList = currentList.toMutableList().apply { + add(it) + } + ) + } + + else -> return + } + } + } +} From d4b4783a9b17538e8c7375a97f28b8ee9f18157e Mon Sep 17 00:00:00 2001 From: Heejin Ham Date: Fri, 27 Dec 2024 21:45:55 +0900 Subject: [PATCH 6/6] [Week3](gmlwls96) Maximum-subarray --- maximum-subarray/gmlwls96.kt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 maximum-subarray/gmlwls96.kt diff --git a/maximum-subarray/gmlwls96.kt b/maximum-subarray/gmlwls96.kt new file mode 100644 index 000000000..552d391f1 --- /dev/null +++ b/maximum-subarray/gmlwls96.kt @@ -0,0 +1,22 @@ +class Solution { + fun maxSubArray(nums: IntArray): Int { + val dp = Array(nums.size) { y -> + IntArray(nums.size) { x -> + if (y == x) { + nums[y] + } else { + 0 + } + } + } + + var max = dp[0][0] + for (y in nums.indices) { + for (x in y + 1..nums.lastIndex) { + dp[y][x] = dp[y][x - 1] + nums[x] + max = max(max, dp[y][x]) + } + } + return max + } +}