{"id":10238,"date":"2025-03-29T15:36:51","date_gmt":"2025-03-29T22:36:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10238"},"modified":"2025-03-29T15:37:40","modified_gmt":"2025-03-29T22:37:40","slug":"leetcode-3242-design-neighbor-sum-service","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-3242-design-neighbor-sum-service\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3242. Design Neighbor Sum Service"},"content":{"rendered":"\n<p>\u9752\u94dc Brute Force\uff1a\u6bcf\u6b21\u9700\u8981\u82b1\u8d39O(n*n)\u7684\u65f6\u95f4\u53bb\u67e5\u627equery\u6240\u5728\u7684\u683c\u5b50\uff0c\u6c42\u548c\u662fO(1)\u3002\u603b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8fbe\u5230O(n^4)\uff0c\u80af\u5b9a\u4f1a\u8d85\u65f6\u3002<\/p>\n\n\n\n<p>\u767d\u94f6 Hashtable\uff1a\u7531\u4e8e\u6240\u6709\u5143\u7d20\u7684\u503c\u7684\u552f\u4e00\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528hashtable\uff08\u6216\u8005\u6570\u7ec4\uff09\u6765\u8bb0\u5f55value\u6240\u5728\u7684\u683c\u5b50\u5750\u6807\uff0c\u8fd9\u6837\u6bcf\u6b21Query\u90fd\u662fO(1)\u65f6\u95f4\u3002<br>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u521d\u59cb\u5316O(n^2)\uff0cquery O(1)\u3002<br>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n^2)<\/p>\n\n\n\n<pre lang=\"c++\">\nclass NeighborSum {\npublic:\n  NeighborSum(vector<vector<int>>& grid): \n    n_(grid.size()), g_(&grid), xy_(n_ * n_) {\n    for (int i = 0; i < n_; ++i)\n      for (int j = 0; j < n_; ++j)\n        xy_[grid[i][j]] = {j, i};\n  }\n  \n  int adjacentSum(int value) {\n    auto [x, y] = xy_[value];\n    return val(x, y - 1) + val(x, y + 1) + val(x + 1, y) + val(x - 1, y);\n  }\n  \n  int diagonalSum(int value) {\n    auto [x, y] = xy_[value];\n    return val(x - 1, y - 1) + val(x - 1, y + 1) + val(x + 1, y - 1) + val(x + 1, y + 1);\n  }\nprivate:\n  inline int val(int x, int y) const {\n    if (x < 0 || x >= n_ || y < 0 || y >= n_) return 0;\n    return (*g_)[y][x];\n  }\n  int n_;\n  vector<vector<int>>* g_;\n  vector<pair<int, int>> xy_;\n};\n<\/pre>\n\n\n\n<p>\u9ec4\u91d1 Precompute\uff1a\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u987a\u4fbf\u6c42\u5408\uff0c\u5f00\u4e24\u4e2a\u6570\u7ec4\u4e00\u4e2a\u5b58\u4e0a\u4e0b\u5de6\u53f3\u7684\uff0c\u4e00\u4e2a\u5b58\u5bf9\u89d2\u7ebf\u7684\u3002query\u7684\u65f6\u5019\u76f4\u63a5\u8fd4\u56de\u7b54\u6848\u5c31\u53ef\u4ee5\u4e86\u3002<br>\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u767d\u94f6\u662f\u4e00\u6837\u7684\uff0c\u4f46\u4f1a\u5feb\u4e00\u4e9b\uff08\u7701\u53bb\u4e86\u6c42\u5408\u7684\u8fc7\u7a0b\uff09\u3002<\/p>\n\n\n\n<pre lang=\"c++\">\nclass NeighborSum {\npublic:\n  NeighborSum(vector<vector<int>>& grid) {\n    int n = grid.size();\n    adj_.resize(n * n);\n    dia_.resize(n * n);\n    auto v = [&](int x, int y) -> int {\n      if (x < 0 || x >= n || y < 0 || y >= n) \n        return 0;\n      return grid[y][x];\n    };\n    for (int y = 0; y < n; ++y)\n      for (int x = 0; x < n; ++x) {\n        adj_[grid[y][x]] = v(x, y - 1) + v(x, y + 1) + v(x + 1, y) + v(x - 1, y);\n        dia_[grid[y][x]] = v(x - 1, y - 1) + v(x - 1, y + 1) + v(x + 1, y - 1) + v(x + 1, y + 1);\n      }\n  }\n  \n  int adjacentSum(int value) {\n    return adj_[value];\n  }\n  \n  int diagonalSum(int value) {\n    return dia_[value];\n  }\nprivate:\n  vector<int> adj_;\n  vector<int> dia_;\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9752\u94dc Brute Force\uff1a\u6bcf\u6b21\u9700\u8981\u82b1\u8d39O(n*n)\u7684\u65f6\u95f4\u53bb\u67e5\u627equery\u6240\u5728\u7684\u683c\u5b50\uff0c\u6c42\u548c\u662fO(1)\u3002\u603b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8fbe\u5230O(n^4)\uff0c\u80af\u5b9a\u4f1a\u8d85\u65f6\u3002 \u767d\u94f6 Hashtable\uff1a\u7531\u4e8e\u6240\u6709\u5143\u7d20\u7684\u503c\u7684\u552f\u4e00\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528hashtable\uff08\u6216\u8005\u6570\u7ec4\uff09\u6765\u8bb0\u5f55value\u6240\u5728\u7684\u683c\u5b50\u5750\u6807\uff0c\u8fd9\u6837\u6bcf\u6b21Query\u90fd\u662fO(1)\u65f6\u95f4\u3002\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u521d\u59cb\u5316O(n^2)\uff0cquery O(1)\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(n^2) class NeighborSum { public: NeighborSum(vector&#038; grid): n_(grid.size()), g_(&#038;grid), xy_(n_ * n_) { for (int i = 0; i <&#8230;\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,82,764],"class_list":["post-10238","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-precompute","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10238"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10238\/revisions"}],"predecessor-version":[{"id":10240,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10238\/revisions\/10240"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}