-
Notifications
You must be signed in to change notification settings - Fork 2
/
744.py
51 lines (46 loc) · 1.28 KB
/
744.py
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
# [ LeetCode ] 744. Find Smallest Letter Greater Than Target
def solution(letters: list[str], target: str) -> str:
if letters[-1] <= target:
return letters[0]
else:
start, end = 0, len(letters) - 1
while start <= end:
middle: int = (start + end) // 2
if letters[middle] <= target:
start = middle + 1
else:
end = middle - 1
return letters[start]
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[str] | str] | str]] = [
{
"input": {
"letters": ["c", "f", "g"],
"target": "a"
},
"output": "c"
},
{
"input": {
"letters": ["c", "f", "g"],
"target": "c"
},
"output": "f"
},
{
"input": {
"letters": ["c", "f", "g"],
"target": "d"
},
"output": "f"
},
{
"input": {
"letters": ["c", "f", "g"],
"target": "z"
},
"output": "c"
}
]
for case in cases:
assert case["output"] == solution(**case["input"])