leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit e4f1cf77688f1215190d0ea580edb0a2301d26b8
parent 774e91e7c1e20776bdb75bedd2698a0c795b6da7
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat, 18 May 2024 12:09:08 +0200

1 Random Problem

Diffstat:
AProblems/1138.cpp | 31+++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/Problems/1138.cpp b/Problems/1138.cpp @@ -0,0 +1,31 @@ +class Solution { + using array_t = array<pair<int, int>, 26>; + static constexpr const array_t lookup = []() constexpr -> array_t { + array_t res; + + for (int i = 0; i < 26; i++) + res[i] = {i / 5, i % 5}; + + return res; + }(); + + public: + string alphabetBoardPath(const string &target) const { + int x = 0, y = 0; + string res; + + for (const char c : target) { + const auto [i, j] = lookup[c - 'a']; + + if (j < y) res += string(y - j, 'L'); + if (i < x) res += string(x - i, 'U'); + if (i > x) res += string(i - x, 'D'); + if (j > y) res += string(j - y, 'R'); + + res += '!'; + x = i, y = j; + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -662,6 +662,7 @@ for solving problems. | 1129 | Medium | [Shortest Path with Alternating Colors](Problems/1129.cpp) | | 1130 | Medium | [Minimum Cost Tree From Leaf Values](Problems/1130.cpp) | | 1137 | Easy | [N-th Tribonacci Number](Problems/1137.cpp) | +| 1138 | Medium | [Alphabet Board Path](Problems/1138.cpp) | | 1140 | Medium | [Stone Game II](Problems/1140.cpp) | | 1141 | Easy | [User Activity for the Past 30 Days I](Problems/1141.cpp) | | 1143 | Medium | [Longest Common Subsequence](Problems/1143.cpp) |