Skip to content

Commit 8ad039b

Browse files
authored
Merge pull request #615 from LetMeFly666/3242
添加问题“3242.设计相邻元素求和服务”的代码和题解
2 parents a04e108 + 1a3db08 commit 8ad039b

12 files changed

+535
-58
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-09 09:55:46
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-09 10:06:29
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
const int adj[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
12+
const int dia[4][2] = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
13+
14+
class NeighborSum {
15+
private:
16+
vector<pair<int, int>> cache;
17+
public:
18+
NeighborSum(vector<vector<int>>& grid) {
19+
int n = grid.size();
20+
cache.resize(n * n);
21+
for (int i = 0; i < n; i++) {
22+
for (int j = 0; j < n; j++) {
23+
int cntAdj = 0, cntDia = 0;
24+
for (int k = 0; k < 4; k++) {
25+
int x = i + adj[k][0], y = j + adj[k][1];
26+
if (x >= 0 && x < n && y >= 0 && y < n) {
27+
cntAdj += grid[x][y];
28+
}
29+
x = i + dia[k][0], y = j + dia[k][1];
30+
if (x >= 0 && x < n && y >= 0 && y < n) {
31+
cntDia += grid[x][y];
32+
}
33+
}
34+
cache[grid[i][j]] = {cntAdj, cntDia};
35+
}
36+
}
37+
}
38+
39+
int adjacentSum(int value) {
40+
return cache[value].first;
41+
}
42+
43+
int diagonalSum(int value) {
44+
return cache[value].second;
45+
}
46+
};
47+
48+
/**
49+
* Your NeighborSum object will be instantiated and called as such:
50+
* NeighborSum* obj = new NeighborSum(grid);
51+
* int param_1 = obj->adjacentSum(value);
52+
* int param_2 = obj->diagonalSum(value);
53+
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-09 10:20:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-09 10:42:32
6+
*/
7+
package main
8+
9+
var direction = []struct{x, y int}{{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}
10+
type Value [][2]int
11+
12+
type NeighborSum struct {
13+
cache Value
14+
}
15+
16+
17+
func Constructor(grid [][]int) NeighborSum {
18+
n := len(grid)
19+
var neighborSum NeighborSum
20+
neighborSum.cache = make(Value, n * n)
21+
for i, row := range grid {
22+
for j, v := range row {
23+
for k, d := range direction {
24+
x, y := i + d.x, j + d.y
25+
if x >= 0 && x < n && y >= 0 && y < n {
26+
neighborSum.cache[v][k / 4] += grid[x][y]
27+
}
28+
}
29+
}
30+
}
31+
return neighborSum
32+
}
33+
34+
35+
func (this *NeighborSum) AdjacentSum(value int) int {
36+
return this.cache[value][0]
37+
}
38+
39+
40+
func (this *NeighborSum) DiagonalSum(value int) int {
41+
return this.cache[value][1]
42+
}
43+
44+
45+
/**
46+
* Your NeighborSum object will be instantiated and called as such:
47+
* obj := Constructor(grid);
48+
* param_1 := obj.AdjacentSum(value);
49+
* param_2 := obj.DiagonalSum(value);
50+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-09 10:15:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-09 10:19:36
6+
*/
7+
class NeighborSum {
8+
private static final int[][] direction = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
9+
private int[][] cache;
10+
11+
public NeighborSum(int[][] grid) {
12+
int n = grid.length;
13+
cache = new int[n * n][2];
14+
for (int i = 0; i < n; i++) {
15+
for (int j = 0; j < n; j++) {
16+
for (int k = 0; k < 8; k++) {
17+
int x = i + direction[k][0], y = j + direction[k][1];
18+
if (x >= 0 && x < n && y >= 0 && y < n) {
19+
cache[grid[i][j]][k / 4] += grid[x][y];
20+
}
21+
}
22+
}
23+
}
24+
}
25+
26+
public int adjacentSum(int value) {
27+
return cache[value][0];
28+
}
29+
30+
public int diagonalSum(int value) {
31+
return cache[value][1];
32+
}
33+
}
34+
35+
/**
36+
* Your NeighborSum object will be instantiated and called as such:
37+
* NeighborSum obj = new NeighborSum(grid);
38+
* int param_1 = obj.adjacentSum(value);
39+
* int param_2 = obj.diagonalSum(value);
40+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-09 10:07:15
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-09 10:15:02
6+
'''
7+
from typing import List
8+
9+
direction = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, 1], [-1, 1], [1, -1]]
10+
11+
class NeighborSum:
12+
def __init__(self, grid: List[List[int]]):
13+
n = len(grid)
14+
self.cache = [[0, 0] for _ in range(n * n)]
15+
for i in range(n):
16+
for j in range(n):
17+
for th, (x, y) in enumerate(direction):
18+
if 0 <= x + i < n and 0 <= y + j < n:
19+
self.cache[grid[i][j]][th // 4] += grid[x + i][y + j]
20+
21+
def adjacentSum(self, value: int) -> int:
22+
return self.cache[value][0]
23+
24+
def diagonalSum(self, value: int) -> int:
25+
return self.cache[value][1]
26+
27+
28+
# Your NeighborSum object will be instantiated and called as such:
29+
# obj = NeighborSum(grid)
30+
# param_1 = obj.adjacentSum(value)
31+
# param_2 = obj.diagonalSum(value)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@
718718
|3216.交换后字典序最小的字符串|简单|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/30/LeetCode%203216.%E4%BA%A4%E6%8D%A2%E5%90%8E%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143362223" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/2971048/letmefly-3216jiao-huan-hou-zi-dian-xu-zu-nxp9/" target="_blank">LeetCode题解</a>|
719719
|3222.求出硬币游戏的赢家|简单|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/05/LeetCode%203222.%E6%B1%82%E5%87%BA%E7%A1%AC%E5%B8%81%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143501415" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/2977629/letmefly-3222qiu-chu-ying-bi-you-xi-de-y-08j3/" target="_blank">LeetCode题解</a>|
720720
|3226.使两个整数相等的位更改次数|简单|<a href="https://leetcode.cn/problems/number-of-bit-changes-to-make-two-integers-equal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/02/LeetCode%203226.%E4%BD%BF%E4%B8%A4%E4%B8%AA%E6%95%B4%E6%95%B0%E7%9B%B8%E7%AD%89%E7%9A%84%E4%BD%8D%E6%9B%B4%E6%94%B9%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143448117" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-bit-changes-to-make-two-integers-equal/solutions/2974558/letmefly-3226shi-liang-ge-zheng-shu-xian-j01h/" target="_blank">LeetCode题解</a>|
721+
|3242.设计相邻元素求和服务|简单|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/11/LeetCode%203242.%E8%AE%BE%E8%AE%A1%E7%9B%B8%E9%82%BB%E5%85%83%E7%B4%A0%E6%B1%82%E5%92%8C%E6%9C%8D%E5%8A%A1/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143698347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/design-neighbor-sum-service/solutions/2985408/letmefly-3242she-ji-xiang-lin-yuan-su-qi-mc7m/" target="_blank">LeetCode题解</a>|
721722
|3254.长度为K的子数组的能量值I|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/06/LeetCode%203254.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143575677" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-i/solutions/2979562/letmefly-3254chang-du-wei-k-de-zi-shu-zu-iu8u/" target="_blank">LeetCode题解</a>|
722723
|3255.长度为K的子数组的能量值II|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/07/LeetCode%203255.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143591327" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/2980432/letmefly-3255chang-du-wei-k-de-zi-shu-zu-rags/" target="_blank">LeetCode题解</a>|
723724
|3259.超级饮料的最大强化能量|中等|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/01/LeetCode%203259.%E8%B6%85%E7%BA%A7%E9%A5%AE%E6%96%99%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BC%BA%E5%8C%96%E8%83%BD%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143429899" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/2973620/letmefly-3259chao-ji-yin-liao-de-zui-da-eeusa/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)