「力扣」第 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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章