From 54f486a1b907d114abe6ef834ab02347bdb0bf35 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 9 Jun 2023 16:04:35 +0200 Subject: [PATCH] =?UTF-8?q?problems(cpp):=20add=20=E2=80=9C744.=20Find=20S?= =?UTF-8?q?mallest=20Letter=20Greater=20Than=20Target=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...nd-smallest-letter-greater-than-target.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 problems/cpp/find-smallest-letter-greater-than-target.cpp 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; +}