|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Deque; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Stack; |
| 11 | + |
| 12 | +public class wk306 { |
| 13 | + //简单题,暴力卷积即可 |
| 14 | + public int[][] largestLocal(int[][] grid) { |
| 15 | + int n = grid.length; |
| 16 | + int[][] res = new int[n - 2][n - 2]; |
| 17 | + for (int i = 0; i < res.length; i++) { |
| 18 | + for (int j = 0; j < res.length; j++) { |
| 19 | + res[i][j] = findMax(i, j, grid); |
| 20 | + } |
| 21 | + } |
| 22 | + return res; |
| 23 | + } |
| 24 | + |
| 25 | + int findMax(int a, int b, int[][] grid) { |
| 26 | + int max = 0; |
| 27 | + for (int i = a; i < a + 3; i++) { |
| 28 | + for (int j = b; j < b + 3; j++) { |
| 29 | + max = Math.max(grid[i][j], max); |
| 30 | + } |
| 31 | + } |
| 32 | + return max; |
| 33 | + } |
| 34 | + |
| 35 | + //中等题,统计入度 |
| 36 | + public int edgeScore(int[] edges) { |
| 37 | + long[] score = new long[edges.length]; |
| 38 | + for (int i = 0; i < edges.length; i++) { |
| 39 | + score[edges[i]] += i; |
| 40 | + } |
| 41 | + int p = -1; |
| 42 | + long s = -1; |
| 43 | + for (int i = 0; i < score.length; i++) { |
| 44 | + if (score[i] > s) { |
| 45 | + p = i; |
| 46 | + s = score[i]; |
| 47 | + } |
| 48 | + } |
| 49 | + return p; |
| 50 | + } |
| 51 | + |
| 52 | + //中等题,dfs回溯 |
| 53 | + /* public String smallestNumber(String pattern) { |
| 54 | + String s = ""; |
| 55 | + for (int i = 1; i <= 9; i++) { |
| 56 | + boolean[] booleans = new boolean[10]; |
| 57 | + booleans[i] = true; |
| 58 | + String ss = help(0, pattern, booleans, i + ""); |
| 59 | + if(ss.equals(""))continue; |
| 60 | + if ("".equals(s) || s.compareTo(ss) > 0) { |
| 61 | + s = ss; |
| 62 | + } |
| 63 | + } |
| 64 | + return s; |
| 65 | + } |
| 66 | +
|
| 67 | + // Map<String, String> memo = new HashMap<>(); |
| 68 | +
|
| 69 | + String help(int index, String pattern, boolean[] visited, String s) { |
| 70 | + if (index >= pattern.length()) { |
| 71 | + return s; |
| 72 | + } |
| 73 | + *//* if (memo.containsKey(s)) { |
| 74 | + return memo.get(s); |
| 75 | + }*//* |
| 76 | + int cur = s.charAt(s.length() - 1) - '0'; |
| 77 | + boolean up = pattern.charAt(index) == 'I'; |
| 78 | + String need = ""; |
| 79 | + for (int i = 1; i <= 9; i++) { |
| 80 | + if (visited[i]) continue; |
| 81 | + if (i > cur == up) { |
| 82 | + visited[i] = true; |
| 83 | + String ss = help(index + 1, pattern, visited, s + i); |
| 84 | + if (!"".equals(ss)) { |
| 85 | + if ("".equals(need)) { |
| 86 | + need = ss; |
| 87 | + } else { |
| 88 | + if (ss.compareTo(need) < 0) { |
| 89 | + need = ss; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + visited[i] = false; |
| 94 | + } |
| 95 | + } |
| 96 | + // memo.put(s, need); |
| 97 | + return need; |
| 98 | +
|
| 99 | + }*/ |
| 100 | + |
| 101 | + //用栈来做 |
| 102 | + public String smallestNumber(String pattern) { |
| 103 | + Deque<Integer> deque=new ArrayDeque<>(); |
| 104 | + |
| 105 | + int num=1; |
| 106 | + StringBuilder sb=new StringBuilder(); |
| 107 | + for (char c : pattern.toCharArray()) { |
| 108 | + if (c == 'D') { |
| 109 | + deque.addLast(num++); |
| 110 | + }else { |
| 111 | + deque.addLast(num++); |
| 112 | + while (!deque.isEmpty()){ |
| 113 | + sb.append(deque.pollLast()); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + deque.addLast(num); |
| 118 | + while (!deque.isEmpty()){ |
| 119 | + sb.append(deque.pollLast()); |
| 120 | + } |
| 121 | + return sb.toString(); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + // f[l][r] 代表 i * (i + 1) * ... * (j - 1) * j |
| 126 | + /* static int[][] f = new int[10][10]; |
| 127 | + static { |
| 128 | + for (int i = 1; i < 10; i++) { |
| 129 | + for (int j = i; j < 10; j++) { |
| 130 | + int cur = 1; |
| 131 | + for (int k = i; k <= j; k++) cur *= k; |
| 132 | + f[i][j] = cur; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + int dp(int x) { |
| 137 | + int t = x; |
| 138 | + List<Integer> nums = new ArrayList<>(); |
| 139 | + while (t != 0) { |
| 140 | + nums.add(t % 10); |
| 141 | + t /= 10; |
| 142 | + } |
| 143 | + int n = nums.size(); |
| 144 | + if (n <= 1) return x + 1; |
| 145 | + int ans = 0; |
| 146 | + for (int i = n - 1, p = 1, s = 0; i >= 0; i--, p++) { |
| 147 | + int cur = nums.get(i), cnt = 0; |
| 148 | + for (int j = cur - 1; j >= 0; j--) { |
| 149 | + if (i == n - 1 && j == 0) continue; |
| 150 | + if (((s >> j) & 1) == 0) cnt++; |
| 151 | + } |
| 152 | + int a = 10 - p, b = a - (n - p) + 1; |
| 153 | + ans += b <= a ? cnt * f[b][a] : cnt; |
| 154 | + if (((s >> cur) & 1) == 1) break; |
| 155 | + s |= (1 << cur); |
| 156 | + if (i == 0) ans++; |
| 157 | + } |
| 158 | + ans += 10; |
| 159 | + for (int i = 2, last = 9; i < n; i++) { |
| 160 | + int cur = last * (10 - i + 1); |
| 161 | + ans += cur; last = cur; |
| 162 | + } |
| 163 | + return ans; |
| 164 | + } |
| 165 | + public int countSpecialNumbers(int n) { |
| 166 | + return dp(n)-1; |
| 167 | + }*/ |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + //困难题,数位dp模板 |
| 172 | + char s[]; |
| 173 | + int dp[][]; |
| 174 | + |
| 175 | + public int countSpecialNumbers(int n) { |
| 176 | + s = Integer.toString(n).toCharArray(); |
| 177 | + int m = s.length; |
| 178 | + dp = new int[m][1 << 10]; |
| 179 | + for (int i = 0; i < m; i++) Arrays.fill(dp[i], -1); |
| 180 | + return f(0, 0, true, false); |
| 181 | + } |
| 182 | + |
| 183 | + //i表示当前位置,mask表示哪些数字使用了,islimit表示是否限制最大到9,isNum表示之前是否有数字 |
| 184 | + int f(int i, int mask, boolean isLimit, boolean isNum) { |
| 185 | + if (i == s.length) return isNum ? 1 : 0; |
| 186 | + if (!isLimit && isNum && dp[i][mask] >= 0) return dp[i][mask]; |
| 187 | + int res = 0; |
| 188 | + //之前没有数字的时候,可以直接去下面查找没有数字的,且没有限制,没有数字 |
| 189 | + if (!isNum) res = f(i + 1, mask, false, false); // 可以跳过当前数位 |
| 190 | + //前面没有数字时,不可以从0开始。 没有限制时,可以到9 |
| 191 | + for (int d = isNum ? 0 : 1, up = isLimit ? s[i] - '0' : 9; d <= up; ++d) // 枚举要填入的数字 d |
| 192 | + if ((mask >> d & 1) == 0) // d 不在 mask 中 |
| 193 | + //isLimit && d == up表示之前有限制并且当前位是最大值,则继续限制 |
| 194 | + res += f(i + 1, mask | (1 << d), isLimit && d == up, true); |
| 195 | + //只缓存没有限制,且前面有数字的情况,只有这种情况才会重复计算 例如 xx23 |
| 196 | + if (!isLimit && isNum) dp[i][mask] = res; |
| 197 | + return res; |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +} |
0 commit comments