-
Notifications
You must be signed in to change notification settings - Fork 0
Longest Repeating Character Replacement #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
424.Longest-Repeating-Character-Replacement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # 424. Longest Repeating Character Replacement | ||
|
|
||
| ## step1 | ||
| sliding windowを使う。 | ||
| 23mほどかかった。 | ||
|
|
||
| 時間計算量はO(N)。ただし文字列に含まれるユニークな文字を定数とみなした場合。 | ||
|
|
||
| 他の解法は思いつかない。Hashmapがヒントにあるがこれをどう使うのかわからない。 | ||
|
|
||
| ## 他の人のコード | ||
|
|
||
| https://github.com/olsen-blue/Arai60/pull/49#issuecomment-3649179920 | ||
|
|
||
| https://github.com/Exzrgs/LeetCode/pull/48 | ||
|
|
||
| hashmapの使い方はこうするのか | ||
|
|
||
| https://github.com/potrue/leetcode/pull/65/changes | ||
|
|
||
| maxを取り出す部分でheapを使った解法。 | ||
|
|
||
| heapの遅延評価: heapからの削除が難しいからとりあえずcounterだけ更新しておいて、heapから取り出したときの値は一致していなかったら捨てる | ||
|
|
||
| 実際にはheapの定数倍の計算量で遅くなるようだ。 | ||
|
|
||
| https://leetcode.com/problems/longest-repeating-character-replacement/solutions/8301734/optimal-solution-by-nthapa000-lejz/ | ||
|
|
||
| max_frequencyは更新されるときだけ計算しても良く、時間計算量が小さくなる。が、毎回計算した方がわかりやすいようにも思う。 | ||
|
|
||
| ## step2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| length_of_longest = 0 | ||
|
|
||
| def update_longest(target_alphabet): | ||
| nonlocal length_of_longest | ||
| left = 0 | ||
| num_other_than_target = 0 | ||
| for right in range(len(s)): | ||
| if s[right] != target_alphabet: | ||
| num_other_than_target += 1 | ||
| while num_other_than_target > k: | ||
| if s[left] != target_alphabet: | ||
| num_other_than_target -= 1 | ||
| left += 1 | ||
| length_of_longest = max(length_of_longest, right - left + 1) | ||
|
|
||
| for target_alphabet in set(s): | ||
| update_longest(target_alphabet) | ||
|
|
||
| return length_of_longest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import collections | ||
|
|
||
|
|
||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| count = collections.defaultdict(int) | ||
| left = 0 | ||
| length_of_longest = 0 | ||
|
|
||
| for right in range(len(s)): | ||
| count[s[right]] += 1 | ||
|
|
||
| while (right - left + 1) - max(count.values()) > k: | ||
| count[s[left]] -= 1 | ||
| left += 1 | ||
|
|
||
| length_of_longest = max(length_of_longest, right - left + 1) | ||
|
|
||
| return length_of_longest |
28 changes: 28 additions & 0 deletions
28
0424.Longest-Repeating-Character-Replacement/step2_heap.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import collections | ||
| import heapq | ||
|
|
||
|
|
||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| count = collections.defaultdict(int) | ||
| left = 0 | ||
| length_of_longest = 0 | ||
| heap = [] | ||
|
|
||
| def get_max_count(): | ||
| while -heap[0][0] != count[heap[0][1]]: | ||
| heapq.heappop(heap) | ||
| return -heap[0][0] | ||
|
|
||
| for right in range(len(s)): | ||
| count[s[right]] += 1 | ||
| heapq.heappush(heap, (-count[s[right]], s[right])) | ||
|
|
||
| while (right - left + 1) - get_max_count() > k: | ||
| count[s[left]] -= 1 | ||
| heapq.heappush(heap, (-count[s[left]], s[left])) | ||
| left += 1 | ||
|
|
||
| length_of_longest = max(length_of_longest, right - left + 1) | ||
|
|
||
| return length_of_longest | ||
28 changes: 28 additions & 0 deletions
28
0424.Longest-Repeating-Character-Replacement/step2_heap_revised.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import collections | ||
| import heapq | ||
|
|
||
|
|
||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| count = collections.defaultdict(int) | ||
| left = 0 | ||
| length_of_longest = 0 | ||
| heap = [] # contains (count, alphabet) in max heap | ||
|
|
||
| def get_max_count(): | ||
| while heap[0][0] != count[heap[0][1]]: | ||
| heapq.heappop_max(heap) | ||
| return heap[0][0] | ||
|
|
||
| for right in range(len(s)): | ||
| count[s[right]] += 1 | ||
| heapq.heappush_max(heap, (count[s[right]], s[right])) | ||
|
|
||
| while (right - left + 1) - get_max_count() > k: | ||
| count[s[left]] -= 1 | ||
| heapq.heappush_max(heap, (count[s[left]], s[left])) | ||
| left += 1 | ||
|
|
||
| length_of_longest = max(length_of_longest, right - left + 1) | ||
|
|
||
| return length_of_longest |
21 changes: 21 additions & 0 deletions
21
0424.Longest-Repeating-Character-Replacement/step2_max_frequency.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import collections | ||
|
|
||
|
|
||
| class Solution: | ||
| def characterReplacement(self, s: str, k: int) -> int: | ||
| count = collections.defaultdict(int) | ||
| max_frequency = 0 | ||
| left = 0 | ||
| length_of_longest = 0 | ||
|
|
||
| for right in range(len(s)): | ||
| count[s[right]] += 1 | ||
| max_frequency = max(max_frequency, count[s[right]]) | ||
|
|
||
| while (right - left + 1) - max_frequency > k: | ||
| count[s[left]] -= 1 | ||
| left += 1 | ||
|
|
||
| length_of_longest = max(length_of_longest, right - left + 1) | ||
|
|
||
| return length_of_longest |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heap に何が入っているのか、処理が追いづらく感じました。変数名に、要素がどのようなものかを表す英単語・英語句を付けるとよいと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントを加えました。heap だけではなく stack などにも適用できる点だと思うので頭にいれておきます。
ついでに heapq モジュールの機能を使って max heap を実現しました。