1+ package  com .leetcode .arrays .binarysearch ;
2+ 
3+ import  static  org .junit .jupiter .api .Assertions .assertEquals ;
4+ 
5+ /** 
6+  * Level: Easy 
7+  * Link: https://leetcode.com/problems/find-smallest-letter-greater-than-target/ 
8+  * Description: 
9+  * Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find 
10+  * the smallest element in the list that is larger than the given target. 
11+  * 
12+  * Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'. 
13+  * 
14+  * Examples: 
15+  * 
16+  * Input: 
17+  * letters = ["c", "f", "j"] 
18+  * target = "a" 
19+  * Output: "c" 
20+  * 
21+  * Input: 
22+  * letters = ["c", "f", "j"] 
23+  * target = "c" 
24+  * Output: "f" 
25+  * 
26+  * Input: 
27+  * letters = ["c", "f", "j"] 
28+  * target = "d" 
29+  * Output: "f" 
30+  * 
31+  * Input: 
32+  * letters = ["c", "f", "j"] 
33+  * target = "g" 
34+  * Output: "j" 
35+  * 
36+  * Input: 
37+  * letters = ["c", "f", "j"] 
38+  * target = "j" 
39+  * Output: "c" 
40+  * 
41+  * Input: 
42+  * letters = ["c", "f", "j"] 
43+  * target = "k" 
44+  * Output: "c" 
45+  * 
46+  * Note: 
47+  * - letters has a length in range [2, 10000]. 
48+  * - letters consists of lowercase letters, and contains at least 2 unique letters. 
49+  * - target is a lowercase letter. 
50+  * 
51+  * @author rampatra 
52+  * @since 2019-08-19 
53+  */ 
54+ public  class  SmallestLetterGreaterThanTarget  {
55+ 
56+     /** 
57+      * Runtime: <a href="https://leetcode.com/submissions/detail/253061487/">0 ms</a>. 
58+      * 
59+      * @param letters 
60+      * @param target 
61+      * @return 
62+      */ 
63+     public  static  char  nextGreatestLetter (char [] letters , char  target ) {
64+         int  low  = 0 , hi  = letters .length  - 1 ;
65+         while  (low  <= hi ) {
66+             int  mid  = low  + (hi  - low ) / 2 ;
67+             if  (letters [mid ] <= target ) {
68+                 low  = mid  + 1 ;
69+             } else  {
70+                 hi  = mid  - 1 ;
71+             }
72+         }
73+         return  letters [low  % letters .length ];
74+     }
75+ 
76+     public  static  void  main (String [] args ) {
77+         assertEquals ('a' , nextGreatestLetter (new  char []{'a' }, 'z' ));
78+         assertEquals ('b' , nextGreatestLetter (new  char []{'a' , 'b' }, 'a' ));
79+         assertEquals ('b' , nextGreatestLetter (new  char []{'a' , 'b' , 'c' }, 'a' ));
80+         assertEquals ('a' , nextGreatestLetter (new  char []{'a' , 'b' , 'c' }, 'z' ));
81+         assertEquals ('c' , nextGreatestLetter (new  char []{'c' , 'f' , 'j' }, 'a' ));
82+         assertEquals ('f' , nextGreatestLetter (new  char []{'c' , 'f' , 'j' }, 'c' ));
83+         assertEquals ('f' , nextGreatestLetter (new  char []{'c' , 'f' , 'j' }, 'd' ));
84+         assertEquals ('b' , nextGreatestLetter (new  char []{'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' ,
85+                 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' }, 'a' ));
86+     }
87+ }
0 commit comments