-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindSmallestLetterGreaterThanTheTarget.java
More file actions
31 lines (29 loc) · 1.12 KB
/
FindSmallestLetterGreaterThanTheTarget.java
File metadata and controls
31 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
Example 1:
Input: letters = ["c","f","j"], target = "a"
Output: "c"
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
*/
public class FindSmallestLetterGreaterThanTheTarget {
public static void main(String[] args) {
char[] letters={'c','f','j'};
char target='a';
char ans=nextGreatestLetter(letters,target);
System.out.println(ans);
}
public static char nextGreatestLetter(char[] letters, char target) {
int s=0;
int e=letters.length-1;
while(s<=e){
int mid=s+(e-s)/2;
if(letters[mid]>target){
e=mid-1;
}else{
s=mid+1;
}
}
return letters[s%letters.length];
}
}