Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. // Runtime: 1 ms, faster than 44.95% of Java online submissions for Magic Squares In Grid.
  2. // Memory Usage: 34.6 MB, less than 100.00% of Java online submissions for Magic Squares In Grid.
  3. class Solution {
  4.     public int numMagicSquaresInside(int[][] grid) {
  5.         int size = 3;
  6.                        
  7.         int maxNumber = size * size;
  8.        
  9.         int magicSum = 0;
  10.         for (int i = 1; i <= maxNumber; i++) magicSum += i;
  11.         magicSum /= size;
  12.                
  13.        
  14.         int[][][] directions = buildDirections(size);
  15.        
  16.         int count = 0;
  17.         for (int i = 0; i <= grid.length - size; i++)
  18.             for (int j = 0; j <= grid[0].length - size; j++)
  19.                 if (isMagic(grid, i, j, size, maxNumber, magicSum, directions)) count++;
  20.        
  21.         return count;
  22.     }
  23.  
  24.     private boolean isMagic(int[][] grid, int i, int j, int size, int maxNumber, int magicSum, int[][][] directions) {
  25.         boolean[] presence = new boolean[maxNumber];
  26.        
  27.         for (int ii = i; ii < i + size; ii++)
  28.             for (int jj = j; jj < j + size; jj++) {
  29.                 int n = grid[ii][jj];
  30.                 if (n < 1 || n > maxNumber) {
  31.                     return false;
  32.                 }
  33.                 if (presence[n - 1]) return false;
  34.                 else presence[n - 1] = true;
  35.             }
  36.        
  37.         for (int[][] direction: directions) {
  38.             if (sum(grid, i, j, direction) != magicSum) return false;            
  39.         }
  40.        
  41.         return true;
  42.     }
  43.        
  44.     private int[][][] buildDirections(int size) {
  45.         int[][][] directions = new int[2*size + 2][][];
  46.         int pos = 0;
  47.        
  48.         for (int r = 0; r < size; r++) {
  49.             int[][] direction = new int[size][2];
  50.             for (int c = 0; c < size; c++) direction[c] = new int[] { r, c };
  51.             directions[pos++] = direction;
  52.         }
  53.        
  54.         for (int c = 0; c < size; c++) {
  55.             int[][] direction = new int[size][2];
  56.             for (int r = 0; r < size; r++) direction[r] = new int[] { r, c };
  57.             directions[pos++] = direction;
  58.         }
  59.        
  60.         int[][] d1 = new int[size][2];            
  61.         for (int i = 0; i < size; i++) d1[i] = new int[] { i, i };
  62.         directions[pos++] = d1;
  63.        
  64.         int[][] d2 = new int[size][2];            
  65.         for (int i = 0; i < size; i++) d2[i] = new int[] { size - i - 1, i };
  66.         directions[pos++] = d2;
  67.        
  68.         return directions;
  69.     }
  70.        
  71.     private int sum(int[][] grid, int i, int j, int[][] direction) {
  72.         int sum = 0;
  73.         for (int[] delta: direction) sum += grid[i + delta[0]][j + delta[1]];
  74.         return sum;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement