diff --git a/daily/June21.cpp b/daily/June21.cpp index 8e41203..a5297f6 100644 --- a/daily/June21.cpp +++ b/daily/June21.cpp @@ -9,8 +9,7 @@ class Solution { * * @param temperatureA * @param temperatureB - * @return int - */ + * @return int */ int temperatureTrend(vector& temperatureA, vector& temperatureB) { auto cmp = [](int x, int y) { return (x > y) - (x < y); }; diff --git a/daily/June22.cpp b/daily/June22.cpp new file mode 100644 index 0000000..724a9c5 --- /dev/null +++ b/daily/June22.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief LC: 2663: Lexicographically Smallest Beautiful string + * Greedy algorithm + * Time: O(N), N: len(s), Space: O(1), inplace update + * + * @param s + * @param k + * @return string + */ + string smallestBeautifulString(string s, int k) { + k += 'a'; + int n = s.length(); + int i = n - 1; // start from last letter + + s[i]++; + while (i < n) { + if (s[i] == k) { // go upgrade + if (i == 0) { // can't go upgrade + return ""; + } + // go upgrade + s[i] = 'a'; + s[--i]++; + } else if (i && s[i] == s[i - 1] || i > 1 && s[i] == s[i - 2]) { + s[i]++; // if s[i] form the + } else { + i++; // check the latter + } + } + return s; + } +}; \ No newline at end of file