Find Smallest Letter Greater Than Target - LeetCode
letters[0] if none exists==. Fold it into the < branch== means the answer is still to the rightleft converges to the first element > targetchar nextGreatestLetter(vector<char>& letters, char target) {
int left = 0, right = letters.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (letters[mid] <= target) { // == and < both push right
left = mid + 1;
} else {
right = mid - 1;
}
}
return letters[left % letters.size()];
}
left lands at letters.size()% letters.size() brings it back to 0letters[mid + 1] on an == match["x","x","y","y"] with target "y", since mid + 1 is also y