Skip to content

Commit a2556d1

Browse files
authored
Merge pull request #764 from gmlwls96/main
2 parents 3b92814 + d4b4783 commit a2556d1

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

combination-sum/gmlwls96.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
// 시간 : O(c^t), 공간 : O(t)
3+
// 알고리즘 : dfs
4+
val answerList = mutableSetOf<List<Int>>()
5+
6+
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
7+
candidates.sort()
8+
combination(
9+
candidates = candidates,
10+
target = target,
11+
current = 0,
12+
currentList = listOf()
13+
)
14+
return answerList.toList()
15+
}
16+
17+
private fun combination(
18+
candidates: IntArray,
19+
target: Int,
20+
current: Int,
21+
currentList: List<Int>
22+
) {
23+
candidates.forEach { // candidates를 한개씩 꺼내
24+
val sum = current + it // 현재값을 더했을때
25+
when {
26+
sum == target -> { // sum이 target과 동일한 값이면 answer 에 추가.
27+
answerList.add(
28+
currentList.toMutableList().apply {
29+
add(it)
30+
sort()
31+
}
32+
)
33+
}
34+
35+
sum < target -> { // sum이 모자르면 다른 조합을 찾기 위해 재귀 호출.
36+
combination(
37+
candidates = candidates,
38+
target = target,
39+
current = sum,
40+
currentList = currentList.toMutableList().apply {
41+
add(it)
42+
}
43+
)
44+
}
45+
46+
else -> return
47+
}
48+
}
49+
}
50+
}

maximum-subarray/gmlwls96.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
fun maxSubArray(nums: IntArray): Int {
3+
val dp = Array(nums.size) { y ->
4+
IntArray(nums.size) { x ->
5+
if (y == x) {
6+
nums[y]
7+
} else {
8+
0
9+
}
10+
}
11+
}
12+
13+
var max = dp[0][0]
14+
for (y in nums.indices) {
15+
for (x in y + 1..nums.lastIndex) {
16+
dp[y][x] = dp[y][x - 1] + nums[x]
17+
max = max(max, dp[y][x])
18+
}
19+
}
20+
return max
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
// 시간 : O(2n) = O(n) ,공간 : O(1)
3+
fun productExceptSelf(nums: IntArray): IntArray {
4+
val answer = IntArray(nums.size) { 1 }
5+
6+
var n = 1
7+
for (i in 0 until nums.lastIndex) {
8+
n *= nums[i]
9+
answer[i + 1] = n
10+
}
11+
println(answer.toList())
12+
13+
n = 1
14+
for (i in nums.lastIndex downTo 1) {
15+
n *= nums[i]
16+
answer[i - 1] *= n
17+
}
18+
return answer
19+
}
20+
}

reverse-bits/gmlwls96.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
// you need treat n as an unsigned value
3+
fun reverseBits(n: Int): Int {
4+
var bitString = Integer.toBinaryString(n)
5+
bitString = CharArray(32 - bitString.length) { '0' }.concatToString() + bitString
6+
var result = 0
7+
var scale = 1
8+
bitString.forEach {
9+
result += it.digitToInt() * scale
10+
scale *= 2
11+
}
12+
return result
13+
}
14+
}

two-sum/gmlwls96.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
// 시간 : O(NlogN)-정렬하는데 드는 시간복잡도., 공간(2N)
3+
fun twoSum(nums: IntArray, target: Int): IntArray {
4+
val sortNums = List(nums.size) { listOf(nums[it], it) }.sortedBy { it[0] }
5+
// 1. list( list('값', 'index')) 형태의 list를 만들고 값을 기준으로 정렬한다.
6+
7+
var i = 0
8+
var j = sortNums.lastIndex
9+
// 2. 2포인터 방식으로 두 값을 합했을때 target이 되는 값을 찾는다.
10+
while (i < j) {
11+
val sum = sortNums[i][0] + sortNums[j][0]
12+
when {
13+
sum == target -> { // target과 sum이 일치할시 바로 return.
14+
return intArrayOf(
15+
min(sortNums[i][1], sortNums[j][1]),
16+
max(sortNums[i][1], sortNums[j][1])
17+
)
18+
}
19+
sum < target -> { // sum이 target보다 값이 작은경우 i를 한칸씩 더한다.
20+
i++
21+
}
22+
sum > target -> { // sum이 target보다 값이 큰경우 j를 한칸씩 내린다.
23+
j--
24+
}
25+
}
26+
}
27+
return intArrayOf()
28+
}
29+
}

0 commit comments

Comments
 (0)