leetcode

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

2120.cpp (857B)


0 // 2120. Execution of All Suffix Instructions Staying in a Grid 1 class Solution { 2 public: 3 vector<int> executeInstructions(int n, const vector<int> &startPos, string s) { 4 const auto valid = [n](int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }; 5 6 vector<int> res; 7 res.reserve(s.size()); 8 for (int k = 0; k < s.size(); k++) { 9 int x = startPos[0], y = startPos[1]; 10 for (int i = k; i < s.size(); i++) { 11 if (s[i] == 'L') y--; 12 if (s[i] == 'R') y++; 13 if (s[i] == 'U') x--; 14 if (s[i] == 'D') x++; 15 if (!valid(x, y)) { 16 res.push_back(i - k); 17 goto next; 18 } 19 } 20 res.push_back(s.size() - k); 21 next:; 22 } 23 return res; 24 } 25 };