leetcode

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

0874.cpp (952B)


0 class Solution {
1 public:
2 int robotSim(const vector<int> &commands, vector<vector<int>> &obstacles) const {
3 unordered_map<int, unordered_set<int>> us;
5 for (const auto &obstacle : obstacles) {
6 us[obstacle[0]].emplace(obstacle[1]);
7 }
9 int res = 0;
10 int x = 0, y = 0, dir = 0;
11 static const int offset[] = {0, 1, 0, -1, 0};
12 for (const auto &command : commands) {
13 switch (command) {
14 case -2: dir = (dir + 3) % 4; break;
15 case -1: dir = (dir + 5) % 4; break;
16 default:
17 for (int i = 0; i < command; i++) {
18 const int nx = x + offset[dir];
19 const int ny = y + offset[dir + 1];
21 if (us[nx].count(ny)) break;
23 x = nx, y = ny;
24 res = max(res, x * x + y * y);
25 }
26 }
27 }
29 return res;
30 }
31 };