Skip to content
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
27 changes: 27 additions & 0 deletions longest-repeating-character-replacement/tigermint.kt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

longest-repeating-character-replacement/tigermint.kt
/**
TC: O(26n) = O(n)
SC: O(26) = O(1)
 */
class Solution {
    fun characterReplacement(s: String, k: Int): Int {
        var left = 0
        val charToCount = mutableMapOf<Char, Int>()
        var maxFrequency = 0

        for (right in s.indices) {
            val added = s[right]
            charToCount[added] = (charToCount[added] ?: 0) + 1

            // 교체 횟수가 k를 넘으면 왼쪽을 당겨 윈도우 축소
            while ((right - left + 1) - charToCount.values.max() > k) {
                val removed = s[left]
                charToCount[removed] = charToCount.getValue(removed) - 1
                left++
            }

            maxFrequency = maxOf(maxFrequency, right - left + 1)
        }

        return maxFrequency
    }
}
  • 패턴: Sliding Window, Hash Map / Hash Set
  • 설명: 문자열 윈도우를 좌우로 이동시키며 길이를 최대화하는 방식으로, 창 크기 내 가장 빈도가 높은 문자를 추적하기 위해 해시 맵을 사용합니다. 조건을 만족하는지 확인하며 윈도우를 조정하는 슬라이딩 윈도우 패턴에 속합니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.characterReplacement — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 윈도우 내부 문자 빈도 수를 트래킹하고, 현재 윈도우의 최다빈도 문자를 기준으로 필요한 교체 횟수를 비교한다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.reverseBits — Time: O(1) / Space: O(1)
복잡도
Time O(1)
Space O(1)

피드백: 상수 시간에 고정된 비트 수를 순회하며 뒤집는다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
TC: O(26n) = O(n)
SC: O(26) = O(1)
*/
class Solution {
fun characterReplacement(s: String, k: Int): Int {
var left = 0
val charToCount = mutableMapOf<Char, Int>()
var maxFrequency = 0

for (right in s.indices) {
val added = s[right]
charToCount[added] = (charToCount[added] ?: 0) + 1

// 교체 횟수가 k를 넘으면 왼쪽을 당겨 윈도우 축소
while ((right - left + 1) - charToCount.values.max() > k) {
val removed = s[left]
charToCount[removed] = charToCount.getValue(removed) - 1
left++
}

maxFrequency = maxOf(maxFrequency, right - left + 1)
}

return maxFrequency
}
}
15 changes: 15 additions & 0 deletions reverse-bits/tigermint.kt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

reverse-bits/tigermint.kt
/**
TC: O(32) = O(1)
SC: O(1)
 */
class Solution {
    fun reverseBits(n: Int): Int {
        var result = 0
        var num = n
        repeat(32) {
            result = (result shl 1) or (num and 1)
            num = num ushr 1
        }
        return result
    }
}
  • 패턴: Bit Manipulation
  • 설명: 정수의 비트를 반전시키며 앞뒤를 뒤집는 과정으로, 비트 연산과 시프트를 이용한 저수준 비트 조작 패턴이다. 반복문으로 32비트를 순회하며 값을 재배치한다.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

코틀린의 비트 연산자는 엄청 직관적이네요!!

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
TC: O(32) = O(1)
SC: O(1)
*/
class Solution {
fun reverseBits(n: Int): Int {
var result = 0
var num = n
repeat(32) {
result = (result shl 1) or (num and 1)
num = num ushr 1
}
return result
}
}
Loading