Week4 algo 打卡

111 阅读2分钟

接水滴


/**
 * 给定 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 个单位的雨水(蓝色部分表示雨水)
 *
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/trapping-rain-water
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 */
public class Trap {
    public static void main(String[] args) {
        Trap trap = new Trap();
        int[] height ={0,1,0,2,1,0,1,3,2,1,2,1};
        int trap1 = trap.trap(height);
        System.out.println(trap1);
    }

    /**
     * 暴力解法
     * @param height
     * @return
     */
    public int trap(int[] height) {
        int ans = 0;
        int size = height.length;
        for (int i = 1; i < size - 1; i++) {
            int max_left = 0, max_right = 0;
            for (int j = i; j >= 0; j--) {
                //Search the left part for max bar size
                max_left = Math.max(max_left, height[j]);
            }
            for (int j = i; j < size; j++) {
                //Search the right part for max bar size
                max_right = Math.max(max_right, height[j]);
            }
            ans += Math.min(max_left, max_right) - height[i];
        }
        return ans;
    }

    /**
     * 双指针解法
     * @param height
     * @return
     */
    public int trap1(int[] height) {
        // 维护下标 定义双指针
        int left = 0, right = height.length - 1;
        int ans = 0;
        int left_max = 0, right_max = 0;
        // 循环控制下标
        while (left < right) {
                //左边小于右边
            if (height[left] < height[right]) {
                if (height[left] >= left_max) {
                    left_max = height[left];
                } else {
                    ans += (left_max - height[left]);
                }
                ++left;
            } else {
                if (height[right] >= right_max) {
                    right_max = height[right];
                } else {
                    ans += (right_max - height[right]);
                }
                --right;
            }
        }
        return ans;
    }
}


动物收容所

package com.stan.algo.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 动物收容所。有家动物收容所只收容狗与猫,且严格遵守“先进先出”的原则。在收养该收容所的动物时,
 * 收养人只能收养所有动物中“最老”(由其进入收容所的时间长短而定)的动物,
 * 或者可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,
 * 收养人不能自由挑选想收养的对象。请创建适用于这个系统的数据结构,
 * 实现各种操作方法,比如enqueue、dequeueAny、dequeueDog和dequeueCat。允许使用Java内置的LinkedList数据结构。
 * <p>
 * enqueue方法有一个animal参数,animal[0]代表动物编号,animal[1]代表动物种类,其中 0 代表猫,1 代表狗。
 * <p>
 * 输入:
 * ["AnimalShelf", "enqueue", "enqueue", "dequeueCat", "dequeueDog", "dequeueAny"]
 * [[], [[0, 0]], [[1, 0]], [], [], []]
 * 输出:
 * [null,null,null,[0,0],[-1,-1],[1,0]]
 * <p>
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/animal-shelter-lcci
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 */
public class AnimalShelf {
    Queue<int[]> queue;

    public AnimalShelf() {
         queue = new LinkedList<>();
    }

    public void enqueue(int[] animal) {
        queue.offer(animal);
    }

    public int[] dequeueAny() {
        if (queue.isEmpty()) {
            return new int[]{-1, -1};
        } else {
            return queue.poll();
        }
    }

    public int[] dequeueDog() {
        for (int[] animal : queue) {
            if (animal[1] == 1) {
                queue.remove(animal);
                return animal;
            }
        }
        return new int[]{-1, -1};
    }

    public int[] dequeueCat() {
        for (int[] animal : queue) {
            if (animal[1] == 0) {
                queue.remove(animal);
                return animal;
            }
        }
        return new int[]{-1, -1};
    }
}