C++ O(n) 4ms O(1) memory, pretty solution with C++20 spaceship operator
While leetcode doesn\'t have the spaceship operator yet, I created a quick utility function that will mimic it. The way this solution works is by assigning each
While leetcode doesn\'t have the spaceship operator yet, I created a quick utility function that will mimic it. The way this solution works is by assigning each direction to an index number in constant time and then tracking the closest distance for each of the directions.\n\n```\nclass Solution {\npublic:\n    int spaceship(int x, int y) {\n        if (x > y)\n            return 1;\n        if (x < y)\n            return -1;\n        return 0;\n    }\n    \n    vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {\n        vector<int> direction_distances(9, -1);\n        vector<vector<int>> queen_coords(9);\n        \n        for (auto queen : queens) {\n            int dx = queen[0] - king[0];\n            int dy = queen[1] - king[1];\n            if (dx == 0 or dy == 0 or abs(dx) == abs(dy)) {\n                // Queen is able to attack. Now to check if closest\n                // int direction = (dy <=> 0) * 3 + (dx <=> 0) + 4;\n                int direction = spaceship(dy, 0) * 3 + spaceship(dx, 0) + 4;\n                int distance =  dy == 0 ? abs(dx) : abs(dy);\n                if (direction_distances[direction] == -1 or direction_distances[direction] > distance) {\n                    direction_distances[direction] = distance;\n                    queen_coords[direction] = queen;\n                }\n            }\n        }\n        \n        vector<vector<int>> result;\n        for (size_t i = 0; i < 9; i++) {\n            if (direction_distances[i] != -1)\n                result.push_back(queen_coords[i]);\n        }\n        return result;\n    }\n};\n```
['C']