算法打卡第六周

leetcode1962题动物收容所
题目描述:
有家动物收容所只收容狗与猫,且严格遵守“先进先出”的原则。在收养该收容所的动物时,收养人只能收养所有动物中“最老”(由其进入收容所的时间长短而定)的动物,或者可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,收养人不能自由挑选想收养的对象。请创建适用于这个系统的数据结构,实现各种操作方法,比如enqueue、dequeueAny、dequeueDog和dequeueCat。允许使用Java内置的LinkedList数据结构。enqueue方法有一个animal参数,animal[0]代表动物编号,animal[1]代表动物种类,其中 0 代表猫,1 代表狗。dequeue*方法返回一个列表[动物编号, 动物种类],若没有可以收养的动物,则返回[-1,-1]。

/**
* 动物收容所
* @Author: jxy
* @Date: 2021/4/17 23:12
*/
public class AnimalShelf {
    private LinkedList<int[]> animalShelfList =new LinkedList<>();
    public AnimalShelf() {
    }

    public void enqueue(int[] animal) {
        animalShelfList.add(animal);
    }

    public int[] dequeueAny() {
        if (animalShelfList.size() == 0) {
            return new int[]{-1,-1};
        }else {
            return animalShelfList.pollFirst();
        }
    }
    
    public int[] dequeueDog() {
       return dequeueAni(1);
    }


    public int[] dequeueCat() {
        return dequeueAni(0);
    }
    
    private int[] dequeueAni(int type) {
        int[] result =  new int[]{-1,-1};;
        if (animalShelfList.size() == 0) {
            return result;
        }else {
            int index = -1;
            for (int i =0;i<animalShelfList.size();i++) {
                int[] arr = animalShelfList.get(i);
                if (arr[1] == type) {
                    result = arr;
                    index= i;
                    break;
                }
            }
            if (index!=-1) {
                animalShelfList.remove(result[0]);
            }
        }
        return result;
    }
}

leetcode第20题接雨水
题目描述
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水。

/**
 *栈 
 *时间复杂度O(n)  
 *空间复杂度O(n)
 */
public class GetRainsV2 {
    public int trap(int[] height) {
         // 使用递减单调栈
        Stack<Integer> stack = new Stack<>();
        int rains =0,current = 0;
        while (current < height.length) {
            while (!stack.isEmpty() && height[current] > height[stack.peek()]) {
                Integer pop = stack.pop();
                if (stack.isEmpty()) {
                    break;
                }
                //计算水洼的宽度
                int width = current - stack.peek() -1;
                //计算水洼的高度
                int h = Math.min(height[current],height[stack.peek()]) - height[pop];
                rains += width*h;
            }
            stack.push(current++);
        }
        return rains;
    }
}
/**
*动态规划 找出一个柱子左右两边最高的柱子
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
public class GetRains {

    public int trap(int[] height) {
        int rains = 0;
        if (height.length == 0) {
            return rains;
        }
        int[] leftMaxArr = new int[height.length];
        int[] rightMaxArr = new int[height.length];
        int leftMax = 0;
        int rightMax = 0;
        for (int i = 0; i < height.length; i++) {
            if (height[i] > leftMax) {
                leftMaxArr[i] = height[i];
                leftMax = height[i];
            } else {
                leftMaxArr[i] = leftMax;
            }
        }


        for (int i = height.length - 1; i >= 0; i--) {
            if (height[i] > rightMax) {
                rightMaxArr[i] = height[i];
                rightMax = height[i];
            } else {
                rightMaxArr[i] = rightMax;
            }
        }
        for (int i = 0; i < height.length - 1; i++) {
            int shortWall = leftMaxArr[i] > rightMaxArr[i] ? rightMaxArr[i] : leftMaxArr[i];
            if (shortWall > height[i]) {
                rains += (shortWall - height[i]);
            }
        }
        return rains;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值