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

int get_first_unused() { // Laufzeit: O(log n) amortisiert.
	auto it = used.lower_bound(unusedCounter);
	while (it != used.end() && *it == unusedCounter) {
		it++;
		unusedCounter++;
	}
	used.insert(unusedCounter);
	return unusedCounter++;
}