|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.Comparator; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.PriorityQueue; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.TreeMap; |
| 14 | + |
| 15 | +public class wk331 { |
| 16 | + public long pickGifts(int[] gifts, int k) { |
| 17 | + PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder()); |
| 18 | + for (int gift : gifts) { |
| 19 | + priorityQueue.add(gift); |
| 20 | + } |
| 21 | + while (k-- > 0 && !priorityQueue.isEmpty()) { |
| 22 | + Integer poll = priorityQueue.poll(); |
| 23 | + priorityQueue.add((int) Math.sqrt(poll)); |
| 24 | + } |
| 25 | + long res = 0; |
| 26 | + while (!priorityQueue.isEmpty()) { |
| 27 | + res += priorityQueue.poll(); |
| 28 | + } |
| 29 | + return res; |
| 30 | + } |
| 31 | + |
| 32 | + public int[] vowelStrings(String[] words, int[][] queries) { |
| 33 | + Set<Character> set = new HashSet<>(); |
| 34 | + set.add('a'); |
| 35 | + set.add('e'); |
| 36 | + set.add('i'); |
| 37 | + set.add('o'); |
| 38 | + set.add('u'); |
| 39 | + int[] count = new int[words.length + 1]; |
| 40 | + for (int i = 0; i < words.length; i++) { |
| 41 | + if (set.contains(words[i].charAt(0)) && set.contains(words[i].charAt(words[i].length() - 1))) { |
| 42 | + count[i + 1]++; |
| 43 | + } |
| 44 | + } |
| 45 | + for (int i = 1; i < count.length; i++) { |
| 46 | + count[i] += count[i - 1]; |
| 47 | + } |
| 48 | + int[] res = new int[queries.length]; |
| 49 | + for (int i = 0; i < queries.length; i++) { |
| 50 | + res[i] = count[queries[i][1] + 1] - count[queries[i][0]]; |
| 51 | + } |
| 52 | + return res; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /*public int minCapability(int[] nums, int k) { |
| 57 | + int[] dp = new int[nums.length]; |
| 58 | +
|
| 59 | +
|
| 60 | + dp[0]=nums[0]; |
| 61 | + for (int i = 1; i < nums.length; i++) { |
| 62 | + dp[i]=Math.min(dp[i-1],nums[i]); |
| 63 | + } |
| 64 | +
|
| 65 | + for (int i = 2; i <= k; i++) { |
| 66 | + int []ndp=new int[nums.length]; |
| 67 | + for (int j = 0; j < nums.length; j++) { |
| 68 | + //偷这个房间 |
| 69 | + ndp[j] =Math.max(nums[j] , (j - 2 >= 0 ? dp[j - 2] : Integer.MAX_VALUE)); |
| 70 | + //不偷这个房间 |
| 71 | + ndp[j] = Math.min(j - 1 >= 0 ? ndp[j - 1] : Integer.MAX_VALUE, ndp[j]); |
| 72 | + } |
| 73 | + dp=ndp; |
| 74 | + } |
| 75 | + return dp[nums.length - 1]; |
| 76 | + }*/ |
| 77 | + |
| 78 | + /*public int minCapability(int[] nums, int k) { |
| 79 | + PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 80 | + for (int i = 0; i < nums.length; i++) { |
| 81 | + priorityQueue.add(new int[]{nums[i], i}); |
| 82 | + } |
| 83 | + Set<Integer> set = new HashSet<>(); |
| 84 | + int max = 0; |
| 85 | + while (!priorityQueue.isEmpty()) { |
| 86 | + int[] poll = priorityQueue.poll(); |
| 87 | + if (!set.contains(poll[1] - 1) && !set.contains(poll[1] + 1)) { |
| 88 | + max = poll[0]; |
| 89 | + set.add(poll[1]); |
| 90 | + k--; |
| 91 | + if (k == 0) return max; |
| 92 | + } |
| 93 | + } |
| 94 | + return max; |
| 95 | + }*/ |
| 96 | + |
| 97 | + public int minCapability(int[] nums, int k) { |
| 98 | + int left = nums[0], right = nums[0]; |
| 99 | + for (int num : nums) { |
| 100 | + left = Math.min(left, num); |
| 101 | + right = Math.max(right, num); |
| 102 | + } |
| 103 | + while (left < right) { |
| 104 | + int mid = (left + right) / 2; |
| 105 | + if (check(mid, nums, k)) { |
| 106 | + right = mid; |
| 107 | + } else { |
| 108 | + left = mid + 1; |
| 109 | + } |
| 110 | + } |
| 111 | + return left; |
| 112 | + } |
| 113 | + |
| 114 | + boolean check(int max, int[] nums, int k) { |
| 115 | + int no = -1; |
| 116 | + int count = 0; |
| 117 | + for (int i = 0; i < nums.length; i++) { |
| 118 | + if (nums[i] <= max && i != no) { |
| 119 | + count++; |
| 120 | + no = i + 1; |
| 121 | + } |
| 122 | + } |
| 123 | + return count >= k; |
| 124 | + } |
| 125 | + |
| 126 | + public static void main(String[] args) { |
| 127 | + wk331 w = new wk331(); |
| 128 | + w.minCost(new int[]{4, 2, 2, 2}, new int[]{1, 4, 1, 2}); |
| 129 | + } |
| 130 | + |
| 131 | + int[] mapToInt(TreeMap<Integer, Integer> map1) { |
| 132 | + int[] res = new int[map1.size()]; |
| 133 | + int i = 0; |
| 134 | + for (Map.Entry<Integer, Integer> entry : map1.entrySet()) { |
| 135 | + res[i++] = entry.getKey(); |
| 136 | + } |
| 137 | + return res; |
| 138 | + } |
| 139 | + |
| 140 | + public long minCost(int[] basket1, int[] basket2) { |
| 141 | + TreeMap<Integer, Integer> map1 = new TreeMap<>(); |
| 142 | + TreeMap<Integer, Integer> map2 = new TreeMap<>(); |
| 143 | + int min = Integer.MAX_VALUE; |
| 144 | + |
| 145 | + for (int i : basket1) { |
| 146 | + min = Math.min(min, i); |
| 147 | + map1.put(i, map1.getOrDefault(i, 0) + 1); |
| 148 | + } |
| 149 | + for (int i : basket2) { |
| 150 | + min = Math.min(min, i); |
| 151 | + map2.put(i, map2.getOrDefault(i, 0) + 1); |
| 152 | + } |
| 153 | + //消去相同的 |
| 154 | + for (int key : mapToInt(map1)) { |
| 155 | + int count1 = map1.get(key); |
| 156 | + int count2 = map2.getOrDefault(key, 0); |
| 157 | + if (count1 > count2) { |
| 158 | + map2.remove(key); |
| 159 | + map1.put(key, count1 - count2); |
| 160 | + } else if (count2 > count1) { |
| 161 | + map1.remove(key); |
| 162 | + map2.put(key, count2 - count1); |
| 163 | + } else { |
| 164 | + map1.remove(key); |
| 165 | + map2.remove(key); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + long res = 0; |
| 170 | + //再展开,只需要存一半值即可 |
| 171 | + List<Integer> list1 = new ArrayList<>(); |
| 172 | + for (Map.Entry<Integer, Integer> entry : map1.entrySet()) { |
| 173 | + if(entry.getValue()%2!=0) return -1; |
| 174 | + for (int i = 0; i < entry.getValue()/2; i++) { |
| 175 | + list1.add(entry.getKey()); |
| 176 | + } |
| 177 | + } |
| 178 | + List<Integer> list2 = new ArrayList<>(); |
| 179 | + for (Map.Entry<Integer, Integer> entry : map2.entrySet()) { |
| 180 | + if(entry.getValue()%2!=0) return -1; |
| 181 | + for (int i = 0; i < entry.getValue()/2; i++) { |
| 182 | + list2.add(entry.getKey()); |
| 183 | + } |
| 184 | + } |
| 185 | + //遍历求最小 |
| 186 | + for (int i = 0; i < list1.size(); i++) { |
| 187 | + res+=Math.min(2*min,Math.min(list1.get(i),list2.get(list2.size()-i-1))); |
| 188 | + } |
| 189 | + |
| 190 | + return res; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | + |
0 commit comments