I have a list of changes to a list - Adds and Deletes. The list could be huge - say 10'000 items.
I want to know the state of the list after change 9'000.
I could walk the list from the start all the way to change 9'000. That seems a bit long-winded to me.
I could keep a list of items and record when they're added and when they're deleted, then walk this list to see what is in the list at a particular change. If Adds and Deletes were equally likely, I would halve the number of list elements I would need to walk through...
But Big O notation says that halving the size of the problem doesn't make things more efficient (if I've understood it correctly).
I could cache the state of the list at every 100th or 1000th change... but again, big O says that dividing the number of items by 'n' doesn't make things more efficient.
So what is the efficient way of doing this? Is there an efficient way of doing this?
More details: Specifically, I'm tracking memory allocations / deallocations in a custom allocater. Each allocation / deallocation is an event in the list. Each allocation has a unique id. I'd like to know what is currently allocated after (e.g) 9'000 events.
My first idea was to store, for each id, the event it was allocated and the event it was deallocated. Then to walk this list up to the first allocation whose alloc event is greater than 9000. But like I said, this would only halve the number of items that I'd need to walk through.
I like the point made by Mike F - walking from the nearest 100th item is constant time...