Skip to content

Commit df99dc7

Browse files
authored
Add files via upload
1 parent 58c7017 commit df99dc7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
};

0 commit comments

Comments
 (0)