|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.TreeMap; |
| 9 | + |
| 10 | +public class wk358 { |
| 11 | + |
| 12 | + //模拟 |
| 13 | + public int maxSum(int[] nums) { |
| 14 | + int[] help = new int[nums.length]; |
| 15 | + for (int i = 0; i < nums.length; i++) { |
| 16 | + int num = nums[i]; |
| 17 | + int max = 0; |
| 18 | + while (num > 0) { |
| 19 | + max = Math.max(max, num % 10); |
| 20 | + num /= 10; |
| 21 | + } |
| 22 | + help[i] = max; |
| 23 | + } |
| 24 | + |
| 25 | + int ans = -1; |
| 26 | + for (int i = 0; i < nums.length; i++) { |
| 27 | + for (int j = i + 1; j < nums.length; j++) { |
| 28 | + if (help[i] == help[j]) { |
| 29 | + ans = Math.max(ans, nums[i] + nums[j]); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + return ans; |
| 34 | + } |
| 35 | + |
| 36 | + public class ListNode { |
| 37 | + int val; |
| 38 | + ListNode next; |
| 39 | + |
| 40 | + ListNode() { |
| 41 | + } |
| 42 | + |
| 43 | + ListNode(int val) { |
| 44 | + this.val = val; |
| 45 | + } |
| 46 | + |
| 47 | + ListNode(int val, ListNode next) { |
| 48 | + this.val = val; |
| 49 | + this.next = next; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + //链表 |
| 54 | + public ListNode doubleIt(ListNode head) { |
| 55 | + ListNode temp = head; |
| 56 | + ListNode pre = new ListNode(0); |
| 57 | + pre.next = head; |
| 58 | + head = pre; |
| 59 | + while (temp != null) { |
| 60 | + |
| 61 | + int val = temp.val; |
| 62 | + val *= 2; |
| 63 | + |
| 64 | + pre.val += val / 10; |
| 65 | + temp.val = val % 10; |
| 66 | + pre = temp; |
| 67 | + temp = temp.next; |
| 68 | + } |
| 69 | + |
| 70 | + if (head.val == 0) { |
| 71 | + return head.next; |
| 72 | + } else { |
| 73 | + return head; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + //滑动窗口 |
| 79 | + public int minAbsoluteDifference(List<Integer> nums, int x) { |
| 80 | + TreeMap<Integer, Integer> map = new TreeMap<>(); |
| 81 | + |
| 82 | + int ans = Integer.MAX_VALUE; |
| 83 | + for (int i = 0; i < nums.size(); i++) { |
| 84 | + |
| 85 | + if (i >= x) { |
| 86 | + map.put(nums.get(i - x), map.getOrDefault(nums.get(i - x), 0) + 1); |
| 87 | + } |
| 88 | + |
| 89 | + int cur = nums.get(i); |
| 90 | + Integer floorKey = map.floorKey(cur); |
| 91 | + if (floorKey != null) { |
| 92 | + ans = Math.min(ans, Math.abs(floorKey - cur)); |
| 93 | + } |
| 94 | + Integer ceilKey = map.ceilingKey(cur); |
| 95 | + if (ceilKey != null) { |
| 96 | + ans = Math.min(ans, Math.abs(ceilKey - cur)); |
| 97 | + } |
| 98 | + } |
| 99 | + return ans; |
| 100 | + } |
| 101 | + |
| 102 | + public static int countDistinctFactors(int n) { |
| 103 | + int count = 0; |
| 104 | + for (int i = 2; i <= Math.sqrt(n); i++) { |
| 105 | + if (n % i == 0) { |
| 106 | + count++; |
| 107 | + while (n % i == 0) { |
| 108 | + n /= i; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + if (n > 1) { |
| 113 | + count++; |
| 114 | + } |
| 115 | + return count; |
| 116 | + } |
| 117 | + |
| 118 | + static Map<Integer, Integer> map = new HashMap<>(); |
| 119 | + |
| 120 | + static { |
| 121 | + map.put(1, 0); |
| 122 | + for(int i=2;i<=(int)1e5+7;i++){ |
| 123 | + map.put(i, countDistinctFactors(i)); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + //质数分解+单调栈+快速幂 |
| 129 | + public int maximumScore(List<Integer> nums, int k) { |
| 130 | + |
| 131 | + ArrayDeque<int[]> deque = new ArrayDeque<>(); |
| 132 | + Integer[][] dp = new Integer[nums.size()][2]; |
| 133 | + //单调栈求边界 |
| 134 | + for (int i = 0; i < nums.size(); i++) { |
| 135 | + int num = nums.get(i); |
| 136 | + int count = map.get(num); |
| 137 | + while (!deque.isEmpty() && deque.peekLast()[0] < count) { |
| 138 | + int[] ints = deque.pollLast(); |
| 139 | + int l = ints[1] - ints[2]; //左边有多少个 |
| 140 | + int r = i - ints[1]; //右边有多少个 |
| 141 | + dp[ints[1]] = new Integer[]{l * r, nums.get(ints[1])}; |
| 142 | + } |
| 143 | + int left = deque.isEmpty() ? -1 : deque.peekLast()[1]; |
| 144 | + deque.add(new int[]{count, i, left}); |
| 145 | + } |
| 146 | + while (!deque.isEmpty()) { |
| 147 | + int[] ints = deque.pollLast(); |
| 148 | + int l = ints[1] - ints[2]; |
| 149 | + int r = nums.size() - ints[1]; |
| 150 | + dp[ints[1]] = new Integer[]{l * r, nums.get(ints[1])}; |
| 151 | + } |
| 152 | + long ans = 1; |
| 153 | + int mod = (int) 1e9 + 7; |
| 154 | + Arrays.sort(dp, (a, b) -> b[1] - a[1]); |
| 155 | + |
| 156 | + |
| 157 | + //快速幂求幂 |
| 158 | + for (int i = 0; i < dp.length&&k>0; i++) { |
| 159 | + int count = dp[i][0]>k?k:dp[i][0]; |
| 160 | + long l = fastPower(dp[i][1], count) % mod; |
| 161 | + ans *= l; |
| 162 | + ans %= mod; |
| 163 | + k -= count; |
| 164 | + } |
| 165 | + return (int) ans; |
| 166 | + } |
| 167 | + |
| 168 | + public static long fastPower(long a, long b) { |
| 169 | + long ans = 1; |
| 170 | + int mod = (int) 1e9 + 7; |
| 171 | + while (b > 0) { |
| 172 | + if ((b & 1) == 1) { |
| 173 | + ans *= a; |
| 174 | + ans %= mod; |
| 175 | + } |
| 176 | + a *= a; |
| 177 | + a %= mod; |
| 178 | + b >>= 1; |
| 179 | + } |
| 180 | + return ans; |
| 181 | + } |
| 182 | + |
| 183 | + public static void main(String[] args) { |
| 184 | + System.out.println(fastPower(7, 5)); |
| 185 | + |
| 186 | + |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments