Skip to content

Commit 97359be

Browse files
update post
1 parent 672d6c9 commit 97359be

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

_posts/2024-04-29-leetcode-424.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,50 @@ class Solution {
6868
### TC, SC
6969

7070
시간 복잡도는 O(n)이고, 공간 복잡도는 O(1)이다.
71+
72+
## 다시 풀어보기 (240603)
73+
74+
지난번에 참고했던 풀이를 보지 않고 다시 풀어보았다. 다시 풀어봐도 어려운 문제였다.
75+
76+
```java
77+
class Solution {
78+
public int characterReplacement(String s, int k) {
79+
int[] counter = new int[26];
80+
int countOfMostFrequent = -1;
81+
82+
int headPointer = 0;
83+
int tailPointer = 0;
84+
85+
int maxLength = 0;
86+
87+
while (headPointer < s.length()) {
88+
char head = s.charAt(headPointer);
89+
char tail = s.charAt(tailPointer);
90+
counter[head - 'A']++;
91+
headPointer++;
92+
93+
countOfMostFrequent = getCountOfMostFrequent(counter[head - 'A'], countOfMostFrequent);
94+
while (headPointer - tailPointer > countOfMostFrequent + k) {
95+
counter[tail - 'A']--;
96+
tailPointer++;
97+
tail = s.charAt(tailPointer);
98+
}
99+
100+
maxLength = Math.max(maxLength, headPointer - tailPointer);
101+
System.out.printf("%d, %d, %s\n", tailPointer, headPointer, s.substring(tailPointer, headPointer));
102+
}
103+
104+
return maxLength;
105+
}
106+
107+
int getCountOfMostFrequent(int challenge, int countOfMostFrequent) {
108+
return Math.max(challenge, countOfMostFrequent);
109+
}
110+
}
111+
```
112+
113+
### TC, SC
114+
115+
시간 복잡도는 O(n)이고, 공간 복잡도는 O(1)이다.
116+
117+
![final result](/assets/images/2024-04-29-leetcode-424/final-result.png)
110 KB
Loading

0 commit comments

Comments
 (0)