problems(cpp): add “744. Find Smallest Letter Greater Than Target”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
6e2c2c2fbd
commit
54f486a1b9
1 changed files with 23 additions and 0 deletions
23
problems/cpp/find-smallest-letter-greater-than-target.cpp
Normal file
23
problems/cpp/find-smallest-letter-greater-than-target.cpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
char nextGreatestLetter(const std::vector<char>& 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;
|
||||||
|
}
|
Loading…
Reference in a new issue