Smallest leter greater than target

Accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
// if the letter is bigger than the biggest in our sorted input then
// just pick the smallest one since letters "wrap around"
if (letters.back() <= target) return letters[0];

int low = 0;
int high = letters.size() - 1;
char res;

while (low <= high) {
int mid = low + (high - low)/2;
if (letters[mid] <= target) {
low = mid + 1;
} else {
res = letters[mid];
high = mid - 1;
}
}

return res;
}
};