File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
0744 Find Smallest Letter Greater Than Target Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ # 0744. Find Smallest Letter Greater Than Target
2+
3+ * Difficulty: easy
4+ * Link: https://leetcode.com/problems/find-smallest-letter-greater-than-target/
5+ * Topics: Binary-Search
6+
7+ # Clarification
8+
9+ 1 . Check the inputs and outputs
10+ - INPUT:
11+ - List[ char]
12+ - char
13+ - OUTPUT: char
14+
15+ # Solution
16+
17+ ### Thought Process
18+
19+ - binary search
20+ - 一開始直接先判斷超出的部分
21+ - 兩個 pointer
22+ - left: 指向陣列中最左邊的 char
23+ - right: 指向陣列中最右邊的 char
24+ - 每次比較 mid = (left+right)/2 index 的 char 與 target
25+ - 若 mid > target → right = mid -1
26+ - 若 mid ≤ target → left = mid + 1
27+ - Implement
28+
29+ ``` python
30+ class Solution :
31+ def nextGreatestLetter (self , letters : List[str ], target : str ) -> str :
32+ # if the number is out of bound
33+ if target >= letters[- 1 ] or target < letters[0 ]:
34+ return letters[0 ]
35+
36+ # binary search
37+ left = 0
38+ right = len (letters)- 1
39+ while left <= right:
40+ mid = (left+ right)// 2
41+ if target >= letters[mid]:
42+ left = mid+ 1
43+ if target < letters[mid]:
44+ right = mid- 1
45+
46+ return letters[left]
47+ ```
48+
49+
50+ # ## Complexity
51+
52+ - Time complexity: log(n)
53+ - Space complexity: log(1 )
54+
55+ # Note
56+
57+ - **** [Python easy solution with detail explanation (modified binary search)](https:// leetcode.com/ problems/ find- smallest- letter- greater- than- target/ discuss/ 1568523 / Python- easy- solution- with - detail- explanation- (modified- binary- search))****
You can’t perform that action at this time.
0 commit comments