1"use strict";(self.webpackChunkcode_fictionist=self.webpackChunkcode_fictionist||[]).push([[67],{28453:(e,n,t)=>{t.d(n,{R:()=>c,x:()=>o});var r=t(96540);const s={},i=r.createContext(s);function c(e){const n=r.useContext(i);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:c(e.components),r.createElement(i.Provider,{value:n},e.children)}},99262:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>a,frontMatter:()=>c,metadata:()=>r,toc:()=>u});const r=JSON.parse('{"id":"notes/Algorithm/LeetCode/Queue","title":"[Queue] 933. Number of Recent Calls","description":"Leetcode 933. Number of Recent Calls","source":"@site/docs/notes/Algorithm/LeetCode/933-Queue.mdx","sourceDirName":"notes/Algorithm/LeetCode","slug":"/notes/Algorithm/LeetCode/Queue","permalink":"/docs/notes/Algorithm/LeetCode/Queue","draft":false,"unlisted":false,"tags":[{"inline":true,"label":"leetcode","permalink":"/docs/tags/leetcode"},{"inline":true,"label":"queue","permalink":"/docs/tags/queue"}],"version":"current","lastUpdatedAt":1767358364000,"sidebarPosition":933,"frontMatter":{"title":"[Queue] 933. Number of Recent Calls","tags":["leetcode","queue"],"keywords":["LeetCode","Queue","Number of Recent Calls"]},"sidebar":"notes","previous":{"title":"[DP] 518. Coin Change II","permalink":"/docs/notes/Algorithm/LeetCode/coin_change"},"next":{"title":"Docusaurus","permalink":"/docs/category/docusaurus"}}');var s=t(74848),i=t(28453);const c={title:"[Queue] 933. Number of Recent Calls",tags:["leetcode","queue"],keywords:["LeetCode","Queue","Number of Recent Calls"]},o=void 0,l={},u=[{value:"\u984c\u76ee\u8981\u6c42",id:"\u984c\u76ee\u8981\u6c42",level:2},{value:"\u89e3\u984c\u601d\u8def",id:"\u89e3\u984c\u601d\u8def",level:2}];function d(e){const n={a:"a",admonition:"admonition",br:"br",code:"code",h2:"h2",li:"li",ol:"ol",p:"p",pre:"pre",ul:"ul",...(0,i.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.admonition,{type:"info",children:(0,s.jsx)(n.p,{children:(0,s.jsx)(n.a,{href:"https://leetcode.com/problems/number-of-recent-calls/description/",children:"Leetcode 933. Number of Recent Calls"})})}),"\n",(0,s.jsx)(n.h2,{id:"\u984c\u76ee\u8981\u6c42",children:"\u984c\u76ee\u8981\u6c42"}),"\n",(0,s.jsx)(n.p,{children:"You have a RecentCounter class which counts the number of recent requests within a certain time frame."}),"\n",(0,s.jsx)(n.p,{children:"Implement the RecentCounter class:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"RecentCounter()"})," Initializes the counter with zero recent requests."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"int ping(int t)"})," Adds a new request at time ",(0,s.jsx)(n.code,{children:"t"}),", where ",(0,s.jsx)(n.code,{children:"t"})," represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range ",(0,s.jsx)(n.code,{children:"[t - 3000, t]"}),"."]}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"It is guaranteed that every call to ping uses a strictly larger value of t than the previous call."}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{children:'Example 1:\n\nInput\n["RecentCounter", "ping", "ping", "ping", "ping"]\n[[], [1], [100], [3001], [3002]]\nOutput\n[null, 1, 2, 3, 3]\n\nExplanation\nRecentCounter recentCounter = new RecentCounter();\nrecentCounter.ping(1); // requests = [1], range is [-2999,1], return 1\nrecentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2\nrecentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3\nrecentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3\n'})}),"\n",(0,s.jsx)(n.h2,{id:"\u89e3\u984c\u601d\u8def",children:"\u89e3\u984c\u601d\u8def"}),"\n",(0,s.jsxs)(n.p,{children:["\u9019\u984c\u5176\u5be6\u6eff\u76f4\u89c0\u7684\uff0c\u82f1\u6587\u6558\u8ff0\u5176\u5be6\u5beb\u5f97\u6eff\u7e5e\u7684\uff0c\u4f46\u5176\u5be6\u57fa\u672c\u4e0a\u5c31\u662f ",(0,s.jsx)(n.code,{children:"t"})," \u4ee3\u8868\u7684\u65e2\u662f request \u4e5f\u662f\u9019\u500b request \u767c\u751f\u7684\u6642\u9593\u9ede\u3002",(0,s.jsx)(n.br,{}),"\n","\u90a3\u984c\u76ee\u8981\u6c42\u7684 ",(0,s.jsx)(n.code,{children:"ping(t)"})," \u5176\u5be6\u5c31\u662f\u8981\u6211\u5011\u56de\u50b3\u5728 ",(0,s.jsx)(n.code,{children:"[t-3000, t]"})," \u9019\u500b\u6642\u9593\u5340\u9593\u5167\u767c\u751f\u7684 request \u6578\u91cf\u3002",(0,s.jsx)(n.br,{}),"\n","\u7136\u5f8c\u6bcf\u6b21\u8abf\u7528 ",(0,s.jsx)(n.code,{children:"ping"})," \u6642\u50b3\u5165\u7684 ",(0,s.jsx)(n.code,{children:"t"})," \u503c\u90fd\u662f\u905e\u589e\u7684\u3002",(0,s.jsx)(n.br,{}),"\n","\u90a3\u9019\u88e1\u6709\u500b\u5c0f\u9677\u9631\uff0c\u4f46\u5176\u5be6\u5c31\u662f\u8aaa\u6587\u89e3\u5b57\u5566\uff0c\u4ed6\u56de\u50b3\u7684\u300c\u6642\u9593\u5340\u9593\u5167\u7684 request \u6578\u91cf\u300d\uff0c\u662f\u5305\u542b\u904e\u53bb\u8abf\u7528 ",(0,s.jsx)(n.code,{children:"ping"})," \u50b3\u5165\u7684\u6240\u6709 ",(0,s.jsx)(n.code,{children:"t"})," \u503c\uff0c\u6240\u4ee5\u6211\u5011\u5f97\u5132\u5b58\u6bcf\u6b21\u8abf\u7528 ",(0,s.jsx)(n.code,{children:"ping"})," \u50b3\u5165\u7684 ",(0,s.jsx)(n.code,{children:"t"})," \u503c\u3002"]}),"\n",(0,s.jsxs)(n.p,{children:["\u90a3\u63a5\u4e0b\u4f86 leetcode \u9084\u6709\u4e00\u500b\u9700\u8981\u6ce8\u610f\u7684\u9ede\u662f\u57f7\u884c\u7684\u6548\u80fd\uff0c\u6211\u5011\u8981\u662f\u6bcf\u6b21\u5b58\u53d6 ",(0,s.jsx)(n.code,{children:"t"})," \u503c\u5f8c\uff0c\u518d\u53bb\u904d\u6b77\u6574\u500b list \u53bb\u8a08\u7b97 ",(0,s.jsx)(n.code,{children:"[t-3000, t]"})," \u9019\u500b\u5340\u9593\u5167\u7684 request \u6578\u91cf\uff0c\u6548\u80fd\u6703\u5f88\u5dee\uff0c\u5728\u9
108f\u8f2f\u4e0a\u9054\u6210\u4e86\u984c\u76ee\u76ee\u6a19\u4f46\u9084\u662f\u6703\u904e\u4e0d\u4e86 leetcode \u7684\u6548\u80fd\u6e2c\u8a66\u3002",(0,s.jsx)(n.br,{}),"\n","\u90a3\u56e0\u70ba\u6bcf\u6b21\u50b3\u5165\u7684 ",(0,s.jsx)(n.code,{children:"t"})," \u503c\u90fd\u662f\u905e\u589e\u7684\uff0c\u6240\u4ee5\u7406\u8ad6\u6bcf\u6b21\u6aa2\u67e5\u7684\u6642\u9593\u5340\u9593 ",(0,s.jsx)(n.code,{children:"[t-3000, t]"})," \u4e5f\u662f\u905e\u589e\u7684\uff0c\u90a3\u5c0f\u65bc ",(0,s.jsx)(n.code,{children:"t-3000"})," \u7684 request \u5728\u672a\u4f86\u662f\u80af\u5b9a\u4e0d\u6703\u88ab\u8a08\u7b97\u5728\u5167\u7684\uff0c\u56e0\u6b64\u5c31\u53ef\u4ee5\u6bcf\u6b21\u6aa2\u67e5\u6642\u5c31\u628a\u5c0f\u65bc ",(0,s.jsx)(n.code,{children:"t-3000"})," \u7684 request \u5f9e\u5132\u5b58\u7d50\u69cb\u4e2d\u79fb\u9664\u3002"]}),"\n",(0,s.jsx)(n.p,{children:"\u56e0\u6b64\u57fa\u65bc\u4e0a\u9762\u7684\u4e09\u9805\u91cd\u9ede\uff1a"}),"\n",(0,s.jsxs)(n.ol,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"t"})," \u662f\u905e\u589e\u7684"]}),"\n",(0,s.jsxs)(n.li,{children:["\u6211\u5011\u9700\u8981\u5132\u5b58\u6bcf\u6b21\u8abf\u7528 ",(0,s.jsx)(n.code,{children:"ping"})," \u50b3\u5165\u7684 ",(0,s.jsx)(n.code,{children:"t"})," \u503c"]}),"\n",(0,s.jsxs)(n.li,{children:["\u6bcf\u6b21\u6aa2\u67e5\u6642\u79fb\u9664\u5c0f\u65bc ",(0,s.jsx)(n.code,{children:"t-3000"})," \u7684 ",(0,s.jsx)(n.code,{children:"t"})," \u503c"]}),"\n"]}),"\n",(0,s.jsxs)(n.p,{children:["\u9019\u4e9b\u91cd\u9ede\u76f8\u7576\u7b26\u5408 Queue \u7684 FIFO (First In First Out) \u7279\u6027\u3002",(0,s.jsx)(n.br,{}),"\n","\u7c21\u55ae\u4f86\u8aaa\uff0c\u6211\u5011\u6bcf\u6b21\u8abf\u7528 ",(0,s.jsx)(n.code,{children:"ping(t)"})," \u6642\u5c31\u628a ",(0,s.jsx)(n.code,{children:"t"})," \u503c\u653e\u9032 Queue \u88e1\uff0c\u6240\u4ee5 Queue \u88e1\u7684\u503c\u6703\u662f\u4f9d\u5e8f\u6162\u6162\u8b8a\u5927\u6392\u5217\u7684\u3002",(0,s.jsx)(n.br,{}),"\n","\u63a5\u4e0b\u4f86\u8abf\u7528 ",(0,s.jsx)(n.code,{children:"ping(t)"})," \u6642\uff0c\u6211\u5011\u5c31\u6aa2\u67e5 Queue \u7684\u6700\u524d\u7aef\u503c (\u4e5f\u5c31\u662f\u6700\u65e9\u653e\u9032\u53bb\u7684\u503c)\uff0c\u5982\u679c\u5b83\u5c0f\u65bc ",(0,s.jsx)(n.code,{children:"t-3000"}),"\uff0c\u90a3\u5c31\u628a\u5b83\u79fb\u9664\uff0c\u7136\u5f8c\u7e7c\u7e8c\u6aa2\u67e5\u4e0b\u4e00\u500b\u6700\u524d\u7aef\u503c\uff0c\u76f4\u5230\u6700\u524d\u7aef\u503c\u4e0d\u5c0f\u65bc ",(0,s.jsx)(n.code,{children:"t-3000"})," \u70ba\u6b62\u3002",(0,s.jsx)(n.br,{}),"\n","\u6700\u5f8c\u8fd4\u56de Queue \u88e1\u7684\u5143\u7d20\u6578\u91cf\u5c31\u662f\u6211\u5011\u8981\u7684\u7d50\u679c\u3002"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-java",children:"class RecentCounter {\n\n Queue<Integer> queue;\n\n public RecentCounter() {\n this.queue = new LinkedList<>();\n }\n\n public int ping(int t) {\n queue.offer(t);\n while (!queue.isEmpty() && queue.peek() < t - 3000) {\n queue.poll();\n }\n return queue.size();\n }\n}\n"})})]})}function a(e={}){const{wrapper:n}={...(0,i.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.