3242. Design Neighbor Sum Service Easy

@problem@discussion
#Array#Hash Table#Design#Matrix#Simulation



1/**
2 * [3242] Design Neighbor Sum Service
3 *
4 * You are given a n x n 2D array grid containing distinct elements in the range [0, n^2 - 1].
5 * Implement the NeighborSum class:
6 * 
7 * 	NeighborSum(int [][]grid) initializes the object.
8 * 	int adjacentSum(int value) returns the sum of elements which are adjacent neighbors of value, that is either to the top, left, right, or bottom of value in grid.
9 * 	int diagonalSum(int value) returns the sum of elements which are diagonal neighbors of value, that is either to the top-left, top-right, bottom-left, or bottom-right of value in grid.
10 * 
11 * <img alt="" src="https://assets.leetcode.com/uploads/2024/06/24/design.png" style="width: 400px; height: 248px;" />
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input:
16 * ["NeighborSum", "adjacentSum", "adjacentSum", "diagonalSum", "diagonalSum"]
17 * [[[[0, 1, 2], [3, 4, 5], [6, 7, 8]]], [1], [4], [4], [8]]
18 * Output: [null, 6, 16, 16, 4]
19 * Explanation:
20 * <strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/06/24/designexample0.png" style="width: 250px; height: 249px;" />
21 * 
22 * 	The adjacent neighbors of 1 are 0, 2, and 4.
23 * 	The adjacent neighbors of 4 are 1, 3, 5, and 7.
24 * 	The diagonal neighbors of 4 are 0, 2, 6, and 8.
25 * 	The diagonal neighbor of 8 is 4.
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input:
30 * ["NeighborSum", "adjacentSum", "diagonalSum"]
31 * [[[[1, 2, 0, 3], [4, 7, 15, 6], [8, 9, 10, 11], [12, 13, 14, 5]]], [15], [9]]
32 * Output: [null, 23, 45]
33 * Explanation:
34 * <strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/06/24/designexample2.png" style="width: 300px; height: 300px;" />
35 * 
36 * 	The adjacent neighbors of 15 are 0, 10, 7, and 6.
37 * 	The diagonal neighbors of 9 are 4, 12, 14, and 15.
38 * </div>
39 *  
40 * Constraints:
41 * 
42 * 	3 <= n == grid.length == grid[0].length <= 10
43 * 	0 <= grid[i][j] <= n^2 - 1
44 * 	All grid[i][j] are distinct.
45 * 	value in adjacentSum and diagonalSum will be in the range [0, n^2 - 1].
46 * 	At most 2 * n^2 calls will be made to adjacentSum and diagonalSum.
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/design-neighbor-sum-service/
52// discuss: https://leetcode.com/problems/design-neighbor-sum-service/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56struct NeighborSum {
57        false
58    }
59
60
61/** 
62 * `&self` means the method takes an immutable reference.
63 * If you need a mutable reference, change it to `&mut self` instead.
64 */
65impl NeighborSum {
66
67    fn new(grid: Vec<Vec<i32>>) -> Self {
68        
69    }
70    
71    fn adjacent_sum(&self, value: i32) -> i32 {
72        
73    }
74    
75    fn diagonal_sum(&self, value: i32) -> i32 {
76        
77    }
78}
79
80/**
81 * Your NeighborSum object will be instantiated and called as such:
82 * let obj = NeighborSum::new(grid);
83 * let ret_1: i32 = obj.adjacent_sum(value);
84 * let ret_2: i32 = obj.diagonal_sum(value);
85 */
86
87// submission codes end
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_3242() {
95    }
96}
97


Back
© 2025 bowen.ge All Rights Reserved.