description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = "aeiou" n = len(s) i = k window = s[:k] res = sum([(1) for x in window if x in vowel]) tmp = res while i < n: if s[i - k] in vowel: tmp -= 1 if s[i] in vowel: tmp += 1 res = max(res, tmp) i += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: maxNum = -1 currNum = 0 vowels = "aeiou" j = 0 while j < k: if s[j] in vowels: currNum += 1 j += 1 maxNum = max(maxNum, currNum) for i in range(k, len(s)): if s[i] in vowels: currNum += 1 if s[i - k] in vowels: currNum -= 1 maxNum = max(maxNum, currNum) return maxNum
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s, k): vcnt, res = dict(), 0 for i, c in enumerate(s): if i - k >= 0 and s[i - k] in "aeiou": vcnt[s[i - k]] -= 1 if c in "aeiou": vcnt[c] = vcnt.get(c, 0) + 1 res = max(res, sum(vcnt.values())) return res class Solution: def maxVowels(self, s, k): res, cnt = 0, 0 for i, c in enumerate(s): cnt += (c in "aeiou") - (i - k >= 0 and s[i - k] in "aeiou") if cnt > res: res = cnt return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING VAR VAR BIN_OP VAR VAR NUMBER IF VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR STRING BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR STRING IF VAR VAR ASSIGN VAR VAR RETURN VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: l = 0 r = 0 v_set = {"a", "e", "i", "o", "u"} window_len = 0 count = 0 max_len = 0 while r < len(s): if window_len < k: if s[r] in v_set: count += 1 window_len += 1 else: print((window_len, count, s[r])) if count == k: max_len = k return max_len else: max_len = max(count, max_len) if s[l] in v_set: count -= 1 l += 1 window_len -= 1 if s[r] in v_set: count += 1 window_len += 1 r += 1 max_len = max(count, max_len) return max_len
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {"a", "e", "i", "o", "u"} vowel_count = 0 for i in range(k): if s[i] in vowels: vowel_count += 1 max_vowels = vowel_count for i in range(k, len(s)): if s[i - k] in vowels: vowel_count -= 1 if s[i] in vowels: vowel_count += 1 max_vowels = vowel_count if vowel_count > max_vowels else max_vowels return max_vowels
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: max_val = 0 tmp = 0 i = 0 he = len(s) for i in range(k): if s[i] in "aeiou": tmp += 1 if tmp == k: return k if tmp > max_val: max_val = tmp for i in range(k, he): if s[i - k] in "aeiou": tmp -= 1 if s[i] in "aeiou": tmp += 1 if tmp > max_val: max_val = tmp return max_val
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {"a", "e", "i", "o", "u"} current = 0 maxx = 0 start, end = 0, 0 if k == 0: return 0 while end < min(start + k, len(s)): if s[end] in vowels: current += 1 end += 1 if end == len(s): return current while end < len(s): maxx = max(maxx, current) if s[start] in vowels: current -= 1 start += 1 if s[end] in vowels: current += 1 end += 1 return max(maxx, current)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: currMax = 0 curr = 0 vowels = set(["a", "e", "i", "o", "u"]) for i, c in enumerate(s): if c in vowels: curr += 1 if i >= k and s[i - k] in vowels: curr -= 1 currMax = max(curr, currMax) if currMax == k: break return currMax
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = set("aeiou") leftwindow = 0 ans = 0 temp = 0 for rightwindow in range(len(s)): if rightwindow < k: if s[rightwindow] in vowel: temp += 1 ans = max(temp, ans) continue leftChar = s[leftwindow] rightChar = s[rightwindow] if rightChar in vowel and leftChar in vowel: pass elif rightChar in vowel: temp += 1 elif leftChar in vowel: temp -= 1 ans = max(temp, ans) leftwindow += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = {"a", "e", "i", "o", "u"} cnt = 0 for i in range(k): if s[i] in vowel: cnt += 1 if cnt == k: return k i = 0 res = cnt for j in range(k, len(s)): if s[i] in vowel: cnt -= 1 if s[j] in vowel: cnt += 1 i += 1 if cnt == k: return k res = max(res, cnt) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: i = 0 j = i + k - 1 cc = 0 for k in range(i, j + 1): if s[k] == "a" or s[k] == "e" or s[k] == "i" or s[k] == "o" or s[k] == "u": cc += 1 max_c = 0 while j < len(s): if cc > max_c: max_c = cc if s[i] == "a" or s[i] == "e" or s[i] == "i" or s[i] == "o" or s[i] == "u": cc -= 1 i += 1 j += 1 if j < len(s): if ( s[j] == "a" or s[j] == "e" or s[j] == "i" or s[j] == "o" or s[j] == "u" ): cc += 1 return max_c
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vo = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]) l, max_l, count = 0, 0, 0 for i in range(len(s)): ch = s[i] if ch in vo: count += 1 if i - l + 1 > k: if s[l] in vo: count -= 1 l += 1 max_l = max(max_l, count) return max_l
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = set({"a", "e", "i", "o", "u"}) curr = 0 maxx = 0 for i in range(k): if s[i] in vowel: curr += 1 maxx += 1 for i in range(1, len(s) - k + 1): if s[i + k - 1] in vowel: curr += 1 if s[i - 1] in vowel: curr -= 1 if curr == k: return curr if curr >= maxx: maxx = curr return maxx
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: maks = 0 string = "" count = 0 lenght = 0 for l in s: if l == "a" or l == "e" or l == "i" or l == "o" or l == "u": count += 1 string += l lenght += 1 if lenght == k: if count > maks: maks = count if ( string[0] == "a" or string[0] == "e" or string[0] == "i" or string[0] == "o" or string[0] == "u" ): count -= 1 lenght -= 1 string = string[1:k] return maks
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: su, res, vov = 0, 0, ("a", "e", "i", "o", "u") for i, v in enumerate(s): if v in vov: su += 1 if i - k >= 0: if s[i - k] in vov: su -= 1 res = max(res, su) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER STRING STRING STRING STRING STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: total = res = 0 for i in range(len(s)): if s[i] in "aeiou": total += 1 if i >= k - 1: res = max(res, total) if s[i - k + 1] in "aeiou": total -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {"a", "e", "i", "o", "u"} count = 0 maxCount = 0 i = 0 for j in range(len(s)): if s[j] in vowels: count += 1 if j >= k and s[j - k] in vowels: count -= 1 maxCount = max(maxCount, count) return maxCount
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowel = 0 max_vowel = 0 front = 0 for i in range(len(s)): if s[i] == "a" or s[i] == "e" or s[i] == "i" or s[i] == "o" or s[i] == "u": vowel += 1 print(s[i]) if i >= k - 1: max_vowel = max(max_vowel, vowel) front = s[i - k + 1] if ( front == "a" or front == "e" or front == "i" or front == "o" or front == "u" ): vowel -= 1 return max_vowel
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING VAR STRING VAR STRING VAR STRING VAR STRING VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = {"a", "e", "i", "o", "u"} mx, vowelCount = 0, 0 l, r = 0, 0 while r < len(s): if s[r] in vowels: vowelCount += 1 r += 1 while r - l == k and l < len(s): mx = max(vowelCount, mx) if s[l] in vowels: vowelCount -= 1 l += 1 return mx
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = set("aeiou") init = len([x for x in s[:k] if x in vowels]) ans = init for i in range(1, len(s) - k + 1): if s[i - 1] in vowels: init -= 1 if s[i + k - 1] in vowels: init += 1 ans = max(ans, init) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: cnt = i = ans = 0 for j in range(len(s)): c = s[j] cnt += s[j] in "aeiou" if j >= k: cnt -= s[i] in "aeiou" i += 1 if j >= k - 1: ans = max(ans, cnt) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR STRING IF VAR VAR VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) vowel = set(["a", "e", "i", "o", "u"]) i = 0 res = 0 while i < k: if s[i] in vowel: res += 1 i += 1 j = k i = 0 maxV = res while j < n: if s[i] in vowel: res -= 1 if s[j] in vowel: res += 1 i += 1 j += 1 if maxV < res: maxV = res return maxV
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: self.vowels = ["a", "e", "i", "o", "u"] def isVowel(char): return char in self.vowels i = 0 j = 0 cur_count = 0 result = 0 length = len(s) while i < length and j < length: if isVowel(s[j]): cur_count += 1 if j - i + 1 >= k: result = max(result, cur_count) if isVowel(s[i]): cur_count -= 1 i += 1 j += 1 return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING FUNC_DEF RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: a = s[:k] d = {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0} def checkVowels(): total = 0 for key in d: total += d[key] return total for c in s[:k]: if c in d: d[c] += 1 total = checkVowels() i = 1 while i <= len(s) - k: prev = s[i - 1] if prev in d: d[prev] -= 1 nxt = s[i + k - 1] if nxt in d: d[nxt] += 1 total = max(total, checkVowels()) i += 1 return total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: ans = 0 cur = 0 for j, c in enumerate(s): cur += c in "aeiou" if j >= k: cur -= s[j - k] in "aeiou" ans = max(ans, cur) return max(ans, cur)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR VAR VAR VAR BIN_OP VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: ans = 0 dic = {"a": 1, "e": 1, "i": 1, "o": 1, "u": 1} for i in range(k): if s[i] in dic: ans += 1 i = 0 j = k temp = ans while j < len(s): if s[j] in dic: temp += 1 if s[i] in dic: temp -= 1 ans = max(ans, temp) i += 1 j += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: def vowel_letters(ch): if ch in ["a", "e", "i", "o", "u"]: return True return False temp = [(1 if vowel_letters(i) else 0) for i in s] su = sum(temp[:k]) m = su for i in range(k, len(temp)): su += temp[i] - temp[i - k] m = max([su, m]) return m
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR LIST STRING STRING STRING STRING STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: _start = 0 _end = 0 _max = 0 start = 0 length = 0 count = 0 for end in range(len(s)): length += 1 if self.check(s[end]): count += 1 if length > k: if self.check(s[start]): count -= 1 start += 1 if count > _max: _max = count _start = start _end = end return _max def check(self, ch): vowels = ["a", "e", "i", "o", "u"] for vowel in vowels: if ch == vowel: return True return False
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = "aeiou" num_vowels = sum(1 for i in range(k) if s[i] in vowels) max_vowels = num_vowels for removed_idx in range(0, len(s) - k): added_idx = removed_idx + k num_vowels += int(s[added_idx] in vowels) - int(s[removed_idx] in vowels) if num_vowels > max_vowels: max_vowels = num_vowels return max_vowels
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: m, beg, end = len(s), 0, 0 vowels = ["a", "e", "i", "o", "u"] maxi, count = 0, 0 while end < m: if s[end] in vowels: count += 1 if end - beg + 1 >= k: maxi = max(maxi, count) if s[beg] in vowels: count -= 1 beg += 1 end += 1 return maxi
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given a string s and an integer k. Return the maximum number of vowel letters in any substring of s with length k. Vowel letters in English are (a, e, i, o, u).   Example 1: Input: s = "abciiidef", k = 3 Output: 3 Explanation: The substring "iii" contains 3 vowel letters. Example 2: Input: s = "aeiou", k = 2 Output: 2 Explanation: Any substring of length 2 contains 2 vowels. Example 3: Input: s = "leetcode", k = 3 Output: 2 Explanation: "lee", "eet" and "ode" contain 2 vowels. Example 4: Input: s = "rhythms", k = 4 Output: 0 Explanation: We can see that s doesn't have any vowel letters. Example 5: Input: s = "tryhard", k = 4 Output: 1   Constraints: 1 <= s.length <= 10^5 s consists of lowercase English letters. 1 <= k <= s.length
class Solution: def maxVowels(self, s: str, k: int) -> int: if not s or not k: return 0 max_vowels = 0 start_pointer = 0 end_pointer = k - 1 vowels = {"a", "e", "i", "o", "u"} num_curr_window_vowels = 0 for i in range(k): if s[i] in vowels: num_curr_window_vowels += 1 max_vowels = max(max_vowels, num_curr_window_vowels) while end_pointer < len(s) - 1: if s[start_pointer] in vowels: num_curr_window_vowels -= 1 start_pointer += 1 end_pointer += 1 if s[end_pointer] in vowels: num_curr_window_vowels += 1 max_vowels = max(max_vowels, num_curr_window_vowels) return max_vowels
CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): hashMap = {} for i in range(len(matrix)): for j in range(len(matrix[0])): if hashMap.get(i - j, "no") != "no": if hashMap[i - j] != matrix[i][j]: return False else: hashMap[i - j] = matrix[i][j] return True
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR STRING STRING IF VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): x, y = len(matrix[0]), len(matrix) for i in range(x - 1): for j in range(y - 1): if matrix[j][i] != matrix[j + 1][i + 1]: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): rows = len(matrix) col = len(matrix[0]) i = 0 j = 0 while i < rows - 1: while j < col - 1: print("Comparing: ", matrix[i][j], matrix[i + 1][j + 1]) if matrix[i][j] == matrix[i + 1][j + 1]: j += 1 else: return False i += 1 j = 0 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): for c in range(len(matrix) - 1): for r in range(len(matrix[0]) - 1): if matrix[c][r] != matrix[c + 1][r + 1]: return False return True
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): vmap = collections.defaultdict(set) M, N = len(matrix), len(matrix[0]) for x in range(M): for y in range(N): vmap[y - x].add(matrix[x][y]) if len(vmap[y - x]) > 1: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): diagonals_upper = [] x, y = len(matrix[0]), len(matrix) starters = [[0, i] for i in range(x)] + [[i, 0] for i in range(y)] for starter in starters: for j in range(min(x, y)): if starter[0] + j < y and starter[1] + j < x: print(starter[0] + j, starter[1] + j) if ( matrix[starter[0]][starter[1]] != matrix[starter[0] + j][starter[1] + j] ): return False print("\n") return True
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR LIST VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): for i in range(0, len(matrix)): for j in range(0, len(matrix[0])): r = i + 1 c = j + 1 while r < len(matrix) and c < len(matrix[0]): if matrix[i][j] == matrix[r][c]: r += 1 c += 1 continue else: return False return True
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix1(self, matrix): row, col = len(matrix), len(matrix[0]) for j in range(col): a = 0 while a + 1 < row and j + a + 1 < col: if matrix[a][j + a] == matrix[a + 1][j + a + 1]: a += 1 else: return False for i in range(row): a = 0 while a + 1 < col and i + 1 + a < row: if matrix[i + a][a] == matrix[i + a + 1][a + 1]: a += 1 else: return False return True def isToeplitzMatrix(self, m): return all( m[i][j] == m[i + 1][j + 1] for i in range(len(m) - 1) for j in range(len(m[0]) - 1) )
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): result = True for i in range(len(matrix) - 1): result = result and matrix[i][:-1] == matrix[i + 1][1:] return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): if not matrix: return False colSize = len(matrix[0]) - 1 for row in range(len(matrix) - 1): if matrix[row][:colSize] != matrix[row + 1][1 : colSize + 1]: return False return True
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): return all( ( True if len(matrix[i]) == 1 or matrix[i][:-1] == matrix[i + 1][1:] else False ) for i in range(len(matrix) - 1) )
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other. Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note: 1 <= len(start) = len(end) <= 10000. Both start and end will only consist of characters in {'L', 'R', 'X'}.
class Solution: def isToeplitzMatrix(self, matrix): M, N = len(matrix), len(matrix[0]) if M == 1: return True prev_row = matrix[0][:-1] for row in matrix[1:]: if row[1:] != prev_row: return False prev_row = row[:-1] return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER
Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1: Input: s = "the sky is blue" Output: "blue is sky the" Example 2: Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Example 4: Input: s = " Bob Loves Alice " Output: "Alice Loves Bob" Example 5: Input: s = "Alice does not even like bob" Output: "bob like even not does Alice" Constraints: 1 <= s.length <= 104 s contains English letters (upper-case and lower-case), digits, and spaces ' '. There is at least one word in s.
class Solution: def reverseWords(self, s: str) -> str: s = s.strip().split() l = [] for i in reversed(range(len(s))): l.append(s[i]) return " ".join(l)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR VAR
Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1: Input: s = "the sky is blue" Output: "blue is sky the" Example 2: Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Example 4: Input: s = " Bob Loves Alice " Output: "Alice Loves Bob" Example 5: Input: s = "Alice does not even like bob" Output: "bob like even not does Alice" Constraints: 1 <= s.length <= 104 s contains English letters (upper-case and lower-case), digits, and spaces ' '. There is at least one word in s.
class Solution: def reverseWords(self, s: str) -> str: curr_word = "" ans = [] for char in s: if char == " ": if curr_word != "": ans.append(curr_word) curr_word = "" else: curr_word += char if curr_word != "": ans.append(curr_word) return " ".join(ans[::-1])
CLASS_DEF FUNC_DEF VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR NUMBER VAR
Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1: Input: s = "the sky is blue" Output: "blue is sky the" Example 2: Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Example 4: Input: s = " Bob Loves Alice " Output: "Alice Loves Bob" Example 5: Input: s = "Alice does not even like bob" Output: "bob like even not does Alice" Constraints: 1 <= s.length <= 104 s contains English letters (upper-case and lower-case), digits, and spaces ' '. There is at least one word in s.
class Solution: def reverseWords(self, s: str) -> str: sLst = s.split() reverseStr = "" for i in range(len(sLst) - 1, -1, -1): if i == len(sLst) - 1: reverseStr += sLst[i] else: reverseStr += " " + sLst[i] return reverseStr
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP STRING VAR VAR RETURN VAR VAR
Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Example 1: Input: s = "the sky is blue" Output: "blue is sky the" Example 2: Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Example 4: Input: s = " Bob Loves Alice " Output: "Alice Loves Bob" Example 5: Input: s = "Alice does not even like bob" Output: "bob like even not does Alice" Constraints: 1 <= s.length <= 104 s contains English letters (upper-case and lower-case), digits, and spaces ' '. There is at least one word in s.
class Solution: def reverseWords(self, s: str) -> str: s = s.strip(" ") ans = "" for segment in s.split(" "): if segment == "": continue ans = segment + " " + ans return ans[:-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR RETURN VAR NUMBER VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) for it in range(t): n, k = input().split(" ") n = int(n) k = int(k) stdin = input().split(" ") a = [int(stdin[i]) for i in range(n)] state = 0 mx = 0 count = 0 maxe = -1 pos = -1 i = 0 while i < n: if state == 0: count += 1 if a[i] > k: state = 1 pos = i maxe = a[i] elif a[i] <= k or a[i] == maxe: count += 1 if a[i] == maxe: pos = i else: mx = max(mx, count) count = i - pos pos = i maxe = a[i] i += 1 if state != 0: mx = max(mx, count) print(mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for i in range(int(input())): n, k = map(int, input().split()) flag, prevhidx, prevh = 0, 0, 0 urr, mx = 0, 0 idx = [] a = list(map(int, input().split())) for i in range(n): if a[i] > k: if flag == 0: flag = 1 prevhidx = i prevh = a[i] urr += 1 elif a[i] == prevh: prevhidx = i urr += 1 else: if urr > mx: mx = urr urr = i - prevhidx prevh = a[i] prevhidx = i else: urr += 1 if urr > mx: mx = urr if flag == 1: print(mx) else: print(flag)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) while t: a = input().split() n = int(a[0]) k = int(a[1]) lst = input().split() for i in range(n): lst[i] = int(lst[i]) greater = 0 length = 0 pos = -1 allTimeLong = 0 for i in range(n): if lst[i] > k and greater != 0 and lst[i] != greater: greater = lst[i] if allTimeLong < length: allTimeLong = length length = i - pos pos = i continue elif lst[i] > k: greater = lst[i] pos = i length += 1 if allTimeLong < length: allTimeLong = length t -= 1 print(allTimeLong)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
import sys from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n, k = map(int, stdin.readline().strip().split(" ")) arr = list(map(int, stdin.readline().strip().split(" "))) tarr = [] p = -1 for i in range(len(arr)): if arr[i] > k: if p == -1: p = arr[i] tarr.append(i) elif arr[i] != p: tarr.append(i) p = arr[i] if len(tarr) == 0: print(0) elif len(tarr) == 1: print(len(arr)) else: mtn = max(tarr[1], +len(tarr) - tarr[-2] - 1) for i in range(1, len(tarr) - 1): mtn = max(mtn, tarr[i] - tarr[i - 1] + tarr[i + 1] - tarr[i] - 1) print(mtn)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
T = int(input()) for q in range(T): n, m = map(int, input().strip().split()) arr = list(map(int, input().strip().split())) c = 0 ind = [] mp = {} k = 0 h = 0 arr.append(max(arr) + 1) for i in range(n + 1): if arr[i] <= m: c += 1 elif h == 0: h = 1 k = i c += 1 elif arr[k] == arr[i]: c += 1 k = i else: ind.append(c) c = max(1, i - k) k = i if len(ind) == 0: print(0) else: print(max(ind))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
a = int(input()) for i in range(a): cc = list(map(int, input().split())) dd = list(map(int, input().split())) for i in range(len(dd)): dd[i] -= cc[1] maxs = 0 i = 0 k = 0 while i < len(dd): j = i + 1 if dd[i] > 0: count = 1 + (i - k) k = i + 1 while j < len(dd) and (dd[j] <= 0 or dd[j] == dd[i]): if dd[j] == dd[i]: k = j + 1 count += 1 j += 1 if count > maxs: maxs = count i = j print(maxs)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) while t > 0: n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] first = 0 prev = 0 i = 0 len = 0 maxlen = 0 while i < n: if a[i] > k: if first == 0: first = a[i] prev = i len += 1 elif first == a[i]: len += 1 prev = i else: first = 0 i = prev if maxlen < len: maxlen = len len = 0 else: len += 1 i += 1 else: if maxlen < len: maxlen = len print(maxlen) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
def solve(arr, n, k): if max(arr) <= k: return 0 ans = 0 m = -1 f = 0 i = 0 j = -1 while j < n - 1: if arr[j + 1] <= k: j += 1 elif arr[j + 1] > k: if m == -1: m = arr[j + 1] f += 1 j += 1 elif arr[j + 1] == m: f += 1 j += 1 else: ans = max(ans, j - i + 1) while f: if arr[i] == m: f -= 1 i += 1 m = -1 f = 0 ans = max(ans, j - i + 1) return ans for _ in range(int(input())): n, k = list(map(int, input().split())) arr = list(map(int, input().split())) print(solve(arr, n, k))
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) for i in range(t): n, k = map(int, input().strip().split()) a = list(map(int, input().strip().split())) c = 0 x = 0 b = list() i1 = 0 i2 = 0 for j in range(n): c += 1 if a[j] > k: x += 1 if x == 1: i1 = j elif a[j] == a[i1]: x -= 1 else: i2 = j if x == 2: x = 1 b.append(c - 1) c = i2 - i1 i1 = i2 i2 = 0 if j == n - 1 and b == []: b.append(n) print(max(b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) for _ in range(t): n, k = list(map(int, input().strip().split())) a = list(map(int, input().strip().split())) c = 0 f = 0 b = [] d = [0, 0] for i in range(n): if a[i] > k: f += 1 if f == 1: d[0] = i elif a[i] == a[d[0]]: f -= 1 else: d[1] = i c += 1 if f == 2: f = 1 b.append(c - 1) c = d[1] - d[0] d[0] = d[1] d[1] = 0 if len(b) == 0: print(c) else: print(max(b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
def answer(): for t in range(int(input())): N, K = [int(x) for x in input().split()] l = [-1] l += [int(x) for x in input().split()] green = 0 orange = 0 red = 0 ans = 0 while True: orange += 1 if orange == N + 1: ans = max(ans, N - green) break elif l[orange] > K: red = orange while True: red += 1 if red == N + 1: ans = max(ans, N - green) break elif l[red] > K: if l[red] != l[orange]: ans = max(ans, red - green - 1) green = orange orange = red - 1 break else: orange = red print(ans) return answer()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
import sys def _int(): return int(sys.stdin.readline()) def _ints(): return map(int, sys.stdin.readline().split()) def _intarr(): return list(map(int, sys.stdin.readline().split())) def _str(): return sys.stdin.readline() def _strarr(): return sys.stdin.readline().split() t = _int() ans = [] for _ in range(t): n, m = _ints() arr = _intarr() start = -1 end = -1 index = -1 ans = 0 for i in range(n): if start == -1: start = i if arr[i] > m: index = i elif arr[i] > m: if index == -1: index = i elif arr[index] != arr[i]: end = i ans = max(ans, end - start) start = index + 1 index = i if end != n - 1: end = n ans = max(ans, end - start) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for _ in range(int(input())): n, k = list(map(int, input().split())) arr = list(map(int, input().split())) if n == 1: if arr[0] >= k: print(2) else: print(1) elif n == 2: mx, mn = max(arr[0], arr[1]), min(arr[0], arr[1]) if mn >= k: if mx == mn: print(2) else: print(1) elif mx >= k > mn: print(1) else: print(0) else: mx = 0 count = 0 f = 0 f_pos = -1 for pos, num in enumerate(arr): if f == 0: if num >= k: f = num f_pos = pos count += 1 elif num <= k: count += 1 elif num == f: count += 1 else: count = pos - f_pos f_pos = pos if count > mx: mx = count print(mx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) def check(mid): d, left = {}, 0 for i in range(mid): if a[i] > k: if a[i] not in d: d[a[i]] = 1 else: d[a[i]] += 1 if len(d) == 1: return True for i in range(mid, n): if a[left] > k: d[a[left]] -= 1 if d[a[left]] == 0: del d[a[left]] if a[i] > k: if a[i] not in d: d[a[i]] = 1 else: d[a[i]] += 1 if len(d) == 1: return True left += 1 return False lo, hi = 0, n while lo <= hi: mid = (lo + hi) // 2 if check(mid): res = mid lo = mid + 1 else: hi = mid - 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for i in range(int(input())): n, k = input().split() n = int(n) k = int(k) arr = list(map(int, input().split())) a = 0 b = 0 count = 0 ans = 0 temp = -1 w = 0 index1 = index2 = None while b < n: if arr[b] > k: if count == 0: index1 = b count += 1 elif arr[b] == arr[index1]: index1 = b else: index2 = b count += 1 if count == 2: w = 1 ans = max(ans, b - a) a = index1 + 1 index1 = index2 index2 = None count = 1 b += 1 if w == 0 and count == 1: print(n) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for _ in range(int(input())): input_list = input().split() N, K = int(input_list[0]), int(input_list[1]) A = list(map(int, input().split())) index = [] greater_elem = -1 for i in range(0, N): if A[i] > K and A[i] != greater_elem: index.append(i) greater_elem = A[i] if len(index) == 0: print(0) elif len(index) == 1: print(N) elif len(index) == 2: print(index[1] - index[0]) else: max_sublen = 0 for i in range(len(index) - 2): max_sublen = max(max_sublen, index[i + 2] - index[i] - 1) print(max_sublen)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for _ in range(int(input())): n, k = list(map(int, input().split())) ays = list(map(int, input().split())) gt = k gtp = -1 mseg = 0 seg = 0 for ax, a in enumerate(ays): if a <= k: seg += 1 elif a == gt: seg += 1 gtp = ax else: if seg > mseg: mseg = seg gt = a seg = ax - gtp gtp = ax if gt == k: mseg = 0 elif seg > mseg: mseg = seg print(mseg)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
T = int(input()) for _ in range(T): N, K = map(int, input().split()) A = list(map(int, input().split())) lower = [] ans = [] if A[0] > K: ans.append(1) lower.append(0) M = A[0] else: ans.append(0) lower.append(1) for i in range(1, N): if A[i] > K: if ans[-1] != 0: if M == A[i]: ans.append(ans[-1] + 1) else: ans.append(lower[-1] + 1) M = A[i] else: ans.append(lower[-1] + 1) M = A[i] lower.append(0) else: if ans[-1] != 0: ans.append(ans[-1] + 1) else: ans.append(0) lower.append(lower[-1] + 1) print(max(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) for j in range(t): n, k = list(map(int, input().split())) a = list(map(int, input().split())) mm = 0 x = 1 l = 0 m = -1 for i in range(n): if x == 1: if a[i] > k: c = i l = l + 1 m = a[i] x = 0 continue if a[i] <= k or a[i] == m: if a[i] == m: c = i l = l + 1 if l > mm: mm = l else: if l > mm: mm = l l = i - c c = i if l > mm: mm = l m = a[i] print(mm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) c = 0 d = {} l = 0 r = 0 sm = 0 m = 1 i = 0 while i < n: if a[i] > k: if a[i] not in d or d[a[i]] == -1: c += 1 d[a[i]] = i if c > 1: l = d[sm] + 1 d[sm] = -1 c -= 1 sm = a[i] r += 1 i += 1 if r - l > m: m = r - l print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Find the length of the longest contiguous segment in an array, in which if a given element $K$ is inserted, $K$ becomes the second largest element of that subarray. -----Input:----- - The first line will contain $T$, number of test cases. Then the test cases follow. - The first line of each test case contains two integers $N$ and $K$. - The next line contains N space-separated integers Ai denoting the elements of the array. -----Output:----- Print a single line corresponding to each test case — the length of the largest segment. -----Constraints----- - $1 \leq T \leq 10$ - $1 \leq N \leq 10^6$ - $1 \leq Ai, K \leq 10^9$ - Sum of N across all test cases doesn't exceed $10^6$ -----Sample Input:----- 2 5 3 2 4 2 4 2 8 5 9 3 5 7 8 11 17 2 -----Sample Output:----- 5 3 -----EXPLANATION:----- If 3 is inserted at anywhere in the array, it is the second largest element. Hence the maximum length is 5. If 5 is inserted anywhere between 1st and 4th element, it is the second largest element. The length of such subarray is 3.
t = int(input()) for i in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) lt = list() nos = list() p = -1 for j in range(n): if a[j] > k: nos.append(a[j]) lt.append(j + 1) lt = [0] + lt + [n + 1] l, j = 0, 0 tm = len(lt) nos = [0] + nos + [nos[tm - 3]] while j < tm - 2: xt = j + 2 while xt + 1 < tm and nos[xt] == nos[j + 1]: xt += 1 ct = lt[xt] - lt[j] - 1 if ct > l: l = ct j = xt - 1 print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m). Calculate the number of distinct arrays a, consisting of n integers such that: * 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n; * bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m. Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i. The number can be pretty large so print it modulo 998244353. Input The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively. Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it. Output Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353. Examples Input 4 3 2 1 3 3 3 4 6 Output 3 Input 5 2 3 1 3 2 2 5 0 3 3 3 Output 33 Note You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND). In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6].
import sys def main(): import sys input = sys.stdin.buffer.readline mod = 998244353 N, K, M = map(int, input().split()) cond = [] for _ in range(M): cond.append(tuple(map(int, input().split()))) ans = 1 for k in range(K): one = [0] * (N + 1) zero_cond = [0] * (N + 1) for l, r, x in cond: if x >> k & 1: one[l - 1] += 1 one[r] -= 1 else: zero_cond[r] = max(zero_cond[r], l) for i in range(N): one[i + 1] += one[i] dp = [0] * (N + 1) dp[0] = 1 cs = [0] * (N + 2) cs[1] = 1 R = 0 for i in range(N): if not one[i]: dp[i + 1] = (cs[i + 1] - cs[R]) % mod cs[i + 2] = (cs[i + 1] + dp[i + 1]) % mod R = max(R, zero_cond[i + 1]) ans = ans * (cs[-1] - cs[R]) % mod % mod print(ans) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], k: int) -> int: flipped = collections.deque() flips = 0 for i in range(len(A)): if flipped and flipped[0] < i: flipped.popleft() if not (len(flipped) & 1) - A[i]: if i <= len(A) - k: flips += 1 flipped.append(i + k - 1) else: return -1 return flips
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: is_flipped = [0] * len(A) flipped = result = 0 for i in range(len(A)): if i >= K: flipped ^= is_flipped[i - K] if A[i] == flipped: if i + K > len(A): return -1 is_flipped[i] = 1 flipped ^= 1 result += 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: N = len(A) flipped = [(0) for _ in range(N)] curr = 0 ans = 0 for i in range(N): if i >= K: curr ^= flipped[i - K] if curr == A[i]: if i + K > N: return -1 flipped[i] = 1 curr ^= 1 ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, a: List[int], k: int) -> int: n = len(a) toggles = [0] * (n + 1) min_flips = 0 view = 0 for i, b in enumerate(a): view ^= toggles[i] if b ^ view == 0: if i + k > n: return -1 min_flips += 1 view ^= 1 toggles[i + k] ^= 1 return min_flips
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], k: int) -> int: c = 0 flippedtill = [(0) for i in range(len(A) + 1)] curr = 0 if len(A) == k: if sum(A) == 0: return 1 elif sum(A) == len(A): return 0 else: return -1 for i in range(len(A) - k + 1): curr = (curr + flippedtill[i]) % 2 if (A[i] + curr) % 2 == 0: flippedtill[i + 1] += 1 flippedtill[i + k] -= 1 c += 1 for i in range(len(A) - k + 1, len(A)): curr = (curr + flippedtill[i]) % 2 if (A[i] + curr) % 2 == 0: return -1 return c
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) num = 0 flip_queue = [] flip_sum = 0 for i in range(n - K + 1): if flip_sum & 1: add = 1 if A[i] == 1 else 0 else: add = 1 if A[i] == 0 else 0 num += add flip_queue.append(add) flip_sum += add if len(flip_queue) >= K: flip_sum -= flip_queue.pop(0) for j in range(n - K + 1, n): if flip_sum & 1: if A[j] == 1: return -1 elif A[j] == 0: return -1 flip_queue.append(0) if len(flip_queue) >= K: flip_sum -= flip_queue.pop(0) return num
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flips = collections.deque() cnt = 0 for i in range(len(A)): while flips and flips[0] <= i - K: flips.popleft() curtStatus = A[i] + len(flips) if curtStatus % 2 == 0: if i > len(A) - K: return -1 else: flips.append(i) cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: nums = A flip_count = 0 flip_hint = [0] * len(nums) total_flips = 0 for i, n in enumerate(nums): if i >= K and flip_hint[i - K]: total_flips -= 1 if total_flips % 2 == n: if i + K > len(nums): return -1 total_flips += 1 flip_hint[i] = 1 flip_count += 1 return flip_count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], k: int) -> int: k_bit_flip_end_indices = deque() switch = 0 flips = 0 for i, bit in enumerate(A): if len(k_bit_flip_end_indices) & 1 ^ bit == 0: flips += 1 k_bit_flip_end_indices.append(i + k - 1) if k_bit_flip_end_indices and i == k_bit_flip_end_indices[0]: k_bit_flip_end_indices.popleft() return flips if not k_bit_flip_end_indices else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR VAR NUMBER VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: q = collections.deque() res = 0 for i, num in enumerate(A): while len(q) > 0 and i - q[0] >= K: q.popleft() if len(q) % 2 == 1 and num == 1 or len(q) % 2 == 0 and num == 0: if i + K > len(A): return -1 q.append(i) res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: Fliped = [0] * len(A) cnt = 0 for i in range(len(A)): if i >= K: fliped = cnt - Fliped[i - K] else: fliped = cnt if A[i] == 0 and fliped % 2 == 0 or A[i] == 1 and fliped % 2 == 1: if i + K > len(A): return -1 A[i] = 1 cnt += 1 Fliped[i] = cnt return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: l = len(A) flipped = [0] * l res = 0 flipping_count = 0 for i in range(l): if i >= K: flipping_count -= flipped[i - K] if ( flipping_count % 2 == 0 and A[i] == 0 or flipping_count % 2 == 1 and A[i] == 1 ): if i + K > l: return -1 flipped[i] += 1 flipping_count += 1 res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: res = 0 queue = collections.deque([]) for i in range(len(A)): while queue and queue[0] < i: queue.popleft() if (A[i] + len(queue)) % 2 == 0: if i + K - 1 >= len(A): return -1 else: queue.append(i + K - 1) res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flip_time = 0 n = len(A) close = [(False) for i in range(n)] res = 0 for i in range(n): if close[i]: flip_time -= 1 if A[i] == 0 and flip_time % 2 == 0 or A[i] == 1 and flip_time % 2 == 1: flip_time += 1 res += 1 if i + K > n: return -1 if i + K < n: close[i + K] = True return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: counter = 0 q = [] qL = 0 for i in range(len(A) - K + 1): if A[i] == (len(q) - qL) % 2: counter += 1 q.append(i + K - 1) if qL < len(q) and q[qL] == i: qL += 1 for i in range(len(A) - K + 1, len(A)): if A[i] == (len(q) - qL) % 2: return -1 if qL < len(q) and q[qL] == i: qL += 1 return counter
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: ALen = len(A) flips = [] prevF = 0 ans = 0 l = 0 for i, num in enumerate(A): temp = 0 while l + temp < len(flips) and flips[l + temp] + K - 1 < i: temp += 1 l += temp prevF = prevF - temp if prevF % 2 and num == 0 or prevF % 2 == 0 and num == 1: continue else: ans += 1 prevF += 1 flips.append(i) if flips != [] and flips[-1] + K - 1 >= ALen: return -1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) flip = 0 current_flips = 0 queue = [] for i in range(n): if len(queue) > 0 and queue[0] + K == i: queue.pop(0) current_flips = current_flips - 1 if ( A[i] == 0 and current_flips % 2 == 0 or A[i] == 1 and current_flips % 2 != 0 ): if i + K > n: return -1 queue.append(i) current_flips = current_flips + 1 flip = flip + 1 return flip
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) record = [0] * n flip = 0 ans = 0 for i in range(n): if i >= K: flip -= record[i - K] if A[i] == flip % 2: if i > n - K: return -1 ans += 1 flip += 1 record[i] = 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: dq = deque() res = 0 for i in range(len(A)): if A[i] == len(dq) % 2: res += 1 dq.append(i + K - 1) if dq and dq[0] <= i: dq.popleft() return res if not dq else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR VAR NUMBER VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flip_ends = deque() count = 0 for idx, bit in enumerate(A): if len(flip_ends) > 0 and idx >= flip_ends[0]: flip_ends.popleft() val = (bit + len(flip_ends)) % 2 if val == 1: continue if idx + K > len(A): return -1 count += 1 flip_ends.append(idx + K) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: ans = flip = 0 N = len(A) hint = [0] * N for i, x in enumerate(A): flip ^= hint[i] if x ^ flip == 0: ans += 1 if i + K > N: return -1 flip ^= 1 if i + K < N: hint[i + K] ^= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], k: int) -> int: n = len(A) count = [0] * n ps = 0 res = 0 for i in range(n - k + 1): flip = ps + A[i] if flip % 2 == 0: count[i] = 1 res += 1 ps += 1 if i - k + 1 >= 0: ps -= count[i - k + 1] for i in range(n - k + 1, n): if (ps + A[i]) % 2 == 0: return -1 ps -= count[i - k + 1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER RETURN NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) flips = [0] * n flip = 0 ans = 0 def mapping(a: int, flip: int) -> int: return 1 - a if flip % 2 == 1 else a for i, a in enumerate(A): flip += flips[i] if mapping(a, flip) == 0: if i + K - 1 < n: ans += 1 flips[i] += 1 flip += 1 A[i] = 1 if i + K < n: flips[i + K] -= 1 else: return -1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR RETURN BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution(object): def minKBitFlips(self, A, K): N = len(A) flip = 0 flip_ends = [0] * (N + 1) res = 0 for i, num in enumerate(A): flip ^= flip_ends[i] if not num ^ flip: if i + K > N: return -1 else: res += 1 flip ^= 1 flip_ends[i + K] = 1 return res
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flips = 0 q = deque() for i, x in enumerate(A): if q and q[0] == i: q.popleft() if len(q) & 1: x ^= 1 if x == 0: flips += 1 q.append(i + K) if i + K > len(A): return -1 return flips
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: num = 0 for a in A: num = (num << 1) + a i = 0 pattern = (1 << K) - 1 flips = 0 while i + K <= len(A): while i + K <= len(A) and num & 1 << i: i += 1 if i + K > len(A): break num ^= pattern << i flips += 1 i += 1 if num == (1 << len(A)) - 1: return flips return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN NUMBER VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: i = 0 c = 0 q = collections.deque() sign = 0 while True: while i < len(A) and A[i] != sign: i += 1 if q and i == q[0]: q.popleft() sign = 1 - sign if i == len(A): return c if i + K > len(A): return -1 q.append(i + K) sign = 1 - sign c += 1 i += 1 if q and i == q[0]: q.popleft() sign = 1 - sign return c
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR RETURN VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) ff = [(0) for i in range(n)] cur = 0 f = 0 for i in range(n): cur += ff[i] xo = cur % 2 if xo ^ A[i] == 0: if i + K - 1 > n - 1: return -1 else: f += 1 ff[i] += 1 if i + K < n: ff[i + K] -= 1 cur += 1 return f
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: i = 0 ans = 0 flips = 0 dq = collections.deque() while i < len(A): while dq and i >= dq[0]: dq.popleft() flips -= 1 if (A[i] + flips) % 2 == 1: i += 1 else: if i + K > len(A): return -1 dq.append(i + K) flips += 1 i += 1 ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) ans = 0 flips = 0 addon = 6 m = False for i in range(n): if A[i] > 5: m = True A[i] -= addon if flips % 2 == 0 and A[i] == 0 or flips % 2 != 0 and A[i] == 1: ans += 1 if i + K > n: return -1 A[i + K - 1] += addon flips += 1 if m or A[i] > 5: flips -= 1 A[i] -= addon m = False return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: total_flips = 0 current_effective_flips = 0 flip_indices = [] for current_index, elem in enumerate(A): if flip_indices and current_index == flip_indices[0] + K: flip_indices.pop(0) current_effective_flips -= 1 if self.needs_flip(elem, current_effective_flips): if current_index + K > len(A): return -1 flip_indices.append(current_index) current_effective_flips += 1 total_flips += 1 return total_flips def needs_flip(self, elem, current_effective_flips): return ( elem == 0 and current_effective_flips % 2 == 0 or elem == 1 and current_effective_flips % 2 != 0 )
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER