All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 01, 2024
Last updated : July 01, 2024
Related Topics : Array, Binary Search
Acceptance Rate : 53.36 %
class Solution {
public char nextGreatestLetter(char[] letters, char target) {
int left = 0;
int right = letters.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (letters[mid] <= target) {
left = mid + 1;
continue;
}
right = mid - 1;
}
if (letters[left] <= target)
return letters[(left + 1) % letters.length];
return letters[left];
}
}
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
left, right = 0, len(letters) - 1
while left < right :
mid = (left + right) // 2
if ord(letters[mid]) <= ord(target) :
left = mid + 1
continue
right = mid - 1
if (ord(letters[left]) <= ord(target)) :
return letters[(left + 1) % len(letters)]
return letters[left]