【在 a**********u 的大作中提到】 : 傻逼。这个“同文同种的 Chinese PHD”品德高尚,挣钱大大地多;而这个“中产阶级 : 出来 的白人男”满身狐臭,还要玩3p,大骂你老母干涉他隐私,很多很多。 : 所以说你提这么一个前提来判断,本身就是个二。 : : 是和自己同文同种的 Chinese PHD,还是一个这里中产阶级出来 的白人男?
p*2
44 楼
瞎写了一个。 final int SIZE=60*60; //seconds in one hour int[] arr=new int[SIZE]; int hour=0; int minute=0; long last=0;
long currSecond(){ return System.currentTimeMillis()/1000; }
long clear(){ long curr=currSecond(); if(curr>last){ if(curr-last>=SIZE){ hour=0; minute=0; Arrays.fill(arr,0); } else{ if(curr-last>=60){ minute=0; } else{ for(long i=last-60+1;i<=curr-60;i++){ minute-=arr[(int)(i%SIZE)]; } }
for(long i=last+1;i<=curr;i++){ int p=(int)(i%SIZE); hour-=arr[p]; arr[p]=0; } } last=curr; }
return curr; }
void request(){ long curr=clear(); arr[(int)(curr%SIZE)]++; hour++; minute++; }
int lastSecond(){ long curr=clear(); return arr[(int)(curr%SIZE)]; }
int lastMinute(){ clear(); return minute; }
int lastHour(){ clear(); return hour; }
【在 z*********8 的大作中提到】 : 二爷能具体讲讲吗?
Q*7
45 楼
只要是人就会打屁打嗝。只要不在公众场合做不文雅的事就行。
r*e
46 楼
膜拜
【在 p*****2 的大作中提到】 : : 瞎写了一个。 : final int SIZE=60*60; //seconds in one hour : int[] arr=new int[SIZE]; : int hour=0; : int minute=0; : long last=0; : : long currSecond(){ : return System.currentTimeMillis()/1000;
Only need 2 60 element array. Just record request count for last 60 seconds and counts for last 60 mins. Update them accordingly based on the count for each new seconds.
【在 a*******r 的大作中提到】 : Only need 2 60 element array. Just record request count for last 60 seconds : and counts for last 60 mins. Update them accordingly based on the count for : each new seconds.
c*a
51 楼
丫用的b+ tree,maintain很复杂,说丫的45分钟光讲道理,没写code。
【在 p*****2 的大作中提到】 : : 瞎写了一个。 : final int SIZE=60*60; //seconds in one hour : int[] arr=new int[SIZE]; : int hour=0; : int minute=0; : long last=0; : : long currSecond(){ : return System.currentTimeMillis()/1000;
I have one question regarding this problem: what does "the last second" mean? It could mean 1 second before the current time till now, it could also mean the last second slot (the most recent time round to seconds minus the second most recent time round to seconds). The first case (1s before current time till now) is more real time, and in this case we can use a vector (or deque or any container that can be dynamically expanded or shrinked with random accesses) to store the time of incoming requests/events. Maintain the size of the vector. When new request comes, insert the time into the vector and update the size. Periodically (e. g., 1 minute) delete the outdated entries from the beginning of the vector ( oldest events, we can safely delete the events before current time-1 day). Or the delete can be performed upon every insertion (if we do this the amortized time for insertion is still O(1 ) if requests are coming at a stable rate). Now to get the last minute events, we can calculate the current time-60 seconds and use that time to do a binary search. This takes O(lgn) where n is the number of events in the vector. For the second case (time slot) we can use an circular array with a size of 60*60 if we want to retrieve the number of events upto 1 hour ago, or a size of 60*60*24 if we want to go back upto 1 day ago. In addition to the circular array, we use six variables, counterHour, counterMin and counterSec (and their prior slot versions) to keep track of the number of events respectively. Upon a new event, we increment the 3 counters for the current hour, min and sec slots. When we hit an even second, we copy counterSec to counterSecPrev and clear counterSec. We do similar things upon even minute and hour. Now to get the last minute events for example, we can just return counterMinPrev in O(1). Actually we don't need the circular array in this case. We only need that 6 variables. For thread safety, we need lock the variable before updating it.
x*0
79 楼
mark
c*a
80 楼
赞思路,赞问的第一个问题。一定会很得面试官心。 circular还是要的。不然59分钟之后一怎么evict第一分钟那个已经out of bound的呢。
also the of request e. (
【在 a**********e 的大作中提到】 : I have one question regarding this problem: what does "the last second" : mean? It could mean 1 second before the current time till now, it could also : mean the last second slot (the most recent time round to seconds minus the : second most recent time round to seconds). : The first case (1s before current time till now) is more real time, and in : this case we can use a vector (or deque or any container that can be : dynamically expanded or shrinked with random accesses) to store the time of : incoming requests/events. Maintain the size of the vector. When new request : comes, insert the time into the vector and update the size. Periodically (e. : g., 1 minute) delete the outdated entries from the beginning of the vector (
如果我理解题目没错,他只需要你最后一秒一分钟和一小时的request的数。不明白为 什么要用到Array. 应该只需要三个counter就足够了。这个code有什么问题吗?(除了 不支持并发) long startTime = new Date().getTime(); int lastSecondRequests = 0; long lastMinuteRequests = 0; long lastHourRequests = 0; long whichSecond = 0; long whichMinute = 0; void requestHandler(HttpRequest request) { long currentTime = new Date().getTime();
没太明白用list怎么计数。是说node里面存当前1秒的数量么?然后用一个double list ,如果要计一小时,就往前数3600个node? 我在想能不能用list每个node存timestamp,然后用map来存timestamp-id的mapping, 然后两个id一减。当然,其实这个就是前面有人提到的bst了。
瞎写了一个。 final int SIZE=60*60; //seconds in one hour int[] arr=new int[SIZE]; int hour=0; int minute=0; long last=0;
long currSecond(){ return System.currentTimeMillis()/1000; }
long clear(){ long curr=currSecond(); if(curr>last){ if(curr-last>=SIZE){ hour=0; minute=0; Arrays.fill(arr,0); } else{ if(curr-last>=60){ minute=0; } else{ for(long i=last-60+1;i<=curr-60;i++){ minute-=arr[(int)(i%SIZE)]; } }
for(long i=last+1;i<=curr;i++){ int p=(int)(i%SIZE); hour-=arr[p]; arr[p]=0; } } last=curr; }
return curr; }
void request(){ long curr=clear(); arr[(int)(curr%SIZE)]++; hour++; minute++; }
int lastSecond(){ long curr=clear(); return arr[(int)(curr%SIZE)]; }
int lastMinute(){ clear(); return minute; }
int lastHour(){ clear(); return hour; }
【在 z*********8 的大作中提到】 : 二爷能具体讲讲吗?
r*e
107 楼
膜拜
【在 p*****2 的大作中提到】 : : 瞎写了一个。 : final int SIZE=60*60; //seconds in one hour : int[] arr=new int[SIZE]; : int hour=0; : int minute=0; : long last=0; : : long currSecond(){ : return System.currentTimeMillis()/1000;
a*r
108 楼
Only need 2 60 element array. Just record request count for last 60 seconds and counts for last 60 mins. Update them accordingly based on the count for each new seconds.
【在 a*******r 的大作中提到】 : Only need 2 60 element array. Just record request count for last 60 seconds : and counts for last 60 mins. Update them accordingly based on the count for : each new seconds.
c*a
110 楼
丫用的b+ tree,maintain很复杂,说丫的45分钟光讲道理,没写code。
【在 p*****2 的大作中提到】 : : 瞎写了一个。 : final int SIZE=60*60; //seconds in one hour : int[] arr=new int[SIZE]; : int hour=0; : int minute=0; : long last=0; : : long currSecond(){ : return System.currentTimeMillis()/1000;
I have one question regarding this problem: what does "the last second" mean? It could mean 1 second before the current time till now, it could also mean the last second slot (the most recent time round to seconds minus the second most recent time round to seconds). The first case (1s before current time till now) is more real time, and in this case we can use a vector (or deque or any container that can be dynamically expanded or shrinked with random accesses) to store the time of incoming requests/events. Maintain the size of the vector. When new request comes, insert the time into the vector and update the size. Periodically (e. g., 1 minute) delete the outdated entries from the beginning of the vector ( oldest events, we can safely delete the events before current time-1 day). Or the delete can be performed upon every insertion (if we do this the amortized time for insertion is still O(1 ) if requests are coming at a stable rate). Now to get the last minute events, we can calculate the current time-60 seconds and use that time to do a binary search. This takes O(lgn) where n is the number of events in the vector. For the second case (time slot) we can use an circular array with a size of 60*60 if we want to retrieve the number of events upto 1 hour ago, or a size of 60*60*24 if we want to go back upto 1 day ago. In addition to the circular array, we use six variables, counterHour, counterMin and counterSec (and their prior slot versions) to keep track of the number of events respectively. Upon a new event, we increment the 3 counters for the current hour, min and sec slots. When we hit an even second, we copy counterSec to counterSecPrev and clear counterSec. We do similar things upon even minute and hour. Now to get the last minute events for example, we can just return counterMinPrev in O(1). Actually we don't need the circular array in this case. We only need that 6 variables. For thread safety, we need lock the variable before updating it.
x*0
138 楼
mark
c*a
139 楼
赞思路,赞问的第一个问题。一定会很得面试官心。 circular还是要的。不然59分钟之后一怎么evict第一分钟那个已经out of bound的呢。
also the of request e. (
【在 a**********e 的大作中提到】 : I have one question regarding this problem: what does "the last second" : mean? It could mean 1 second before the current time till now, it could also : mean the last second slot (the most recent time round to seconds minus the : second most recent time round to seconds). : The first case (1s before current time till now) is more real time, and in : this case we can use a vector (or deque or any container that can be : dynamically expanded or shrinked with random accesses) to store the time of : incoming requests/events. Maintain the size of the vector. When new request : comes, insert the time into the vector and update the size. Periodically (e. : g., 1 minute) delete the outdated entries from the beginning of the vector (
如果我理解题目没错,他只需要你最后一秒一分钟和一小时的request的数。不明白为 什么要用到Array. 应该只需要三个counter就足够了。这个code有什么问题吗?(除了 不支持并发) long startTime = new Date().getTime(); int lastSecondRequests = 0; long lastMinuteRequests = 0; long lastHourRequests = 0; long whichSecond = 0; long whichMinute = 0; void requestHandler(HttpRequest request) { long currentTime = new Date().getTime();
没太明白用list怎么计数。是说node里面存当前1秒的数量么?然后用一个double list ,如果要计一小时,就往前数3600个node? 我在想能不能用list每个node存timestamp,然后用map来存timestamp-id的mapping, 然后两个id一减。当然,其实这个就是前面有人提到的bst了。
g*g
145 楼
两部分: 1 建立counter for every second 可以用循环数组 2 建立 window (5分钟,1 小时,等等) 这个问题就是一个固定window 滑动的问题了。 可以在 O(1) 返回结果
g*g
146 楼
两部分: 1 建立counter for every second 可以用循环数组 2 建立 window (5分钟,1 小时,等等) 这个问题就是一个固定window 滑动的问题了。 可以在 O(1) 返回结果
g*g
147 楼
Second this, if you need sub-second accuracy. You can record each request with the timestamp, retrieve the head and tail second records for adjustment. You'll need a linked list, plus a 3600 second indexing rotational array for data structure, each array entry would point to the first request after a round second. Build another index for minute or whatever period.
【在 g*****g 的大作中提到】 : 两部分: : 1 建立counter for every second : 可以用循环数组 : 2 建立 window (5分钟,1 小时,等等) : 这个问题就是一个固定window 滑动的问题了。 : 可以在 O(1) 返回结果