744. Find Smallest Letter Greater Than Target
Problem description:
Given a characters array letters
that is sorted in non-decreasing order and a character target
, return the smallest character in the array that is larger than target
.
Note that the letters wrap around.
- For example, if
target == 'z'
andletters == ['a', 'b']
, the answer is'a'
.
Example 1:
1 | Input: letters = ["c","f","j"], target = "a" |
Example 2:
1 | Input: letters = ["c","f","j"], target = "c" |
Example 3:
1 | Input: letters = ["c","f","j"], target = "d" |
Example 4:
1 | Input: letters = ["c","f","j"], target = "g" |
Example 5:
1 | Input: letters = ["c","f","j"], target = "j" |
Constraints:
2 <= letters.length <= 104
letters[i]
is a lowercase English letter.letters
is sorted in non-decreasing order.letters
contains at least two different characters.target
is a lowercase English letter.
Solution:
1 | class Solution: |
time complexity: $O(logn)$
space complexity: $O()$
reference:
related problem: