当前位置: 首页 > news >正文

LeetCode75——Day27

文章目录

    • 一、题目
    • 二、题解

一、题目

933. Number of Recent Calls

You have a RecentCounter class which counts the number of recent requests within a certain time frame.

Implement the RecentCounter class:

RecentCounter() Initializes the counter with zero recent requests.
int ping(int t) Adds a new request at time t, where 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 [t - 3000, t].
It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

Example 1:

Input
[“RecentCounter”, “ping”, “ping”, “ping”, “ping”]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]

Explanation
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3

Constraints:

1 <= t <= 109
Each test case will call ping with strictly increasing values of t.
At most 104 calls will be made to ping.

二、题解

class RecentCounter {
private:queue<int> q;
public:RecentCounter() {}int ping(int t) {q.push(t);while(q.front() < t - 3000) q.pop();return q.size();}
};/*** Your RecentCounter object will be instantiated and called as such:* RecentCounter* obj = new RecentCounter();* int param_1 = obj->ping(t);*/
http://www.lryc.cn/news/219551.html

相关文章:

  • P6462补刀
  • Python教程---Python交互界面
  • 基于计算机视觉的身份证识别系统 计算机竞赛
  • [python] logging输出到控制台(标准输出)
  • uniapp 离线打包 google 登录
  • 【实战Flask API项目指南】之一 概述
  • AD面试总结
  • 从今年最硬科幻游戏中的思考
  • Linux多值判断利用case...esac判断
  • 【教3妹学编程-算法题】重复的DNA序列
  • jetsonTX2 nx配置yolov5和D435I相机,完整步骤
  • RflySim | 滤波器设计实验一
  • 设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
  • 游戏中的随机抽样算法
  • 【Qt之QtXlsx模块】安装及使用
  • 如何在 TFRecord 文件上训练 Keras 模型实现黑色素瘤分类器
  • C++ 复制控制之复制构造函数
  • Windows ObjectType Hook 之 ParseProcedure
  • 下载树莓派对应的64位Ubuntu系统步骤
  • 网络运维Day03
  • LangChain+LLM实战---ChatGPT的工作原理
  • Appium知多少
  • 【实战Flask API项目指南】之五 RESTful API设计
  • 尚硅谷大数据项目《在线教育之实时数仓》笔记005
  • 算法通过村第十八关-回溯|青铜笔记|什么叫回溯(中篇)
  • generate by chatgpt:应用上线前的checkList(部分是我自己的回答)
  • Redis实战 | 使用Redis 的有序集合(Sorted Set)实现排行榜功能,和Spring Boot集成
  • 基于信号功率谱特征和GRNN广义回归神经网络的信号调制类型识别算法matlab仿真
  • matplotlib从起点出发(10)_Tutorial_10_Layout
  • HTTP头部信息解释分析(详细整理)(转载)