File tree Expand file tree Collapse file tree
longest-repeating-character-replacement Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import kotlin.math.max
2+
3+ fun characterReplacement (s : String , k : Int ): Int {
4+ // 시간복잡도: O(N) 공간복잡도: O(1)
5+
6+ val frequencyMap = mutableMapOf<Char , Int >()
7+ var left = 0
8+ var maxFreq = 0
9+ var result = 0
10+ // 처음부터 끝까지 right 순회
11+ for (right in s.indices) {
12+ // right 위치 문자에 빈도를 증가시키고 map 저장
13+ val rightChar = s[right]
14+ frequencyMap[rightChar] = frequencyMap.getOrDefault(rightChar, 0 ) + 1
15+ maxFreq = max(maxFreq, frequencyMap[rightChar]!! )
16+
17+ val windowSize = right - left + 1
18+ val downSizeNeeded = windowSize - maxFreq
19+
20+ if (downSizeNeeded > k) {
21+ val leftChar = s[left]
22+ frequencyMap[leftChar] = frequencyMap[leftChar]!! - 1
23+ left++
24+ }
25+ result = max(result, right - left + 1 )
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ fun reverseBits (n : Int ): Int {
2+ var x = n
3+ var result = 0
4+ // 시간복잡도: O(N) 공간복잡도: O(1)
5+ repeat(32 ) {
6+ // result를 왼쪽으로 1비트 밀어서 빈자리를 만듬
7+ result = (result shl 1 )
8+ // x의 맨 오른쪽 1자리를 result의 맨 오른쪽에 할당
9+ result = result or (x and 1 )
10+ // x 를 오른쪽으로 1비트 민다
11+ x = (x ushr 1 )
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments