[
binary search
]
Leetcode 0744 Find Smallest Letter Greater Than Target
Problem statement
https://leetcode.com/problems/find-smallest-letter-greater-than-target/
Solution
Just perform classical binary search and if we found that answer is equal to n, than we return 0.
Complexity
Time complexity is O(log n), space is O(1).
Code
class Solution:
def nextGreatestLetter(self, letters, target):
return letters[bisect.bisect(letters, target) % len(letters)]