File tree Expand file tree Collapse file tree 1 file changed +6
-14
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 1 file changed +6
-14
lines changed Original file line number Diff line number Diff line change 11class Solution {
22 fun characterReplacement (s : String , k : Int ): Int {
33 val cnts = MutableList <Int >(' Z' .code - ' A' .code + 1 ) {0 }
4- val queue = PriorityQueue <List <Int >>() {a, b -> b.first() - a.first()}
5- queue.offer(listOf (0 , - 1 ))
4+ var max_cnt = 0
65 var ans = 0
76 var i = 0
87 var j = 0
9- while (j != s.length) { // T(n) = S(n) = O(nlogn)
10- while (queue.size > 1 ) {
11- val u = queue.peek()
12- if (u.first() == cnts[u.last()]) {
13- break
14- }
15- queue.poll()
16- }
8+ while (j != s.length) { // T(n) = S(n) = O(n)
179 while (j != s.length) {
18- val diff = j - i - queue.peek().first()
10+ val diff = j - i - max_cnt
1911 if (diff > k) {
2012 break
2113 }
2214 val d = s[j].code - ' A' .code
23- if (diff == k && queue.peek().first() != cnts[d]) {
15+ if (diff == k && max_cnt != cnts[d]) {
2416 break
2517 }
2618 j++
27- queue.offer( listOf ( ++ cnts[d], d)) // O(logn )
19+ max_cnt = maxOf(max_cnt, ++ cnts[d])
2820 }
2921 ans = maxOf(ans, j - i)
3022 val d = s[i++ ].code - ' A' .code
31- queue.offer( listOf ( -- cnts[d], d)) // O(logn)
23+ cnts[d]--
3224 }
3325 return ans
3426 }
You can’t perform that action at this time.
0 commit comments