-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextGreatestLetter.java
53 lines (36 loc) · 1.13 KB
/
NextGreatestLetter.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 com.vinay.practice.lc;
// https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/
public class NextGreatestLetter {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] arr = {'c', 'f', 'j'};
char target = 'g';
System.out.println("The smallest letter greater than target: " + nextGreatestLetter(arr, target));
}
public static char nextGreatestLetter(char[] letters, char target) {
// Brute Force
/*
for(int i = 0 ; i <= letters.length-1 ; i++) {
if(letters[i] > target ) {
return letters[i];
}
}
return letters[0];
*/
// BinarySearch
int start = 0;
int end = letters.length-1;
while(start <= end) {
System.out.println("s");
int mid = start + (end-start) / 2;
if(target < letters[mid]) {
end = mid-1;
} else {
start = mid+1;
}
}
// start would have given us the required char but if in case, the target is not found, then we have to return 1st char.
// To handle return of 1st char, we use mod by length which will give 0. Hence, arr[0]
return letters[start % letters.length];
}
}