「力扣」第 193 场周赛前 3 题代码(照例不做第 4 题)

说明:第 4 题不做是因为超纲不会做,本人非科班,平常事情多,没有时间去研究第 4 题。

第 193 场周赛地址:https://leetcode-cn.com/contest/weekly-contest-193/

第 1 题:一维数组的动态和

  • 一句话题解:其实就是求前缀和,注意有 1 个位置的下标偏移。

  • 题目链接

Java 代码:

import java.util.Arrays;

public class Solution {

    public int[] runningSum(int[] nums) {
        int len = nums.length;
        int[] preSum = new int[len + 1];

        for (int i = 0; i < len; i++) {
            preSum[i + 1] = preSum[i] + nums[i];
        }

        int[] res = new int[len];
        System.arraycopy(preSum, 1, res, 0, len);
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{1, 2, 3, 4};
        int[] res = solution.runningSum(nums);
        System.out.println(Arrays.toString(res));
    }
}

第 2 题:不同整数的最少数目

  • 一句话题解:有贪心的思想,先移除次数出现少的整数。

  • 题目链接

Java 代码:

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;

public class Solution {

    public int findLeastNumOfUniqueInts(int[] arr, int k) {
        int len = arr.length;

        Map<Integer, Integer> freq = new HashMap<>();
        for (int num : arr) {
            freq.put(num, freq.getOrDefault(num, 0) + 1);
        }

        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        Set<Map.Entry<Integer, Integer>> entries = freq.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            minHeap.add(entry.getValue());
        }

        while (!minHeap.isEmpty()) {
            Integer head = minHeap.peek();
            if (k >= head) {
                k -= head;
                minHeap.poll();
            } else {
                break;
            }
        }
        return minHeap.size();
    }


    public static void main(String[] args) {
//        int[] arr = {5, 5, 4};
//        int k = 1;

        int[] arr = {4,3,1,1,3,3,2};
        int k = 3;
        Solution solution = new Solution();
        int res = solution.findLeastNumOfUniqueInts(arr, k);
        System.out.println(res);
    }
}

第 3 题:制作 m 束花所需的最少天数

  • 一句话题解:天数是一个整数,可以二分去做。

  • 题目链接

  • 参考题解,说明:这篇题解里有和今天这道问题类似的两个问题的题解链接。

所以今天第 3 题 = 「力扣」第 410 题 = 小张刷题计划 ,嘻嘻嘻。

Java 代码:

public class Solution {

    public int minDays(int[] bloomDay, int m, int k) {
        int len = bloomDay.length;
        if (m * k > len) {
            return -1;
        }

        // 二分等待天数 waitingDays
        int left = 0;
        int right = 0;

        for (int day : bloomDay) {
            right = Math.max(right, day);
        }
        while (left < right) {
            int mid = left + (right - left) / 2;
            // 找大于等于 m 的最少天数
            int currentWaitingDays = calculateDays(bloomDay, mid, k);
            // System.out.println("mid => " + mid + " 结果 => " + currentWaitingDays);
            if (currentWaitingDays < m) {
                // 小于 m 的时候,一定不是解
                // 下一轮搜索区间是 [mid + 1, right]
                left = mid + 1;
            } else {
                // 下一轮搜索区间是 [left, mid]
                right = mid;
            }
        }
        return left;

    }

    /**
     * 可以制作多少束花
     *
     * @param bloomDay
     * @param waitingDays
     * @param k
     * @return
     */
    private int calculateDays(int[] bloomDay, int waitingDays, int k) {
        int currentCount = 0;
        int days = 0;

        for (int needDays : bloomDay) {
            if (needDays <= waitingDays) {
                currentCount++;
            } else {
                currentCount = 0;
                continue;
            }

            if (currentCount == k) {
                days++;
                currentCount = 0;
            }
        }
        return days;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
//        int[] bloomDay = new int[]{1, 10, 3, 10, 2};
//        int m = 3;
//        int k = 1;

        int[] bloomDay = new int[]{1, 10, 3, 10, 2};
        int m = 3;
        int k = 2;

//        int[] bloomDay = new int[]{7, 7, 7, 7, 12, 7, 7};
//        int m = 2;
//        int k = 3;

//        int[] bloomDay = new int[]{1000000000, 1000000000};
//        int m = 1;
//        int k = 1;
        int res = solution.minDays(bloomDay, m, k);
        System.out.println(res);
    }
}

你可能感兴趣的:(力扣)