|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +public class wk371 { |
| 13 | + //暴力 |
| 14 | + /* public int maximumStrongPairXor(int[] nums) { |
| 15 | + int ans = -1; |
| 16 | + for (int i = 0; i < nums.length; i++) { |
| 17 | + for (int j = 0; j < nums.length; j++) { |
| 18 | + if (Math.abs(nums[i] - nums[j]) <= Math.min(nums[i], nums[j])) { |
| 19 | + ans = Math.max(nums[i] ^ nums[j], ans); |
| 20 | + } |
| 21 | +
|
| 22 | + } |
| 23 | + } |
| 24 | + return ans; |
| 25 | + }*/ |
| 26 | + |
| 27 | + //模拟 |
| 28 | + public List<String> findHighAccessEmployees(List<List<String>> access_times) { |
| 29 | + List<int[]> list = new ArrayList<>(access_times.size()); |
| 30 | + for (int i = 0; i < access_times.size(); i++) { |
| 31 | + list.add(new int[]{i, Integer.parseInt(access_times.get(i).get(1))}); |
| 32 | + } |
| 33 | + |
| 34 | + Collections.sort(list, (a, b) -> a[1] - b[1]); |
| 35 | + |
| 36 | + Map<String, List<Integer>> map = new HashMap<>(); |
| 37 | + for (int i = 0; i < list.size(); i++) { |
| 38 | + String emp = access_times.get(list.get(i)[0]).get(0); |
| 39 | + int time = list.get(i)[1]; |
| 40 | + if (!map.containsKey(emp)) map.put(emp, new ArrayList<>()); |
| 41 | + map.get(emp).add(time); |
| 42 | + } |
| 43 | + |
| 44 | + List<String> res = new ArrayList<>(); |
| 45 | + for (Map.Entry<String, List<Integer>> entry : map.entrySet()) { |
| 46 | + List<Integer> value = entry.getValue(); |
| 47 | + for (int i = 2; i < value.size(); i++) { |
| 48 | + if (help(value.get(i - 2), value.get(i)) <= 60) { |
| 49 | + res.add(entry.getKey()); |
| 50 | + break; |
| 51 | + |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return res; |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + int help(int a, int b) { |
| 61 | + if (a / 100 == b / 100) { |
| 62 | + return b - a; |
| 63 | + } else if ((b / 100) - (a / 100) == 1) { |
| 64 | + return b % 100 + (60 - a % 100); |
| 65 | + } else return Integer.MAX_VALUE; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + //贪心 |
| 70 | + public int minOperations(int[] nums1, int[] nums2) { |
| 71 | + int v1 = help(nums1, nums2, nums1[nums1.length - 1], nums2[nums2.length - 1]); |
| 72 | + int v2 = help(nums1, nums2, nums2[nums1.length - 1], nums1[nums2.length - 1]) + 1; |
| 73 | + int v = Math.min(v1, v2); |
| 74 | + if (v >= (int) 1e9 + 7) return -1; |
| 75 | + return v; |
| 76 | + } |
| 77 | + |
| 78 | + int help(int[] nums1, int[] nums2, int a, int b) { |
| 79 | + int ans = 0; |
| 80 | + for (int i = 0; i < nums1.length - 1; i++) { |
| 81 | + if (a >= nums1[i] && b >= nums2[i]) { |
| 82 | + |
| 83 | + } else if (b >= nums1[i] && a >= nums2[i]) { |
| 84 | + ans++; |
| 85 | + } else { |
| 86 | + return (int) 1e9 + 7; |
| 87 | + } |
| 88 | + } |
| 89 | + return ans; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + public int maximumStrongPairXor(int[] nums) { |
| 94 | + Arrays.sort(nums); |
| 95 | + int left = 0, right = 0; |
| 96 | + int ans = 0; |
| 97 | + Trie t = new Trie(); |
| 98 | + |
| 99 | + for (int i = 0; i < nums.length; i++) { |
| 100 | + t.insert(nums[i]); |
| 101 | + int x = nums[i]; |
| 102 | + while (nums[left] < (x + 1) / 2) { |
| 103 | + t.remove(nums[left]); |
| 104 | + left++; |
| 105 | + } |
| 106 | + ans=Math.max(ans,t.maxXor(nums[i])); |
| 107 | + } |
| 108 | + return ans; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + class Node { |
| 113 | + Node[] children = new Node[2]; |
| 114 | + int cnt; // 子树大小 |
| 115 | + } |
| 116 | + |
| 117 | + class Trie { |
| 118 | + private static final int HIGH_BIT = 19; |
| 119 | + private Node root = new Node(); |
| 120 | + |
| 121 | + // 添加 val |
| 122 | + public void insert(int val) { |
| 123 | + Node cur = root; |
| 124 | + for (int i = HIGH_BIT; i >= 0; i--) { |
| 125 | + int bit = (val >> i) & 1; |
| 126 | + if (cur.children[bit] == null) { |
| 127 | + cur.children[bit] = new Node(); |
| 128 | + } |
| 129 | + cur = cur.children[bit]; |
| 130 | + cur.cnt++; // 维护子树大小 |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // 删除 val,但不删除节点 |
| 135 | + // 要求 val 必须在 trie 中 |
| 136 | + public void remove(int val) { |
| 137 | + Node cur = root; |
| 138 | + for (int i = HIGH_BIT; i >= 0; i--) { |
| 139 | + cur = cur.children[(val >> i) & 1]; |
| 140 | + cur.cnt--; // 维护子树大小 |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // 返回 val 与 trie 中一个元素的最大异或和 |
| 145 | + // 要求 trie 不能为空 |
| 146 | + public int maxXor(int val) { |
| 147 | + Node cur = root; |
| 148 | + int ans = 0; |
| 149 | + for (int i = HIGH_BIT; i >= 0; i--) { |
| 150 | + int bit = (val >> i) & 1; |
| 151 | + // 如果 cur.children[bit^1].cnt == 0,视作空节点 |
| 152 | + // 判断有没有相异的节点 |
| 153 | + if (cur.children[bit ^ 1] != null && cur.children[bit ^ 1].cnt > 0) { |
| 154 | + ans |= 1 << i; //相异直接+1即可 |
| 155 | + bit ^= 1; |
| 156 | + } |
| 157 | + //这里省略了相同不加 |
| 158 | + cur = cur.children[bit]; |
| 159 | + } |
| 160 | + return ans; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments