Skip to content

Instantly share code, notes, and snippets.

@DixieKorley
Last active June 11, 2019 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DixieKorley/32df12e71bba94dbfdda6164a37a7d00 to your computer and use it in GitHub Desktop.
Save DixieKorley/32df12e71bba94dbfdda6164a37a7d00 to your computer and use it in GitHub Desktop.
"""Max Consecutive Ones II"""
def max_consecutive_ones_II(bits):
res = max_count = 0
counter = Counter()
for i in range(len(bits)):
counter[bits[i]] += 1
maxf = max(maxf, counter[bits[i]])
if res - maxf < 1:
res += 1
else:
counter[bits[i - res]] -= 1
return res
"""Max Consecutive Ones III"""
from collections import Counter
def max_consecutive_ones_III(bits, k):
maxf = res = 0
counter = Counter()
for i in range(len(bits)):
counter[bits[i]] += 1
maxf = max(maxf, counter[bits[i]])
if res - maxf < k:
res += 1
else:
counter[bits[i - res]] -= 1
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment