-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
38 lines (34 loc) · 1.04 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
class Solution {
public char nextGreatestLetter(char[] letters, char target) {
// //Linear Search
// boolean[] seen = new boolean[26];
// for (char c: letters)
// seen[c - 'a'] = true;
// while (true) {
// //Because we need char greater than target
// target++;
// //Wrap up around a-z
// if (target > 'z')
// target = 'a';
// //Found in letters
// if (seen[target - 'a'])
// return target;
// }
//Linear Scan
// for (char c: letters)
// if (c > target)
// return c;
// return letters[0];
//Binary Search
int l = 0, r = letters.length;
while(l < r){
int mid = l + (r - l) / 2;
if(letters[mid] <= target){
l = mid + 1;
} else {
r = mid;
}
}
return letters[l % letters.length];
}
}