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 3 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
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(logN), 공간(2N)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 코치 Flynn입니다 😀
Big O 분석에 도움이 될만한 링크 첨부하겠습니다
참고하시면 좋을 것 같아요

https://discord.com/channels/775115965964222492/1313636223058972703/1314140425216327731

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다~ 한동안 코딩테스트 놓고있다가 다시하니 시간복잡도, 공간복잡도가 많이 헷갈리더라구요~
좋은 자료 제공해주셔서 감사합니다~!

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