LeetCode 1488. Avoid Flood in The City - Java - 优先队列

30 篇文章 1 订阅
4 篇文章 0 订阅

题目链接: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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值