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
34 changes: 34 additions & 0 deletions longest-repeating-character-replacement/sadie100.ts
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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window, Hash Map / Hash Set
  • 설명: 이 코드는 슬라이딩 윈도우 기법으로 연속된 구간을 탐색하며, 문자 빈도 수를 해시 맵으로 저장하여 최적의 구간을 찾는다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
start, end 투 포인터 변수를 생성하고 end를 하나씩 늘려 가며 s를 탐색
글자별 개수를 저장하는 map, 최빈 등장수 maxCount를 통해 가장 많이 나온 캐릭터의 카운트를 저장하고
만약 end-start+1-maxCount가 k를 넘어가면 start를 +1한다
매번 end-start+1와 현 result를 비교하여 큰 값을 result로 갱신한다

시간복잡도 : O(N)
*/

function characterReplacement(s: string, k: number): number {
let start = 0
let end = 0
let result = 0
let maxCount = 0
const charMap = new Map()

while (end < s.length) {
const thisChar = s[end]
const updatedCount = (charMap.get(thisChar) || 0) + 1
charMap.set(thisChar, updatedCount)
maxCount = Math.max(maxCount, updatedCount)

if (end - start + 1 - maxCount > k) {
const startChar = s[start]
charMap.set(startChar, charMap.get(startChar) - 1)
start += 1
}

result = Math.max(result, end - start + 1)
end += 1
}

return result
}
19 changes: 19 additions & 0 deletions reverse-bits/sadie100.ts
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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 이 코드는 비트 연산을 활용하여 숫자의 비트를 뒤집는 작업을 수행하므로 Bit Manipulation 패턴에 속합니다. 효율적인 비트 연산을 통해 빠르게 결과를 도출합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
n의 뒤의 자리에서부터 비트로 변환해서 result에 붙이고, result 비트를 앞으로 당기는 일을 32번 반복해서 뒤집어진 비트를 만든다

시간복잡도 : O(1) - 비트 연산
공간복잡도 : O(1)
*/

function reverseBits(n: number): number {
let result = 0

for (let i = 0; i < 32; i++) {
const bit = n & 1

result = (result << 1) | bit

n = n >>> 1
}
return result >>> 0
}
Loading