Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ericness/e6ab06fd943f52cd07a81dd38bea1998 to your computer and use it in GitHub Desktop.
Save ericness/e6ab06fd943f52cd07a81dd38bea1998 to your computer and use it in GitHub Desktop.
Leetcode 424 step 2
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