Leetcode # 2024. Maximize the Confusion of an Exam

Problem

https://leetcode.com/problems/maximize-the-confusion-of-an-exam

Solution: Sliding Window

Time Complexity: O(len(answerKey))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
    left = ans = 0
    nt = nf = 0
    for right in range(len(answerKey)):
      if answerKey[right] == "T":
        nt += 1
      else:
        nf += 1

      while min(nt, nf) > k:
        if answerKey[left] == "T":
          nt -= 1
        else:
          nf -= 1
        left += 1

      ans = max(ans, right - left + 1)
    return ans

 

Last Updated on 2023/09/07 by A1go

目錄
Bitnami