Python, interview quality, 20 ms beats 94.2%
In my experience, interviewers are looking for this type of solution.\nLooping through the input list is O(n), and no other loops are used.\nThe only the best m
In my experience, interviewers are looking for this type of solution.\nLooping through the input list is O(n), and no other loops are used.\nThe only the best match in each direction is stored in memory.\n\nIt\'s worth stepping back from leetcode contests for a moment, and asking\nif the code you\'re writing would be viewed favorably in an interview.\nEven the best solutions, during a contest, tend to use variables that\nare only one letter long, and do not describe their purpose.  Those are\nquick to write under time pressure, but they do not look good during\nan interview.  If other people can\'t read your code easily, it costs\ntime, effort and confusion.  It looks bad in an interview.\n\nSo I don\'t always follow that approach myself, but here I\'ve tried\nto use good variable names, O(n) time, and O(n) space.\n```python\nclass Solution(object):\n    def queensAttacktheKing(self, queens, king):\n        # initial minimum distance must be greater than 7+7 (farthest diagonal)\n        best_dist = { (x,y) : 15  for x in [-1,0,1] for y in [-1,0,1]}\n        best_queens = {}\n        king_x, king_y = king\n\n        for queen in queens:\n            queen_x, queen_y = queen\n\n            # raw diff can be neative, but distance will be positive\n            x_diff, y_diff = queen_x - king_x, queen_y - king_y\n            x_dist, y_dist = abs(x_diff), abs(y_diff)\n            if x_dist and y_dist and x_dist != y_dist:\n                # no shared row, and not on a a diagonal\n                continue\n\n            # avoid (1//0) by using (0 and 1//0), which evaluates to 0.\n            dist = x_dist + y_dist\n            direction = (x_diff and x_diff//x_dist, y_diff and y_diff//y_dist)\n            if dist < best_dist[direction]:\n                # closest to king so far, in this direction.\n                best_dist[direction] = dist\n                best_queens[direction] = queen\n\n        return best_queens.values()\n```
['Python']