File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ char nextGreatestLetter (vector<char >& letters, char target) {
4
+ for (char ch : letters) {
5
+ if (ch > target) {
6
+ return ch; // Return the first character greater than target
7
+ }
8
+ }
9
+ return letters[0 ]; // Wrap around case
10
+ }
11
+ };
12
+
13
+
14
+
15
+
16
+ class Solution {
17
+ public:
18
+ char nextGreatestLetter (vector<char >& letters, char target) {
19
+ int left = 0 , right = letters.size () - 1 ;
20
+
21
+ while (left <= right) {
22
+ int mid = left + (right - left) / 2 ;
23
+
24
+ if (letters[mid] > target) {
25
+ right = mid - 1 ; // Move left if mid is greater than target
26
+ } else {
27
+ left = mid + 1 ; // Move right if mid is less than or equal to target
28
+ }
29
+ }
30
+
31
+ // If left goes out of bounds, wrap around to the first element
32
+ return letters[left % letters.size ()];
33
+ }
34
+ };
You can’t perform that action at this time.
0 commit comments