Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gmlwls96] Week3 #764

Merged
merged 6 commits into from
Dec 28, 2024
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
50 changes: 50 additions & 0 deletions combination-sum/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution {
// 시간 : O(c^t), 공간 : O(t)
// 알고리즘 : dfs
val answerList = mutableSetOf<List<Int>>()

fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
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<Int>
) {
candidates.forEach { // candidates를 한개씩 꺼내
val sum = current + it // 현재값을 더했을때
when {
sum == target -> { // sum이 target과 동일한 값이면 answer 에 추가.
answerList.add(
currentList.toMutableList().apply {
add(it)
sort()
}
eunhwa99 marked this conversation as resolved.
Show resolved Hide resolved
)
}

sum < target -> { // sum이 모자르면 다른 조합을 찾기 위해 재귀 호출.
combination(
candidates = candidates,
target = target,
current = sum,
currentList = currentList.toMutableList().apply {
add(it)
}
)
}

else -> return
}
}
}
}
22 changes: 22 additions & 0 deletions maximum-subarray/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
20 changes: 20 additions & 0 deletions product-of-array-except-self/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 14 additions & 0 deletions reverse-bits/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -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
eunhwa99 marked this conversation as resolved.
Show resolved Hide resolved
var result = 0
var scale = 1
bitString.forEach {
result += it.digitToInt() * scale
scale *= 2
}
return result
}
}
29 changes: 29 additions & 0 deletions two-sum/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
// 시간 : 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를 만들고 값을 기준으로 정렬한다.

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()
}
}
Loading