algorithm - Integer assignment in range -
i'm looking algorithm assigns , releases integers interval [a,b].
every time request assignment, algorithm produces integer in [a,b] range has not been assigned before. may free integers had been assigned asking algorithm release them. doing makes them available assignment again.
to summarise:
- after integer assigned, cannot assigned again until released
- if 1 or more integers not assigned, algorithm must assign 1 of them
- if , if integers in range assigned, algorithm assigns none
it desired both time , space complexity of algorithm sub-linear (for n = b - a).
as bonus requirement, assume specific integers in [a,b] required assigned.
the first thing comes mind start single "available" range. when user assigns integer, remove range:
[a,b] <--- [a+1,b]
this o(1) operation in cases. due way return step works, means maximum number assigned when no other number available, , low numbers reused possible.
when user returns integer, binary search figure out range either goes before, or merges into:
[28][30-50] <---- user going release 29 [28-50] -or- [26][30-50] <----- user going release 28 [26][28][30-50]
this o(log m) operation m number of ranges, small. starts @ 1, can @ n/2
depending on numbers user releases.
if want tricky, can add second ordering on ranges, moves smaller ranges closer toward "front", , larger ranges closer "end", , when user makes number "available", give them first value in "smallest" range, heuristically lead smaller sets being "consumed", leaving fewer sets total, saving space on average. however, adds lot of complexity, , small amount of space , time, worst cases worse. measure first before trying this.
it's worth mentioning does use linear space, though i'm pretty sure constant <=1, memory overhead less of storing of numbers. it's hard calculate average without making wild assumptions how user assigns , releases integers, i'm pretty sure average memory usage closer logarithmic linear. hard tell though, might optimism.
some dynamic memory dispatches work on similar concepts this, storing "unused" parts linked list inside unused memory. way, there's no additional overhead space required. since you're dealing integer ranges rather heap memory, optimization doesn't you.
Comments
Post a Comment