summaryrefslogtreecommitdiff
path: root/datastructures/firstUnused.cpp
blob: 141eb0059136194815fde7d0282c731ad5eca0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
// Erste natürliche Zahl nicht im set used.
set<int> used;
int unusedCounter = 1;

int getFirstUnused() { // Laufzeit: O(log n) amortisiert.
  auto it = used.lower_bound(unusedCounter);
  while (it != used.end() && *it == unusedCounter) {
    it++;
    unusedCounter++;
  }
  return unusedCounter++; // Evtl. neuen Wert noch hinzufügen.
}