LeetCode //C - 933. Number of Recent Calls

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 933. Number of Recent Calls。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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 < = 1 0 9 1 <= t <= 10^9 1<=t<=109
  • Each test case will call ping with strictly increasing values of t.
  • At most 1 0 4 10^4 104 calls will be made to ping.

From: LeetCode
Link: 933. Number of Recent Calls文章来源地址https://www.toymoban.com/news/detail-818893.html


Solution:

Ideas:
  • The RecentCounter is essentially maintaining a dynamic list of timestamps representing request times.
  • When a new request comes in (signified by calling recentCounterPing with a timestamp), it’s added to this list.
  • The recentCounterPing function then counts how many of these stored requests fall within the last 3000 milliseconds (inclusive of the new request).
  • This count is returned as the number of recent requests.
Code:
typedef struct {
    int *requests;    // Array to store the timestamps of requests
    int size;         // Current number of requests stored
    int capacity;     // Total capacity of the array
} RecentCounter;

RecentCounter* recentCounterCreate() {
    RecentCounter* obj = (RecentCounter*) malloc(sizeof(RecentCounter));
    obj->capacity = 10; // Initial capacity
    obj->size = 0;
    obj->requests = (int*) malloc(obj->capacity * sizeof(int));
    return obj;
}

// Helper function to resize the array if needed
void resizeIfNeeded(RecentCounter* obj) {
    if (obj->size >= obj->capacity) {
        obj->capacity *= 2;
        obj->requests = (int*) realloc(obj->requests, obj->capacity * sizeof(int));
    }
}

int recentCounterPing(RecentCounter* obj, int t) {
    // Resize the array if needed
    resizeIfNeeded(obj);

    // Add the new request
    obj->requests[obj->size++] = t;

    // Calculate the minimum acceptable time
    int minTime = t - 3000;

    // Count requests within the time frame
    int count = 0;
    for (int i = 0; i < obj->size; ++i) {
        if (obj->requests[i] >= minTime) {
            ++count;
        }
    }

    return count;
}

void recentCounterFree(RecentCounter* obj) {
    free(obj->requests);
    free(obj);
}

/**
 * Your RecentCounter struct will be instantiated and called as such:
 * RecentCounter* obj = recentCounterCreate();
 * int param_1 = recentCounterPing(obj, t);
 * recentCounterFree(obj);
 */

到了这里,关于LeetCode //C - 933. Number of Recent Calls的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 452. Minimum Number of Arrows to Burst Balloons

    There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [ x s t a r t , x e n d x_{start}, x_{end} x s t a r t ​ , x e n d ​ ] denotes a balloon whose horizontal diameter stretches between x s t a r t x_{start} x s t a r t ​ and x e n d x_{end} x

    2024年02月12日
    浏览(46)
  • Leetcode 3016. Minimum Number of Pushes to Type Word II

    Leetcode 3016. Minimum Number of Pushes to Type Word II 1. 解题思路 2. 代码实现 题目链接:3016. Minimum Number of Pushes to Type Word II 这道题的话思路其实还是蛮简单的,显然我们的目的是要令对给定的word在键盘上敲击的次数最小。 因此,我们只需要对单词当中按照字符的频次进行倒序排列,

    2024年01月22日
    浏览(50)
  • LeetCode 2475. Number of Unequal Triplets in Array【数组,排序,哈希表】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(46)
  • leetcode - 1751. Maximum Number of Events That Can Be Attended II

    You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend. You can only attend one event at a time. If you choose to attend

    2024年02月16日
    浏览(78)
  • leetcode - 1326. Minimum Number of Taps to Open to Water a Garden

    There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, …, n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i

    2024年02月10日
    浏览(40)
  • 【LeetCode75】第二十七题(933)最近的请求次数

    目录 题目: 示例: 分析: 代码+运行结果: 首先这是LeetCode75里第一道设计类的题目,这种类型的题目会比较新颖,就是按照题目要求来设计一个类。然后测试用例是模拟真实调用类的成员函数的。 这道题也算是简单题,整个类除了构造函数以外就一个成员函数,测试用例

    2024年02月13日
    浏览(44)
  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解题思路 2. 代码实现 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。 因此,剩下的

    2024年01月20日
    浏览(49)
  • Leetcode 268. Missing Number

    Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Sum all the numbers as x x x and use n ( n + 1 ) 2 − x frac{n(n+1)}{2} - x 2 n ( n + 1 ) ​ − x .

    2024年02月14日
    浏览(42)
  • leetcode - 260. Single Number III

    Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. Example 1: Example 2: Example 3: Constraints: U

    2024年02月11日
    浏览(47)
  • LeetCode287. Find the Duplicate Number

    Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space. Example 1: Input: nums = [1,3,4,2,2] Output: 2 Example 2: Input: nums = [3,1,3,4,

    2024年01月20日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包