|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +public class wkb107 { |
| 10 | + |
| 11 | + //暴力 |
| 12 | + //字符串长度是2,可以不用这么麻烦 |
| 13 | + public int maximumNumberOfStringPairs(String[] words) { |
| 14 | + int ans = 0; |
| 15 | + Set<Integer> set = new HashSet<>(); |
| 16 | + for (int i = 0; i < words.length; i++) { |
| 17 | + if (set.contains(i)) continue; |
| 18 | + for (int j = i + 1; j < words.length; j++) { |
| 19 | + if (set.contains(j)) continue; |
| 20 | + String word = words[j]; |
| 21 | + String rs = new StringBuilder(word).reverse().toString(); |
| 22 | + if (words[i].equals(rs)) { |
| 23 | + ans++; |
| 24 | + set.add(i); |
| 25 | + set.add(j); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + return ans; |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + //找规律 |
| 34 | + public int longestString(int x, int y, int z) { |
| 35 | + int ans = 0; |
| 36 | + int min = Math.min(x, y); |
| 37 | + |
| 38 | + |
| 39 | + //按照 min*(AA+BB)+z*AB 拼接 |
| 40 | + |
| 41 | + ans += 4 * min + z * 2; |
| 42 | + |
| 43 | + //剩下的 AA或BB |
| 44 | + int left = Math.max(x, y) - min; |
| 45 | + boolean flag = true; |
| 46 | + |
| 47 | + //若之前有AA或BB 且 还有剩下的,那么就可以把AA放在最后或者把BB放在最前面 |
| 48 | + if (x == 0 || y == 0) { |
| 49 | + flag = false; |
| 50 | + } |
| 51 | + if (left > 0 && flag) { |
| 52 | + ans += 2; |
| 53 | + } |
| 54 | + return ans; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + //记忆化搜索 |
| 59 | + public int minimizeConcatenatedLength(String[] words) { |
| 60 | + memo = new int[words.length][26 * 26]; |
| 61 | + for (int[] ints : memo) { |
| 62 | + Arrays.fill(ints, -1); |
| 63 | + } |
| 64 | + return help(words[0].charAt(0), words[0].charAt(words[0].length() - 1), 1, words) + words[0].length(); |
| 65 | + } |
| 66 | + |
| 67 | + int[][] memo; |
| 68 | + |
| 69 | + int help(char left, char right, int index, String[] words) { |
| 70 | + if (index >= words.length) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + int key = (left - 'a') * 26 + (right - 'a'); |
| 74 | + |
| 75 | + if (memo[index][key] != -1) { |
| 76 | + return memo[index][key]; |
| 77 | + } |
| 78 | + |
| 79 | + String word = words[index]; |
| 80 | + int len = Integer.MAX_VALUE; |
| 81 | + //放在左边 |
| 82 | + int l = 0; |
| 83 | + if (word.charAt(word.length() - 1) == left) { |
| 84 | + l = 1; |
| 85 | + } |
| 86 | + len = Math.min(help(word.charAt(0), right, index + 1, words) + word.length() - l, len); |
| 87 | + |
| 88 | + //放在右边 |
| 89 | + int r = 0; |
| 90 | + if (word.charAt(0) == right) { |
| 91 | + r = 1; |
| 92 | + } |
| 93 | + len = Math.min(len, help(left, word.charAt(word.length() - 1), index + 1, words) + word.length() - r); |
| 94 | + |
| 95 | + memo[index][key] = len; |
| 96 | + return len; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + //滑动窗口 |
| 103 | + static public int[] countServers(int n, int[][] logs, int x, int[] queries) { |
| 104 | + int[][] qq = new int[queries.length][2]; |
| 105 | + for (int i = 0; i < queries.length; i++) { |
| 106 | + qq[i] = new int[]{queries[i] - x, queries[i], i}; |
| 107 | + } |
| 108 | + Arrays.sort(qq, (a, b) -> a[0] - b[0]); |
| 109 | + Arrays.sort(logs, (a, b) -> a[1] - b[1]); |
| 110 | + |
| 111 | + int right=0; |
| 112 | + int left=0; |
| 113 | + Map<Integer,Integer> counter=new HashMap<>(); |
| 114 | + int []res=new int[queries.length]; |
| 115 | + for (int i = 0; i < qq.length; i++) { |
| 116 | + int l=qq[i][0]; |
| 117 | + int r=qq[i][1]; |
| 118 | + |
| 119 | + //向右滑到查询的截止时间 |
| 120 | + while (right<logs.length&&logs[right][1]<=r){ |
| 121 | + counter.put(logs[right][0],counter.getOrDefault(logs[right][0],0)+1); |
| 122 | + right++; |
| 123 | + } |
| 124 | + |
| 125 | + //左边开始滑动到查询的开始时间 |
| 126 | + while (left<logs.length&&logs[left][1]<l){ |
| 127 | + Integer count = counter.get(logs[left][0]); |
| 128 | + if(count==1){ |
| 129 | + counter.remove(logs[left][0]); |
| 130 | + }else { |
| 131 | + counter.put(logs[left][0],count-1); |
| 132 | + |
| 133 | + } |
| 134 | + left++; |
| 135 | + } |
| 136 | + |
| 137 | + res[qq[i][2]]=n-counter.size(); |
| 138 | + } |
| 139 | + return res; |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + public static void main(String[] args) { |
| 144 | + countServers(3,new int[][]{ |
| 145 | + {1,3},{2,6},{1,5} |
| 146 | + },5,new int[]{10,11}); |
| 147 | + } |
| 148 | +} |
0 commit comments