Skip to content

Commit 015f1e6

Browse files
committed
Updates leetcode/binary-search/744-find-smallest-letter-greater-than-target.md
Auto commit by GitBook Editor
1 parent 393efa0 commit 015f1e6

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

leetcode/binary-search/744-find-smallest-letter-greater-than-target.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/](https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/)
44

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.
66

77
Letters also wrap around. For example, if the target is target = 'z' and letters = \['a', 'b'\], the answer is 'a'.
88

@@ -40,23 +40,59 @@ target = "k"
4040
Output: "c"
4141
```
4242

43-
**Note:**
43+
**Note:
44+
**
4445

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.
4748
3. target is a lowercase letter.
4849

4950
### Thought Process {#thought-process}
5051

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
5264
1. We add 1 to target and mod 26 to get the "right" target
5365
2. If the index is equal to the length of list, we return the first element
5466
3. Time complexity O\(logn\)
5567
4. Space complexity O\(1\)
56-
2. asd
5768

5869
### Solution
5970

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+
6096
```java
6197
class Solution {
6298
public char nextGreatestLetter(char[] letters, char target) {

0 commit comments

Comments
 (0)