-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
53 lines (48 loc) · 2.02 KB
/
Solution.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package BINARYSEARCH.smallestlettergreaterthantarget.attempt;
/**
* 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.
* <p>
* 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.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* Input: letters = ["c","f","j"], target = "a"
* Output: "c"
* Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
* Example 2:
* <p>
* Input: letters = ["c","f","j"], target = "c"
* Output: "f"
* Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
* Example 3:
* <p>
* Input: letters = ["x","x","y","y"], target = "z"
* Output: "x"
* Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
*/
class Solution {
static char nextGreatestLetter(char[] letters, char target) {
int pos = binarySearch(letters, target);
return letters[pos];
}
static int binarySearch(char[] letters, char target) {
int start = 0, end = letters.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
// even if target found - search in right half for next greater element
if (target >= letters[mid]) start = mid + 1;
else if (target < letters[mid]) end = mid - 1;
}
return start % letters.length;
}
public static void main(String[] args) {
char[] letters = {'x', 'x', 'y', 'y'};
char target = 'z';
System.out.println("Next greatest letter: " + nextGreatestLetter(letters, target));
letters = new char[]{'c', 'f', 'j'};
System.out.println("Next greatest letter: " + nextGreatestLetter(letters, 'c'));
System.out.println("Next greatest letter: " + nextGreatestLetter(letters, 'a'));
}
}