leetcode

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

commit 1c2c815cfdd4dbd451dd2972974db99e628c2c6e
parent 50c0e709dc9371f0e221eb3073f128006a557b49
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  4 Sep 2024 20:58:39 +0200

Daily Problem

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

diff --git a/Problems/0874.cpp b/Problems/0874.cpp @@ -0,0 +1,32 @@ +class Solution { + public: + int robotSim(const vector<int> &commands, vector<vector<int>> &obstacles) const { + unordered_map<int, unordered_set<int>> us; + + for (const auto &obstacle : obstacles) { + us[obstacle[0]].emplace(obstacle[1]); + } + + int res = 0; + int x = 0, y = 0, dir = 0; + static const int offset[] = {0, 1, 0, -1, 0}; + for (const auto &command : commands) { + switch (command) { + case -2: dir = (dir + 3) % 4; break; + case -1: dir = (dir + 5) % 4; break; + default: + for (int i = 0; i < command; i++) { + const int nx = x + offset[dir]; + const int ny = y + offset[dir + 1]; + + if (us[nx].count(ny)) break; + + x = nx, y = ny; + res = max(res, x * x + y * y); + } + } + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -551,6 +551,7 @@ for solving problems. | 0869 | Medium | [Reordered Power of 2](Problems/0869.cpp) | | 0870 | Medium | [Advantage Shuffle](Problems/0870.cpp) | | 0872 | Easy | [Leaf-Similar Trees](Problems/0872.cpp) | +| 0874 | Medium | [Walking Robot Simulation](Problems/0874.cpp) | | 0875 | Medium | [Koko Eating Bananas](Problems/0875.cpp) | | 0876 | Easy | [Middle of the Linked List](Problems/0876.cpp) | | 0877 | Medium | [Stone Game](Problems/0877.cpp) |