【leetcode】 最近的请求次数

 

typedef struct {
    int arr[10001];
    int n;
    int start;
} RecentCounter;

RecentCounter* recentCounterCreate() {
    RecentCounter* obj = (RecentCounter*)calloc(sizeof(RecentCounter),1);
    obj->n = obj->start = 0;
    return obj;
}

int recentCounterPing(RecentCounter* obj, int t) {
    obj->arr[obj->n++] = t;
    for (int i=obj->start; i<obj->n; i++)
    {
        if (obj->arr[i] >= t-3000) 
        {
            obj->start = i;
            return obj->n-i;
        }
    }
    return 0;
}

void recentCounterFree(RecentCounter* obj) {
    free(obj);
}

 

posted @ 2020-09-14 10:48  温暖了寂寞  阅读(152)  评论(0编辑  收藏  举报