Select a random number from stream, with O(1) space | GeeksforGeeks 1) Initialize ‘count’ as 0, ‘count...

Please Visit: http://ift.tt/1ajReyV



Select a random number from stream, with O(1) space | GeeksforGeeks

1) Initialize ‘count’ as 0, ‘count’ is used to store count of numbers seen so far in stream.

2) For each number ‘x’ from stream, do following

…..a) Increment ‘count’ by 1.

…..b) If count is 1, set result as x, and return result.

…..c) Generate a random number from 0 to ‘count-1′. Let the generated random number be i.

…..d) If i is equal to ‘count – 1′, update the result as x.

http://ift.tt/1rVrODY

http://ift.tt/1qdEx1D



Select a random number from stream, with O(1) space | GeeksforGeeks







from Public RSS-Feed of Jeffery yuan. Created with the PIXELMECHANICS 'GPlusRSS-Webtool' at http://gplusrss.com http://ift.tt/1rVrOE2

via LifeLong Community

No comments:

Post a Comment