-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem no. 744.java
34 lines (28 loc) · 1.06 KB
/
Problem no. 744.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
// Ques: 744. Find Smallest Letter Greater Than Target
// Ques link - https://leetcode.com/problems/find-smallest-letter-greater-than-target/
package com.nitin;
public class SmallestLetter {
public static void main(String[] args) {
char[] letters = {'c','f','j'};
char target = 'a';
char ans = ceiling(letters, target);
System.out.println(ans);
}
// return the index of the smallest no > target
static char ceiling(char[] letters, char target){
// but what if the target is greater than the greatest number in the array
int start = 0;
int end = letters.length -1;
while(start <= end){
// find the middle element
// int mid = (start + end) / 2; // might be possible that the (start + end) exceeds the range of int in java
int mid = start + (end - start) / 2;
if (target < letters[mid]){
end = mid - 1;
} else {
start = mid + 1;
}
}
return letters[start % letters.length];
}
}