LeetCode 1488. Avoid Flood in The City - Java - 優先隊列

題目鏈接:1488. 避免洪水氾濫

Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake.

Given an integer array rains where:

  • rains[i] > 0 means there will be rains over the rains[i] lake.
  • rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.

Return an array ans where:

  • ans.length == rains.length
  • ans[i] == -1 if rains[i] > 0.
  • ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.

If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.

Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)

Example 1:

Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There’s no day to dry any lake and there is no flood in any lake.

Example 2:

Input: rains = [1,2,0,0,2,1]
Output: [-1,-1,2,1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day, we dry lake 2. Full lakes are [1]
After the fourth day, we dry lake 1. There is no full lakes.
After the fifth day, full lakes are [2].
After the sixth day, full lakes are [1,2].
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.

Example 3:

Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
After that, it will rain over lakes [1,2]. It’s easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.

Example 4:

Input: rains = [69,0,0,0,69]
Output: [-1,69,1,1,-1]
Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9

Example 5:

Input: rains = [10,20,20]
Output: []
Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.

Constraints:
  • 1 <= rains.length <= 10^5
  • 0 <= rains[i] <= 10^9
題解

不考慮代碼,直接面對問題,可以得出此結論。儘可能的先抽乾最有可能發洪水的湖泊。什麼樣的湖泊最先發洪水呢,當然是已經裝滿水並且未來最先下雨的容易發洪水。先排除這些湖泊的隱患不就解決了嗎?

所以需要用到優先隊列,此處用PriorityQueue實現。

Java代碼
/**
 * <p>創建日期:2020-06-21 12:35:10</p>
 */
class Solution {
    public int[] avoidFlood(int[] rains) {
        // 天數
        int days = rains.length;
        Map<Integer, Integer> map = new HashMap<>(days);
        // next[i]=k表示第rains[i]個湖泊下一次下雨在第k天
        int[] next = new int[days];
        // 逆序遍歷每天的下雨情況
        for (int i = days - 1; i >= 0; i--) {
            // 將第rains[i]個湖泊下雨的日期保存到next數組中
            next[i] = map.getOrDefault(rains[i], days);
            // 更新第rains[i]個湖泊的下雨的最近的日期
            map.put(rains[i], i);
        }

        int[] res = new int[days];
        Arrays.fill(res, -1);
        // 裝滿水的湖泊隊列,按照未來最先下雨的順序排列,因爲裝滿水的情況下,先下雨可能發洪水,優先抽乾
        Queue<Lake> full = new PriorityQueue<>(days, Comparator.comparingInt(lake -> lake.nextDay));
        // 順序遍歷每天的下雨情況
        for (int i = 0; i < days; i++) {
            if (rains[i] == 0) {
                // 沒有湖泊下雨
                if (full.isEmpty()) {
                    // 沒有湖泊裝滿水
                    res[i] = 1;
                } else {
                    // 將第k個湖泊抽乾,k爲下面等號右邊的值
                    res[i] = full.poll().index;
                }
            } else {
                // 第rains[i]個湖泊
                Lake lake = new Lake();
                // 湖泊索引
                lake.index = rains[i];
                // 此湖泊未來最近的下雨日期
                lake.nextDay = next[i];
                // 添加到優先隊列中
                full.offer(lake);
            }
            if (!full.isEmpty() && full.peek().nextDay <= i) {
                // 如果有裝滿水的湖泊並且該湖泊在下次下雨之前已經沒有機會抽乾,會發洪水
                return new int[0];
            }
        }
        return res;
    }

    /**
     * 湖泊
     */
    private static class Lake {
        /**
         * 湖泊索引
         */
        int index;
        /**
         * 此湖泊未來最近的下雨日期
         */
        int nextDay;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章