Created
March 3, 2022 00:29
-
-
Save ericness/e6ab06fd943f52cd07a81dd38bea1998 to your computer and use it in GitHub Desktop.
Leetcode 424 step 2
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
def calculate_length(self, diffs: List[int], k: int) -> int: | |
"""Calculate maximum sequence length based | |
on the differences between character positions | |
Args: | |
diffs (List[int]): List of position differences | |
k (int): Number of characters to replace | |
Returns: | |
int: Maximum length of replacement | |
""" | |
behind, ahead = 0, 0 | |
current_k = diffs[0] | |
max_length = 0 | |
while ahead < len(diffs): | |
if current_k <= k: | |
ahead += 1 | |
if ahead == len(diffs): | |
break | |
current_k += diffs[ahead] | |
else: | |
behind += 1 | |
current_k -= diffs[behind] | |
length = ahead - behind + current_k | |
if length > max_length and current_k <= k: | |
max_length = length | |
return max_length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment