-
Leetcode 2024. Maximize the Confusion of an Exam개발/Leetcode 2023. 7. 7. 12:14
https://leetcode.com/problems/maximize-the-confusion-of-an-exam/description/
Maximize the Confusion of an Exam - LeetCode
Can you solve this real interview question? Maximize the Confusion of an Exam - A teacher is writing a test with n true/false questions, with 'T' denoting true and 'F' denoting false. He wants to confuse the students by maximizing the number of consecutive
leetcode.com
문제
선생님은 참(T)과 거짓(F)으로 나뉘는 문제들을 작성하고 있다. 선생님은 학생들을 혼란스럽게 하기 위해 최대한 연속해서 같은 답이 나올수 있도록 하고자 한다.
문자열 answerKey가 주어지며 answerKey[i]에는 문제에 대한 답이 주어진다. 또한 정수 k가 주어지며 이 k는 다음의 작업을 수행할 수 있는 최대의 횟수이다.
- 문제의 답을 바꿀 수 있는 횟수이다. 만약 정답이 참(T)인 경우 거짓(F)로 바꿀수 있으며 반대의 상황도 가능하다.
위의 조건을 활용하여 최대로 연속할 수 있는 T와 F의 갯수를 구하여라.
코드
class Solution:def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:if min([answerKey.count('F'), answerKey.count('T')]) <= k:return len(answerKey)
answer = 0m = {'T': 0, 'F': 0}l, r = 0, 1m[answerKey[0]] = 1
while r < len(answerKey) and l < r:next_key = answerKey[r]# m_key = min(m, key=m.get)vals = list(m.values())if next_key == 'T':vals[0] += 1else:vals[1] += 1
if any(map(lambda x: x <= k, vals)):m[answerKey[r]] += 1r += 1else:m[answerKey[l]] -= 1l += 1answer = max(answer, r - l)return answer
너무 어렵게 생각한건지 시간이 한참 걸렸다..
'개발 > Leetcode' 카테고리의 다른 글
Leetcode 802. Find Eventual Safe States (0) 2023.07.12 Leetcode 863. All Nodes Distance K in Binary Tree (0) 2023.07.11 Leetcode 209. Minimum Size Subarray Sum (0) 2023.07.06 Leetcode 1493. Longest Subarray of 1's After Deleting One Element (0) 2023.07.05 Leetcode 137. Single Number II (0) 2023.07.04