用二分法即可:
from typing import List
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if target < letters[0] or target >= letters[-1]:
return letters[0]
i = 0
j = len(letters) - 1
while i < j:
m = (i + j) // 2
if target < letters[m]:
j = m
elif target >= letters[m]:
i = m + 1
return letters[i]