|
2 | 2 |
|
3 | 3 | [https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/) |
4 | 4 |
|
5 | | -Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target. |
| 5 | +Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target. |
6 | 6 |
|
7 | 7 | Letters also wrap around. For example, if the target is target = 'z' and letters = \['a', 'b'\], the answer is 'a'. |
8 | 8 |
|
@@ -40,23 +40,59 @@ target = "k" |
40 | 40 | Output: "c" |
41 | 41 | ``` |
42 | 42 |
|
43 | | -**Note:** |
| 43 | +**Note: |
| 44 | +** |
44 | 45 |
|
45 | | -1. letters has a length in range \[2, 10000\]. |
46 | | -2. letters consists of lowercase letters, and contains at least 2 unique letters. |
| 46 | +1. letters has a length in range \[2, 10000\]. |
| 47 | +2. letters consists of lowercase letters, and contains at least 2 unique letters. |
47 | 48 | 3. target is a lowercase letter. |
48 | 49 |
|
49 | 50 | ### Thought Process {#thought-process} |
50 | 51 |
|
51 | | -1. Binary Search |
| 52 | +1. Linear Scan |
| 53 | + 1. Scan from left to right, at anytime we see a character greater than our target, we can return immediately |
| 54 | + 2. Else, we can return the first letter |
| 55 | + 3. Time complexity O\(n\) |
| 56 | + 4. Space complexity O\(1\) |
| 57 | +2. Modified Binary Search |
| 58 | + 1. We can implement our own binary search, using lo and hi to narrow our search by half every time |
| 59 | + 2. When the letter\[mi\] <= target, we set lo = mi + 1, where mi = \(lo + hi\) / 2 |
| 60 | + 3. Else hi = mi |
| 61 | + 4. Time complexity O\(logn\) |
| 62 | + 5. Space complexity O\(1\) |
| 63 | +3. Binary Search |
52 | 64 | 1. We add 1 to target and mod 26 to get the "right" target |
53 | 65 | 2. If the index is equal to the length of list, we return the first element |
54 | 66 | 3. Time complexity O\(logn\) |
55 | 67 | 4. Space complexity O\(1\) |
56 | | -2. asd |
57 | 68 |
|
58 | 69 | ### Solution |
59 | 70 |
|
| 71 | +```java |
| 72 | +class Solution { |
| 73 | + public char nextGreatestLetter(char[] letters, char target) { |
| 74 | + for (char c : letters) { |
| 75 | + if (c > target) return c; |
| 76 | + } |
| 77 | + return letters[0]; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +```java |
| 83 | +class Solution { |
| 84 | + public char nextGreatestLetter(char[] letters, char target) { |
| 85 | + int lo = 0, hi = letters.length; |
| 86 | + while (lo < hi) { |
| 87 | + int mi = lo + (hi - lo) / 2; |
| 88 | + if (letters[mi] <= target) lo = mi + 1; |
| 89 | + else hi = mi; |
| 90 | + } |
| 91 | + return letters[lo % letters.length]; |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
60 | 96 | ```java |
61 | 97 | class Solution { |
62 | 98 | public char nextGreatestLetter(char[] letters, char target) { |
|
0 commit comments