2023年11月29日 星期三

[933] Number of Recent Calls

感覺歡樂的題目?!果然是easy等級,笑嗨嗨 XD
typedef struct {
int left;
int right;
int queue[10000];
} RecentCounter;

RecentCounter* recentCounterCreate() {
RecentCounter *calls = calloc (1, sizeof(RecentCounter));
calls->left= -1;
calls->right = -1;
return calls;
}
int recentCounterPing(RecentCounter* obj, int t) {
obj->queue[++(obj->right)]= t;
if (obj->left<0)
(obj->left)++;
else
while (obj->left < obj->right && obj->queue[obj->right] - obj->queue[obj->left] > 3000)
(obj->left)++;

return (obj->right - obj->left)+1;
}

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

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

沒有留言:

張貼留言