|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.Deque; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.LinkedList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Queue; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +public class wk308 { |
| 17 | + |
| 18 | + //ranking: 1283 / 6394 |
| 19 | + |
| 20 | + //简单题,关键在于排序,可以前缀和+二分优化 |
| 21 | + public int[] answerQueries(int[] nums, int[] queries) { |
| 22 | + int[] ans = new int[queries.length]; |
| 23 | + Arrays.sort(nums); |
| 24 | + for (int i = 0; i < queries.length; i++) { |
| 25 | + int count = 0; |
| 26 | + int sum = 0; |
| 27 | + for (int j = 0; j < nums.length; j++) { |
| 28 | + sum += nums[j]; |
| 29 | + count++; |
| 30 | + if (sum > queries[i]) { |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + if (sum > queries[i]) { |
| 35 | + count -= 1; |
| 36 | + } |
| 37 | + ans[i] = count; |
| 38 | + } |
| 39 | + return ans; |
| 40 | + } |
| 41 | + |
| 42 | + //中等题,栈模拟就行 |
| 43 | + public String removeStars(String s) { |
| 44 | + Deque<Character> deque = new ArrayDeque<>(); |
| 45 | + for (int i = 0; i < s.length(); i++) { |
| 46 | + char c = s.charAt(i); |
| 47 | + if (c == '*') { |
| 48 | + deque.pollLast(); |
| 49 | + } else { |
| 50 | + deque.addLast(c); |
| 51 | + } |
| 52 | + } |
| 53 | + StringBuilder sb = new StringBuilder(); |
| 54 | + while (!deque.isEmpty()) { |
| 55 | + sb.append(deque.pollFirst()); |
| 56 | + } |
| 57 | + return sb.toString(); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + //中等题,非必要不前进 |
| 62 | + /* public int garbageCollection(String[] garbage, int[] travel) { |
| 63 | + int[][] count = new int[garbage.length][3]; |
| 64 | + for (int i = 0; i < garbage.length; i++) { |
| 65 | + String g = garbage[i]; |
| 66 | + for (char c : g.toCharArray()) { |
| 67 | + if (c == 'M') { |
| 68 | + count[i][0]++; |
| 69 | + } else if (c == 'P') { |
| 70 | + count[i][1]++; |
| 71 | + } else { |
| 72 | + count[i][2]++; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + int ans = 0; |
| 77 | + for (int k = 0; k < 3; k++) { |
| 78 | + int cost = 0; |
| 79 | + int cur = 0; |
| 80 | + for (int i = 0; i < count.length; i++) { |
| 81 | + if (i > 0) { |
| 82 | + cur += travel[i - 1]; |
| 83 | + } |
| 84 | + if (count[i][k] > 0) { |
| 85 | + cost += count[i][k]; |
| 86 | + cost += cur; |
| 87 | + cur = 0; |
| 88 | + } |
| 89 | + } |
| 90 | + // System.out.println(cost); |
| 91 | + ans += cost; |
| 92 | + } |
| 93 | +
|
| 94 | + return ans; |
| 95 | + }*/ |
| 96 | + |
| 97 | + //一种简单的做法,只与最右边出现的有关 |
| 98 | + public int garbageCollection(String[] garbage, int[] travel) { |
| 99 | + int ans = 0; |
| 100 | + int[] right = new int[3]; |
| 101 | + for (int j = 0; j < garbage.length; j++) { |
| 102 | + String s = garbage[j]; |
| 103 | + ans += s.length(); |
| 104 | + for (int i = 0; i < "MPG".toCharArray().length; i++) { |
| 105 | + char c = "MPG".charAt(i); |
| 106 | + if (s.indexOf(c) >= 0) { |
| 107 | + right[i] = j; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + for (int i = 0; i < right.length; i++) { |
| 113 | + for (int j = 0; j < Math.min(travel.length, right[i]); j++) { |
| 114 | + ans += travel[j + 1]; |
| 115 | + } |
| 116 | + } |
| 117 | + return ans; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + //困难题,周赛做的太麻烦了 |
| 122 | +// public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) { |
| 123 | +// int[] row = new int[k + 1]; |
| 124 | +// int[] col = new int[k + 1]; |
| 125 | +// Map<Integer, Set<Integer>> rowMap = new HashMap<>(); |
| 126 | +// Map<Integer, Set<Integer>> colMap = new HashMap<>(); |
| 127 | +// |
| 128 | +// for (int[] rowCondition : rowConditions) { |
| 129 | +// if (!rowMap.containsKey(rowCondition[0])) { |
| 130 | +// rowMap.put(rowCondition[0], new HashSet<>()); |
| 131 | +// } |
| 132 | +// boolean add = rowMap.get(rowCondition[0]).add(rowCondition[1]); |
| 133 | +// if (add) row[rowCondition[1]]++; |
| 134 | +// |
| 135 | +// } |
| 136 | +// for (int[] colCondition : colConditions) { |
| 137 | +// if (!colMap.containsKey(colCondition[0])) { |
| 138 | +// colMap.put(colCondition[0], new HashSet<>()); |
| 139 | +// } |
| 140 | +// boolean add = colMap.get(colCondition[0]).add(colCondition[1]); |
| 141 | +// if (add) col[colCondition[1]]++; |
| 142 | +// |
| 143 | +// |
| 144 | +// } |
| 145 | +// |
| 146 | +// int[] l = help(colMap, col); |
| 147 | +// if (l == null) return new int[][]{}; |
| 148 | +// Queue<Integer> queue = new LinkedList<>(); |
| 149 | +// for (int i = 1; i < row.length; i++) { |
| 150 | +// //没有入度 |
| 151 | +// if (row[i] == 0) { |
| 152 | +// queue.add(i); |
| 153 | +// } |
| 154 | +// } |
| 155 | +// int[][] res = new int[k][k]; |
| 156 | +// int up = 0; |
| 157 | +// int add = 0; |
| 158 | +// while (!queue.isEmpty()) { |
| 159 | +// int size = queue.size(); |
| 160 | +// List<Integer> list = new ArrayList<>(); |
| 161 | +// while (size-- > 0) { |
| 162 | +// Integer poll = queue.poll(); |
| 163 | +// list.add(poll); |
| 164 | +// for (Integer next : rowMap.getOrDefault(poll, new HashSet<>())) { |
| 165 | +// if (row[next] == 0) continue; |
| 166 | +// row[next]--; |
| 167 | +// if (row[next] == 0) { |
| 168 | +// queue.add(next); |
| 169 | +// } |
| 170 | +// } |
| 171 | +// } |
| 172 | +// for (int i = 0; i < list.size(); i++) { |
| 173 | +// int left = 0; |
| 174 | +// for (int j = 1; j < l.length; j++) { |
| 175 | +// if (l[j] < l[list.get(i)]) { |
| 176 | +// left++; |
| 177 | +// } |
| 178 | +// } |
| 179 | +// res[up++][left] = list.get(i); |
| 180 | +// add++; |
| 181 | +// } |
| 182 | +// } |
| 183 | +// return add == k ? res : new int[][]{}; |
| 184 | +// } |
| 185 | +// |
| 186 | +// int[] help(Map<Integer, Set<Integer>> map, int[] in) { |
| 187 | +// int add = 0; |
| 188 | +// int[] number = new int[in.length]; |
| 189 | +// Queue<Integer> queue = new LinkedList<>(); |
| 190 | +// for (int i = 1; i < in.length; i++) { |
| 191 | +// //没有入度 |
| 192 | +// if (in[i] == 0) { |
| 193 | +// queue.add(i); |
| 194 | +// } |
| 195 | +// } |
| 196 | +// boolean[] visited = new boolean[in.length]; |
| 197 | +// int step = 0; |
| 198 | +// while (!queue.isEmpty()) { |
| 199 | +// int size = queue.size(); |
| 200 | +// while (size-- > 0) { |
| 201 | +// Integer poll = queue.poll(); |
| 202 | +// number[poll] = step; |
| 203 | +// add++; |
| 204 | +// for (Integer next : map.getOrDefault(poll, new HashSet<>())) { |
| 205 | +// if (in[next] == 0) continue; |
| 206 | +// in[next]--; |
| 207 | +// if (in[next] == 0) { |
| 208 | +// queue.add(next); |
| 209 | +// } |
| 210 | +// } |
| 211 | +// step++; |
| 212 | +// } |
| 213 | +// } |
| 214 | +// |
| 215 | +// return add == in.length - 1 ? number : null; |
| 216 | +// } |
| 217 | + |
| 218 | + //代码优化,横竖无关,两次拓扑排序就能唯一确定数字的位置 |
| 219 | + public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) { |
| 220 | + Map<Integer, Integer> row = help(k, rowConditions); |
| 221 | + Map<Integer, Integer> col = help(k, colConditions); |
| 222 | + if (row.size() < k || col.size() < k) { |
| 223 | + return new int[][]{}; |
| 224 | + } |
| 225 | + int[][] res = new int[k][k]; |
| 226 | + for (Map.Entry<Integer, Integer> entry : row.entrySet()) { |
| 227 | + int v = entry.getKey(); |
| 228 | + res[entry.getValue()][col.get(v)] = v; |
| 229 | + } |
| 230 | + return res; |
| 231 | + } |
| 232 | + |
| 233 | + |
| 234 | + Map<Integer, Integer> help(int k, int[][] condition) { |
| 235 | + Map<Integer, List<Integer>> map = new HashMap<>(); |
| 236 | + int[] in = new int[k + 1]; |
| 237 | + for (int[] ints : condition) { |
| 238 | + if (!map.containsKey(ints[0])) { |
| 239 | + map.put(ints[0], new ArrayList<>()); |
| 240 | + } |
| 241 | + map.get(ints[0]).add(ints[1]); |
| 242 | + in[ints[1]]++; |
| 243 | + } |
| 244 | + Queue<Integer> queue = new LinkedList<>(); |
| 245 | + for (int i = 1; i < in.length; i++) { |
| 246 | + if (in[i] == 0) { |
| 247 | + queue.add(i); |
| 248 | + } |
| 249 | + } |
| 250 | + Map<Integer, Integer> res = new HashMap<>(); |
| 251 | + |
| 252 | + int index = 0; |
| 253 | + while (!queue.isEmpty()) { |
| 254 | + Integer poll = queue.poll(); |
| 255 | + res.put(poll, index++); |
| 256 | + for (Integer next : map.getOrDefault(poll, new ArrayList<>())) { |
| 257 | + in[next]--; |
| 258 | + if (in[next] == 0) { |
| 259 | + queue.add(next); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + return res; |
| 264 | + } |
| 265 | + |
| 266 | + public static void main(String[] args) { |
| 267 | + wk308 w = new wk308(); |
| 268 | + w.buildMatrix(3, new int[][]{{1, 2}, {3, 2}}, |
| 269 | + new int[][]{{2, 1}, {3, 1}}); |
| 270 | + } |
| 271 | +} |
0 commit comments