国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

LeetCode //C - 933. Number of Recent Calls

這篇具有很好參考價(jià)值的文章主要介紹了LeetCode //C - 933. Number of Recent Calls。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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文章來(lái)源地址http://www.zghlxwxcb.cn/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);
 */

到了這里,關(guān)于LeetCode //C - 933. Number of Recent Calls的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 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日
    瀏覽(53)
  • Leetcode 3016. Minimum Number of Pushes to Type Word II

    Leetcode 3016. Minimum Number of Pushes to Type Word II 1. 解題思路 2. 代碼實(shí)現(xiàn) 題目鏈接:3016. Minimum Number of Pushes to Type Word II 這道題的話思路其實(shí)還是蠻簡(jiǎn)單的,顯然我們的目的是要令對(duì)給定的word在鍵盤上敲擊的次數(shù)最小。 因此,我們只需要對(duì)單詞當(dāng)中按照字符的頻次進(jìn)行倒序排列,

    2024年01月22日
    瀏覽(19)
  • LeetCode 2475. Number of Unequal Triplets in Array【數(shù)組,排序,哈希表】簡(jiǎn)單

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無(wú)鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2024年02月09日
    瀏覽(22)
  • leetcode - 1751. Maximum Number of Events That Can Be Attended II

    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日
    瀏覽(23)
  • leetcode - 1326. Minimum Number of Taps to Open to Water a Garden

    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日
    瀏覽(20)
  • 【LeetCode75】第二十七題(933)最近的請(qǐng)求次數(shù)

    【LeetCode75】第二十七題(933)最近的請(qǐng)求次數(shù)

    目錄 題目: 示例: 分析: 代碼+運(yùn)行結(jié)果: 首先這是LeetCode75里第一道設(shè)計(jì)類的題目,這種類型的題目會(huì)比較新穎,就是按照題目要求來(lái)設(shè)計(jì)一個(gè)類。然后測(cè)試用例是模擬真實(shí)調(diào)用類的成員函數(shù)的。 這道題也算是簡(jiǎn)單題,整個(gè)類除了構(gòu)造函數(shù)以外就一個(gè)成員函數(shù),測(cè)試用例

    2024年02月13日
    瀏覽(34)
  • 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. 代碼實(shí)現(xiàn) 題目鏈接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 這一題我的思路上就是一個(gè)二分的思路,先確定一個(gè)上下界,然后不斷通過(guò)二分來(lái)找到最大的price不超過(guò)k的值。 因此,剩下的

    2024年01月20日
    瀏覽(24)
  • 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日
    瀏覽(16)
  • 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日
    瀏覽(24)
  • 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日
    瀏覽(18)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包