Skip to content

Commit

Permalink
add 3242
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Aug 4, 2024
1 parent 6b0cf19 commit 347a4da
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions paginated_contents/algorithms/4th_thousand/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| # | Title | Solutions | Video | Difficulty | Tag
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
| 3243 | [Shortest Distance After Road Addition Queries I](https://leetcode.com/problems/shortest-distance-after-road-addition-queries-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3243.java) | | Medium |
| 3242 | [Design Neighbor Sum Service](https://leetcode.com/problems/design-neighbor-sum-service/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3242.java) | | Easy |
| 3241 | [Time Taken to Mark All Nodes](https://leetcode.com/problems/time-taken-to-mark-all-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java) | | Hard |
| 3240 | [Minimum Number of Flips to Make Binary Grid Palindromic II](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java) | | Medium |
| 3239 | [Minimum Number of Flips to Make Binary Grid Palindromic I](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java) | | Easy |
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/fishercoder/solutions/fourththousand/_3242.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fishercoder.solutions.fourththousand;

import java.util.HashMap;
import java.util.Map;

public class _3242 {
public static class Solution1 {
class neighborSum {
int[][] matrix;
int m;
int n;
Map<Integer, int[]> map;

public neighborSum(int[][] grid) {
this.matrix = grid;
this.m = grid.length;
this.n = grid[0].length;
this.map = new HashMap<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
map.put(grid[i][j], new int[]{i, j});
}
}
}

public int adjacentSum(int value) {
int[] dirs = new int[]{0, 1, 0, -1, 0};
int[] pos = this.map.get(value);
int sum = 0;
for (int i = 0; i < dirs.length - 1; i++) {
int nextx = pos[0] + dirs[i];
int nexty = pos[1] + dirs[i + 1];
if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) {
sum += matrix[nextx][nexty];
}
}
return sum;
}

public int diagonalSum(int value) {
int[][] dirs = new int[][]{
{-1, 1},
{1, 1},
{1, -1},
{-1, -1}
};
int[] pos = this.map.get(value);
int sum = 0;
for (int[] dir : dirs) {
int nextx = pos[0] + dir[0];
int nexty = pos[1] + dir[1];
if (nextx >= 0 && nextx < m && nexty >= 0 && nexty < n) {
sum += matrix[nextx][nexty];
}
}
return sum;
}
}
}
}

0 comments on commit 347a4da

Please sign in to comment.