Question
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:
Input: letters = ["c","f","j"], target = "a" Output: "c"
Example 2:
Input: letters = ["c","f","j"], target = "c" Output: "f"
Example 3:
Input: letters = ["c","f","j"], target = "d" Output: "f"
Constraints:
2 <= letters.length <= 104letters[i]is a lowercase English letter.lettersis sorted in non-decreasing order.letterscontains at least two different characters.targetis a lowercase English letter.
Python Solution
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if target>=letters[-1]:
return letters[0]
l=0
r=len(letters)-1
while l<r:
m = l + (r-l)//2
if letters[m]>target:
r = m
else:
l = m+1
return letters[l]

![[Solved] You are given an integer n and an integer start. Define an array nums where nums[i] = start + 2 * i (0-indexed) and n == nums.length. Return the bitwise XOR of all elements of nums.](https://machinelearningprojects.net/wp-content/uploads/2022/09/Leetcode-solutions-MLP-Feature-Image-1024x536.webp)