diff --git a/problems/cpp/find-smallest-letter-greater-than-target.cpp b/problems/cpp/find-smallest-letter-greater-than-target.cpp new file mode 100644 index 0000000..e46a482 --- /dev/null +++ b/problems/cpp/find-smallest-letter-greater-than-target.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +class Solution { +public: + char nextGreatestLetter(const std::vector& letters, char target) + { + auto it = std::lower_bound(letters.begin(), letters.end(), target + 1); + return it == letters.end() ? letters.front() : *it; + } +}; + +int main() +{ + Solution s; + + assert((s.nextGreatestLetter(std::vector { 'c', 'f', 'j' }, 'a') == 'c')); + assert((s.nextGreatestLetter(std::vector { 'c', 'f', 'j' }, 'c') == 'f')); + assert((s.nextGreatestLetter(std::vector { 'x', 'x', 'y', 'y' }, 'z') == 'x')); + + return 0; +}