Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b771202

Browse files
LeetCode problem: 744. Find Smallest Letter Greater Than Target
1 parent 84305cf commit b771202

File tree

3 files changed

+176
-0
lines changed
  • src
    • main/java/com/borrelunde/leetcodesolutions/problem0744/findsmallestlettergreaterthantarget
    • test/java/com/borrelunde/leetcodesolutions/problem0744/findsmallestlettergreaterthantarget

3 files changed

+176
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 744. Find Smallest Letter Greater Than Target
2+
3+
Difficulty: `Easy`
4+
Topics: `Array`, `Binary Search`
5+
6+
You are given an array of characters `letters` that is sorted in **non-decreasing order**, and a character `target`.
7+
There are **at least two different characters** in `letters`.
8+
9+
Return *the smallest character in `letters` that is lexicographically greater than `target`*. If such a character does
10+
not exist, return the first character in `letters`.
11+
12+
**Example 1:**
13+
14+
```text
15+
Input: letters = ["c","f","j"], target = "a"
16+
Output: "c"
17+
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
18+
```
19+
20+
**Example 2:**
21+
22+
```text
23+
Input: letters = ["c","f","j"], target = "c"
24+
Output: "f"
25+
Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
26+
```
27+
28+
**Example 3:**
29+
30+
```text
31+
Input: letters = ["x","x","y","y"], target = "z"
32+
Output: "x"
33+
Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
34+
```
35+
36+
**Constraints:**
37+
38+
- `2 <= letters.length <= 10^4`
39+
- `letters[i]` is a lowercase English letter.
40+
- `letters` is sorted in **non-decreasing** order.
41+
- `letters` contains at least two different characters.
42+
- `target` is a lowercase English letter.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.borrelunde.leetcodesolutions.problem0744.findsmallestlettergreaterthantarget;
2+
3+
/**
4+
* This is the solution to the LeetCode problem: 744. Find Smallest Letter
5+
* Greater Than Target
6+
*
7+
* @author Børre A. Opedal Lunde
8+
* @since 2024年01月29日
9+
*/
10+
public class Solution {
11+
12+
public char nextGreatestLetter(char[] letters, char target) {
13+
14+
int lowIndex = 0;
15+
int highIndex = letters.length - 1;
16+
17+
// Binary search.
18+
while (lowIndex <= highIndex) {
19+
20+
final int middleIndex = lowIndex + (highIndex - lowIndex) / 2;
21+
final char middleCharacter = letters[middleIndex];
22+
23+
if (middleCharacter > target) {
24+
highIndex = middleIndex - 1;
25+
} else {
26+
lowIndex = middleIndex + 1;
27+
}
28+
}
29+
30+
// After the search, the low index is the index of the smallest
31+
// greatest character. The only chance it isn't is if the index
32+
// surpasses the array and is out of bounds.
33+
34+
// In that case, return the first character in letters.
35+
if (lowIndex >= letters.length) {
36+
return letters[0];
37+
}
38+
39+
// Otherwise, return the character at its position.
40+
return letters[lowIndex];
41+
}
42+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.borrelunde.leetcodesolutions.problem0744.findsmallestlettergreaterthantarget;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
/**
14+
* This is the test to the LeetCode problem: 744. Find Smallest Letter Greater
15+
* Than Target
16+
*
17+
* @author Børre A. Opedal Lunde
18+
* @since 2024年01月29日
19+
*/
20+
@DisplayName("Find Smallest Letter Greater Than Target")
21+
class SolutionTest {
22+
23+
private final Solution solution = new Solution();
24+
25+
@Test
26+
@DisplayName("Example one")
27+
void exampleOne() {
28+
char[] letters = {'c', 'f', 'j'};
29+
char target = 'a';
30+
31+
char expected = 'c';
32+
char actual = solution.nextGreatestLetter(letters, target);
33+
34+
assertEquals(expected, actual);
35+
}
36+
37+
@Test
38+
@DisplayName("Example two")
39+
void exampleTwo() {
40+
char[] letters = {'c', 'f', 'j'};
41+
char target = 'c';
42+
43+
char expected = 'f';
44+
char actual = solution.nextGreatestLetter(letters, target);
45+
46+
assertEquals(expected, actual);
47+
}
48+
49+
@Test
50+
@DisplayName("Example three")
51+
void exampleThree() {
52+
char[] letters = {'x', 'x', 'y', 'y'};
53+
char target = 'z';
54+
55+
char expected = 'x';
56+
char actual = solution.nextGreatestLetter(letters, target);
57+
58+
assertEquals(expected, actual);
59+
}
60+
61+
@Test
62+
@DisplayName("Should return first letter when target is last element in array")
63+
void shouldReturnFirstLetterWhenTargetIsLastElementInArray() {
64+
char[] letters = {'a', 'b', 'c', 'd', 'f', 'g'};
65+
char target = 'g';
66+
67+
char expected = 'a';
68+
char actual = solution.nextGreatestLetter(letters, target);
69+
70+
assertEquals(expected, actual);
71+
}
72+
73+
static Stream<Arguments> provideLettersWithTargetInMiddleButNotPresent() {
74+
return Stream.of(
75+
Arguments.of(new char[]{'b', 'd'}, 'c', 'd'),
76+
Arguments.of(new char[]{'a', 'b', 'd', 'e'}, 'c', 'd'),
77+
Arguments.of(new char[]{'m', 'n', 'p', 'q'}, 'o', 'p'),
78+
Arguments.of(new char[]{'n', 'n', 'p', 'p'}, 'o', 'p'),
79+
Arguments.of(new char[]{'a', 'b', 'x', 'y'}, 'c', 'x'),
80+
Arguments.of(new char[]{'c', 'f', 'j'}, 'd', 'f')
81+
);
82+
}
83+
84+
@ParameterizedTest(name = "In {0} with target {1} should return {2}")
85+
@DisplayName("Should return smallest greater character when target is not in array")
86+
@MethodSource("provideLettersWithTargetInMiddleButNotPresent")
87+
void shouldReturnSmallestGreaterCharacterWhenTargetIsNotInArray(char[] letters, char target, char expected) {
88+
char actual = solution.nextGreatestLetter(letters, target);
89+
90+
assertEquals(expected, actual);
91+
}
92+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /