diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/find_smallest_letter.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/find_smallest_letter.cpp')
-rw-r--r-- | other/find_smallest_letter.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/other/find_smallest_letter.cpp b/other/find_smallest_letter.cpp new file mode 100644 index 0000000..01a5562 --- /dev/null +++ b/other/find_smallest_letter.cpp @@ -0,0 +1,34 @@ +// 744. Find Smallest Letter Greater Than Target +#include "leetcode.h" + +/* + * given an array of characters 'letters' that is sorted in non-decreasing + * order, and a character 'target'. there are at least two different characters + * in 'letters'. return the smallest character in 'letters' that is + * lexicographically greater than 'target'. if such a character does not exist, + * return the first character in 'letters'. + */ + +class Solution { +public: + char nextGreatestLetter(vector<char> &letters, char target) { + int low = 0, high = letters.size() - 1; + while (low < high) { + int mid = low + (high - low) / 2; + if (letters[mid] <= target) + low = mid + 1; + else + high = mid; + } + return letters[low] > target ? letters[low] : letters[0]; + } +}; + +int main() { + Solution obj; + vector<char> l1_2 = {'c', 'f', 'j'}; + vector<char> l3 = {'x', 'x', 'y', 'y'}; + printf("%c", obj.nextGreatestLetter(l1_2, 'a')); // expect: c + printf("%c", obj.nextGreatestLetter(l1_2, 'c')); // expect: f + printf("%c", obj.nextGreatestLetter(l3, 'z')); // expect: x +} |