-
Notifications
You must be signed in to change notification settings - Fork 10
/
744_find-smallest-letter-greater-than-target.js
56 lines (55 loc) · 1.99 KB
/
744_find-smallest-letter-greater-than-target.js
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
54
55
56
/**
*
* Problem:
* Given an array of lowercase letters sorted in ascending order, find the smallest
* letter in the given array greater than a given "key". Assume the given array is a
* circular list, which means that the last letter is assumed to be connected with the
* first letter. This also means that the smallest letter in the given array is greater
* than the last letter of the array and is also the first letter of the array. Write a
* function to return the next letter of the given "key".
* https://leetcode.com/problems/find-smallest-letter-greater-than-target/
*
* Example 1:
* Input: ['a', 'c', 'f', 'h'], key = 'f'
* Output: 'h'
* Explanation: The smallest letter greater than 'f' is 'h' in the given array.
*
* Example 2:
* Input: ['a', 'c', 'f', 'h'], key = 'm'
* Output: 'a'
* Explanation: As the array is assumed to be circular, the smallest letter greater than
* 'm' is 'a'.
*
*
* Time: O(log n)
* Space: O(1)
*
* @param {string[]} letters
* @param {string} key
* @return {string}
*/
function findSmallestLetterGreaterThanTarget(letters, key) {
let start = 0;
let end = letters.length;
while (start < end) {
const middle = start + Math.floor((end - start) / 2);
// char can be compared directly here
if (letters[middle] > key) {
end = middle;
} else {
start = middle + 1;
}
}
/**
* Follow the binary search, the only difference is here we need to return the
* "mod" of `start` here. Remember `start` is always the smallest index which
* can satisfy the "end change condition" (e.g. `letters[middle] > key`, the
* smallest index which is greater than the target, exactly as the question asked),
* but remember the index can equal to `letters.length` (example 2 above), that's
* why "mod" here is required.
*/
return letters[start % letters.length];
}
// Test
console.log(findSmallestLetterGreaterThanTarget(['a', 'c', 'f', 'h'], 'f')); // 'h'
console.log(findSmallestLetterGreaterThanTarget(['a', 'c', 'f', 'h'], 'm')); // 'a'