No.1K base representation of the sum of each digit solution idea Base conversion using the mod operation. The code shown

public int sumBase(int n, int k) { int sum = 0; for (int i = n; i > 0; i /= k) { sum += i % k; } return sum; }}Copy the code

First count the number of occurrences of each value, and then enumerate each value from smallest to largest. In this process, use queues to store the elements to be changed to the current value. The code shown

    public int maxFrequency(int[] nums, int k) {
        final int max = Arrays.stream(nums).max().getAsInt();
        int[] cnt = new int[max + 1];
        for (int num : nums) {
            cnt[num]++;
        }
        LinkedList<Integer> queue = new LinkedList<>();
        int result = 0;
        int cost = 0;
        for (int i = 0; i <= max; i++) {
            cost += queue.size();
            while (cost > k && !queue.isEmpty()) {
                cost -= i - queue.poll();
            }
            if (cost <= k) {
                result = Math.max(queue.size() + cnt[i], result);
            }
            for (int j = 0; j < cnt[i]; j++) {
                queue.add(i);
            }
        }
        return result;
    }
}
Copy the code

Aeiou = aeiou = aeiou = aeiou = aeiou = aeiou = aeiou The code shown

class Solution { public int longestBeautifulSubstring(String word) { StringBuilder sb = new StringBuilder(); List<Integer> count = new ArrayList<>(); int cnt = 1; char c = word.charAt(0); for (int i = 1; i < word.length(); i++) { if (word.charAt(i) == c) { cnt++; continue; } sb.append(c); count.add(cnt); c = word.charAt(i); cnt = 1; } sb.append(c); count.add(cnt); int result = 0; String str = sb.toString(); for (int i = 0; i + 5 <= str.length(); i++) { if ("aeiou".equals(str.substring(i, i + 5))) { int tmp = 0; for (int j = 0; j < 5; j++) { tmp += count.get(i + j); } result = Math.max(result, tmp); } } return result; }}Copy the code

Compute the tallest building between two adjacent height restrictions in order.

Why recalculate? Restrictions may not always be reached. For example, there are [2, 1] and [3, 5], since building 2 is restricted to a maximum of 1, then building 3 can only be a maximum of 2, which is impossible to achieve given [3, 5]. In other words, due to the limitation that the height difference between adjacent buildings is no more than 1, the adjacent height restriction conditions will also affect each other. Enter [[2, 1], [3, 5]] to get [[2, 1], [3, 2]].

The recalculation process is somewhat similar to Dijkstra’s optimized version of the shortest source reactor. After recalculation, some building limits may be lower, but not higher. We can calculate from the lowest limit, each limit may reduce the height of its two adjacent limits. For another example: in [[5, 5], [8, 1], [10, 10]], the height limits of building 5 and 10 will be reduced by the height limits of building 8, and finally get [[5, 4], [8, 1], [10, 3]].

The code shown

Public int maxBuilding(int n, int[][] restrictions) {public int maxBuilding(int n, int[][] Restrictions) [] R = new int[restrictions. Length + 2][2]; R[0] = new int[]{1, 0}; R[restrictions.length + 1] = new int[]{n, n - 1}; Arrays.sort(restrictions, Comparator.comparingInt(a -> a[0])); System. arrayCopy (Restrictions, 0, R, 1, restrictions. Length); // recalculate R[I][1] PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); for (int i = 0; i < R.length; i++) { heap.add(new int[]{i, R[i][1]}); } boolean[] done = new boolean[R.length]; while (! heap.isEmpty()) { int id = heap.poll()[0]; if (done[id]) { continue; } done[id] = true; for (int i = -1; i <= 1; i += 2) { int nei = id + i; // neighbor if (nei < 0 || nei >= R.length) { continue; } int maxHeight = R[id][1] + Math.abs(R[id][0] - R[nei][0]); if (R[nei][1] > maxHeight) { R[nei][1] = maxHeight; heap.add(new int[]{nei, maxHeight}); }} // Calculate the tallest building between two adjacent buildings (including itself) according to the height limit of each building; for (int i = 1; i < R.length; i++) { result = Math.max(result, calc(R[i], R[i - 1])); } return result; } private int calc(int[] a, int[] b) { return Math.max(a[1], b[1]) + (Math.abs(b[0] - a[0]) - Math.abs(b[1] - a[1])) / 2; }}Copy the code

———————————————— Copyright notice: This article is an original article BY CSDN blogger “Shanganya20”. It follows CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement. The original link: blog.csdn.net/shanganya20…