1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| package days;
import java.util.HashSet; import java.util.Set;
public class _2711对角线上不同值的数量差 {
public int[][] differenceOfDistinctValues_prefix(int[][] grid) { int m = grid.length; int n = grid[0].length; int[][] res = new int[m][n];
for (int i = 0; i < m; ++i) { int x = i, y = 0; Set<Integer> s = new HashSet<>(); while (x < m && y < n) { res[x][y] += s.size(); s.add(grid[x][y]); x += 1; y += 1; } }
for (int j = 1; j < n; ++j) { int x = 0, y = j; Set<Integer> s = new HashSet<>(); while (x < m && y < n) { res[x][y] += s.size(); s.add(grid[x][y]); x += 1; y += 1; } }
for (int i = 0; i < m; ++i) { int x = i, y = n - 1; Set<Integer> s = new HashSet<>(); while (x >= 0 && y >= 0) { res[x][y] -= s.size(); res[x][y] = Math.abs(res[x][y]); s.add(grid[x][y]); x -= 1; y -= 1; } }
for (int j = n - 2; j >= 0; --j) { int x = m - 1, y = j; Set<Integer> s = new HashSet<>(); while (x >= 0 && y >= 0) { res[x][y] -= s.size(); res[x][y] = Math.abs(res[x][y]); s.add(grid[x][y]); x -= 1; y -= 1; } }
return res; }
public int[][] differenceOfDistinctValues_gov(int[][] grid) { int m = grid.length, n = grid[0].length; int[][] res = new int[m][n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { Set<Integer> s1 = new HashSet<>(); int x = i + 1, y = j + 1; while (x < m && y < n) { s1.add(grid[x][y]); x += 1; y += 1; } Set<Integer> s2 = new HashSet<>(); x = i - 1; y = j - 1; while (x >= 0 && y >= 0) { s2.add(grid[x][y]); x -= 1; y -= 1; } res[i][j] = Math.abs(s1.size() - s2.size()); } } return res; }
public int[][] differenceOfDistinctValues(int[][] grid) { int n = grid.length, m = grid[0].length; int[][] res = new int[n][m], topLeft = new int[n][m], bottomRight = new int[n][m]; for (int i = 0; i < n; i++) { build(grid, i, 0, topLeft, true); build(grid, i, m - 1, bottomRight, false); } for (int i = 0; i < m; i++) { build(grid, 0, i, topLeft, true); build(grid, n - 1, i, bottomRight, false); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { res[i][j] = Math.abs(topLeft[i][j] - bottomRight[i][j]); } } return res; }
private void build(int[][] grid, int i, int j, int[][] matrix, boolean isTopToBottom) { Set<Integer> set = new HashSet<>(); if (isTopToBottom) { while (i < matrix.length && j < matrix[0].length) { matrix[i][j] = set.size(); set.add(grid[i][j]); i++; j++; } } else { while (i >= 0 && j >= 0) { matrix[i][j] = set.size(); set.add(grid[i][j]); i--; j--; } } }
public static void main(String[] args) { int[][] grid = { {15, 42, 48, 22, 36, 47, 13, 23, 31, 41}, {25, 3, 44, 17, 37, 9, 14, 37, 4, 43}, {7, 15, 38, 10, 25, 7, 37, 6, 43, 4}, {50, 9, 14, 36, 35, 36, 44, 17, 10, 44}, {46, 50, 45, 28, 10, 18, 18, 3, 42, 24}, {14, 11, 13, 32, 37, 31, 50, 32, 12, 38}, {44, 24, 42, 9, 32, 40, 8, 20, 46, 39}, {33, 5, 42, 30, 20, 37, 26, 38, 30, 30}, {32, 39, 31, 33, 41, 23, 4, 29, 44, 22}, {8, 8, 11, 21, 9, 2, 37, 19, 30, 37} }; _2711对角线上不同值的数量差 obj = new _2711对角线上不同值的数量差(); int[][] res = obj.differenceOfDistinctValues(grid); for (int[] ints : res) { for (int anInt : ints) { System.out.print(anInt + " "); } System.out.println(); } }
}
|