{"id":10343,"date":"2025-04-12T10:09:15","date_gmt":"2025-04-12T17:09:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10343"},"modified":"2025-04-12T10:10:57","modified_gmt":"2025-04-12T17:10:57","slug":"leetcode-2024-maximize-the-confusion-of-an-exam","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-2024-maximize-the-confusion-of-an-exam\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2024. Maximize the Confusion of an Exam"},"content":{"rendered":"\n<p>\u6570\u636e\u89c4\u6a21\u9ad8\u8fbe5 *10<sup>4<\/sup>,\u6211\u4eec\u53ea\u80fd\u4f7f\u7528O(n)\u7684\u7b97\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u60f3\u5230\u7684\u5c31\u662f\u6ed1\u52a8\u7a97\u53e3(sliding window)\uff0c\u7531\u4e8e\u6700\u957f\u957f\u5ea6\u672a\u77e5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6ed1\u52a8\u7a97\u53e3\u3002<\/p>\n\n\n\n<p>\u8bb0\u5f55\u5f53\u524d\u6ed1\u52a8\u7a97\u53e3\u4e2dT\u548cF\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5982\u679c\u5176\u4e2d\u8f83\u5c11\u7684\u4e00\u4e2a&lt;=k\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u5168\u90e8\u66ff\u6362\u5b83\uff0c\u4f7f\u5f97\u6574\u4e2a\u6ed1\u52a8\u7a97\u53e3\u90fd\u53d8\u6210\u76f8\u540c\u7684\u503c\u3002\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u6ed1\u52a8\u7a97\u53e3\u957f\u5ea6\u5927\u4e8e\u5f53\u524d\u6700\u5927\u957f\u5ea6\uff0c\u6211\u4eec\u5c31\u628a\u6ed1\u52a8\u7a97\u53e3\u53d8\u5927\uff0c\u53f3\u4fa7+1\uff0c\u5e76\u66f4\u65b0\u6700\u5927\u957f\u5ea6\u3002\u5426\u5219\uff0c\u51cf\u5c11\u6ed1\u52a8\u7a97\u53e3\uff0c\u5de6\u4fa7-1\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(2)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maxConsecutiveAnswers(string_view A, int k) {\n    int ans = 0;\n    int f = 0;\n    array<int, 2> count; \/\/ number of 'F' and 'T' in the sliding window\n    for (int i = 0; i < A.size(); ++i) {\n      ++count[A[i] == 'T'];\n      if (min(count[0], count[1]) <= k &#038;&#038; count[0] + count[1] > ans)\n        ++ans;\n      else\n        --count[A[i - ans] == 'T'];\n    }\n    return ans;\n  }\n};\n\n\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u89c4\u6a21\u9ad8\u8fbe5 *104,\u6211\u4eec\u53ea\u80fd\u4f7f\u7528O(n)\u7684\u7b97\u6cd5\u4e86\u3002 \u53ef\u4ee5\u60f3\u5230\u7684\u5c31\u662f\u6ed1\u52a8\u7a97\u53e3(sliding window)\uff0c\u7531\u4e8e\u6700\u957f\u957f\u5ea6\u672a\u77e5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6ed1\u52a8\u7a97\u53e3\u3002 \u8bb0\u5f55\u5f53\u524d\u6ed1\u52a8\u7a97\u53e3\u4e2dT\u548cF\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5982\u679c\u5176\u4e2d\u8f83\u5c11\u7684\u4e00\u4e2a&lt;=k\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u5168\u90e8\u66ff\u6362\u5b83\uff0c\u4f7f\u5f97\u6574\u4e2a\u6ed1\u52a8\u7a97\u53e3\u90fd\u53d8\u6210\u76f8\u540c\u7684\u503c\u3002\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u6ed1\u52a8\u7a97\u53e3\u957f\u5ea6\u5927\u4e8e\u5f53\u524d\u6700\u5927\u957f\u5ea6\uff0c\u6211\u4eec\u5c31\u628a\u6ed1\u52a8\u7a97\u53e3\u53d8\u5927\uff0c\u53f3\u4fa7+1\uff0c\u5e76\u66f4\u65b0\u6700\u5927\u957f\u5ea6\u3002\u5426\u5219\uff0c\u51cf\u5c11\u6ed1\u52a8\u7a97\u53e3\uff0c\u5de6\u4fa7-1\u3002 \u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n)\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(2) class Solution { public: int maxConsecutiveAnswers(string_view A, int k) { int ans = 0; int f = 0; array&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[177,215,4],"class_list":["post-10343","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-medium","tag-sliding-window","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10343"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10343\/revisions"}],"predecessor-version":[{"id":10347,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10343\/revisions\/10347"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}