【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: IsOneBitCharacter
 * @Author: xiaof
 * @Description: TODO 717. 1-bit and 2-bit Characters
 * We have two special characters. The first character can be represented by one bit 0.
 * The second character can be represented by two bits (10 or 11).
 * Now given a string represented by several bits.
 * Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
 *
 * Input:
 * bits = [1, 0, 0]
 * Output: True
 * Explanation:
 * The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
 *
 * 有两种字符,一种是0,一种是10或者11,现在要判断整个数组是否由这两种组成的,要求最后一位的数字必须是单个的0.
 * @Date: 2019/7/10 8:54
 * @Version: 1.0
 */
public class IsOneBitCharacter {

    public boolean solution(int[] bits) {
        //必须最后一个是单个0,中间是10或者11,那么一定是奇数,然后最后一个必须是0
        if(bits[bits.length - 1] != 0) {
            return false;
        }
        //遍历获取结果,每次遍历两个
        for(int i = 0; i < bits.length - 1;) {
            if(bits[i] == 1 && (bits[i + 1] == 0 || bits[i + 1] == 1) && i < bits.length - 2) {
                i += 2;
            } else if (bits[i] == 0) {
                i += 1;
            } else {
                return false;
            }
        }

        return true;
    }

    public static void main(String args[]) {
//        int[] A = {1,0,0};
//        int[] A = {0,0};
        int[] A = {1,1,1,0};
        System.out.println(new IsOneBitCharacter().solution(A));
    }

}

 

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: ImageSmoother
 * @Author: xiaof
 * @Description: TODO 661. Image Smoother
 * Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray
 * scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.
 * If a cell has less than 8 surrounding cells, then use as many as you can.
 *
 * Input:
 * [[1,1,1],
 *  [1,0,1],
 *  [1,1,1]]
 * Output:
 * [[0, 0, 0],
 *  [0, 0, 0],
 *  [0, 0, 0]]
 * Explanation:
 * For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
 * For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
 * For the point (1,1): floor(8/9) = floor(0.88888889) = 0
 *
 * 包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,
 * 平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们
 *
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/image-smoother
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Date: 2019/7/10 9:27
 * @Version: 1.0
 */
public class ImageSmoother {

    public int[][] solution(int[][] M) {
        int[][] result = new int[M.length][M[0].length];
        //直接暴力求解
        for(int i = 0; i < M.length; ++i) {
            for(int j = 0; j < M[i].length; ++j) {
                //8个值得位置
                int minUp = i > 0 ? i - 1 : 0;
                int maxDown = i < M.length - 1 ? i + 1 : M.length - 1;
                int minLeft = j > 0 ? j - 1 : 0;
                int maxRight = j < M[i].length - 1 ? j + 1 : M[i].length - 1;
                int countT = 0, countN = 0;
                //获取所有的数据的平均值
                for(int r = minUp; r <= maxDown; ++r) {
                    for(int c = minLeft; c <= maxRight; ++c) {
                        countN++;
                        countT += M[r][c];
                    }
                }

                result[i][j] = countT / countN;
            }
        }

        return result;
    }

}

 

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: MinCostClimbingStairs
 * @Author: xiaof
 * @Description: TODO 746. Min Cost Climbing Stairs
 * On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
 * Once you pay the cost, you can either climb one or two steps.
 * You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0,
 * or the step with index 1.
 *
 * Input: cost = [10, 15, 20]
 * Output: 15
 * Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
 *
 * 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始)。
 * 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯。
 * 您需要找到达到楼层顶部的最低花费。在开始时,你可以选择从索引为 0 或 1 的元素作为初始阶梯。
 *
 * Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
 * Output: 6
 * Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
 * 1(1) + 1(2)
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/min-cost-climbing-stairs
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Date: 2019/7/10 9:42
 * @Version: 1.0
 */
public class MinCostClimbingStairs {

    public int solution(int[] cost) {
        //因为每次可以爬一个楼梯,或者2个楼梯,那么dp一维数组
        //爬到n层需要前面MIN{(n-1) + 当前层, 或者n-2 到达当前层,可以最后一层直接跳过
        int[] dp = new int[cost.length + 1];
        int index = 0;
        //可以第一步走一层,或者2层
        dp[0] = cost[0]; dp[1] = cost[1];
        for(int i = 2; i < cost.length + 1; ++i) {
            dp[i] = Math.min(dp[i - 1], dp[i - 2]);
            if(i < cost.length)
                dp[i] += cost[i];
        }

        return Math.min(dp[cost.length - 1], dp[cost.length]);
    }

    public static void main(String args[]) {
//        int[] A = {1,0,0};
//        int[] A = {0,0};
        int[] A = {0,0,0,1};
        System.out.println(new MinCostClimbingStairs().solution(A));
    }

}

 

package y2019.Algorithm.array;

/**
 * @ClassName MaximumProduct
 * @Description TODO  628. Maximum Product of Three Numbers
 *
 * Given an integer array, find three numbers whose product is maximum and output the maximum product.
 *
 * Input: [1,2,3]
 * Output: 6
 *
 * 给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。
 *
 * @Author xiaof
 * @Date 2019/7/10 22:29
 * @Version 1.0
 **/
public class MaximumProduct {

    public int solution(int[] nums) {
        //最大的三个数,除了最大的三个数相乘之外还要考虑一下负数,那么就是2个负数乘以一个正数,那么必须是最小的两个数乘最大的那个数
        Integer[] maxThreeNum = new Integer[3];
        Integer[] minTwoNum = new Integer[2];
        for(int i = 0; i < maxThreeNum.length; ++i) {
            maxThreeNum[i] = null;
        }
        //先值为空,然后遍历
        for(int i = 0; i < nums.length; ++i) {
            //依次和max数组上的数据比较,然后依次吧数组的数据进行调整
            int index = -1; //填入的位置
            for(int j = 0; j < 3; ++j) {
                if(maxThreeNum[j] == null || nums[i] > maxThreeNum[j]) {
                    ++index;
                } else {
                    break;
                }
            }

            if(index > -1) {
                //修改位置
                for(int k = 0; k < index; ++k) {
                    //前面几位从新排序
                    maxThreeNum[k] = maxThreeNum[k+1];
                }
                maxThreeNum[index] = nums[i];
            }

            //计算最小的两个数
            int minIndex = 2;
            for(int j = 1; j >= 0; --j) {
                if(minTwoNum[j] == null || nums[i] < minTwoNum[j]) {
                    --minIndex;
                } else {
                    break;
                }
            }

            if(minIndex < 2) {
                //移动位置
                for(int k = 1; k > minIndex; --k) {
                    minTwoNum[k] = minTwoNum[k - 1];
                }
                minTwoNum[minIndex] = nums[i];
            }
        }

        //最大三个数的乘积
        return Math.max(maxThreeNum[0] * maxThreeNum[1] * maxThreeNum[2], minTwoNum[0] * minTwoNum[1] * maxThreeNum[2]);

    }

    public static void main(String args[]) {
//        int[] A = {1,0,0};
//        int[] A = {0,0};
        int[] A = {-4,-3,-2,-1,60};
        int[] B = {-1,-2,-3};
        System.out.println(new MaximumProduct().solution(B));
    }

}

 

package y2019.Algorithm.array;

/**
 * @ClassName FindMaxAverage
 * @Description TODO 643. Maximum Average Subarray I
 *
 * Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.
 * Example 1:
 * Input: [1,12,-5,-6,50,3], k = 4
 * Output: 12.75
 * Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
 *
 * Note:
 * 1 <= k <= n <= 30,000.
 * Elements of the given array will be in the range [-10,000, 10,000].
 *
 * 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。
 *
 * @Author xiaof
 * @Date 2019/7/10 23:12
 * @Version 1.0
 **/
public class FindMaxAverage {
    public double solution(int[] nums, int k) {
        //每次统计k个数,然后从第k个开始,没多遍历一个数就减去前面一个数
        int sum = 0;
        int result = 0;
        for(int i = 0; i < k; ++i) {
            sum += nums[i];
            result = sum;
        }

        for(int i = k; i < nums.length; ++i) {
            int cur = sum + nums[i] - nums[i - k];
            result = Math.max(cur, result);
            sum = cur;
        }

        return result / (k * 1.0);
    }

    public static void main(String args[]) {
        int[] A = {0,4,0,3,2};
        int[] B = {5};
        int k = 1;
        System.out.println(new FindMaxAverage().solution(B, k));
    }

}

 

package y2019.Algorithm.array;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MaxDistToClosest
 * @Description TODO 849. Maximize Distance to Closest Person
 *
 * In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.
 * There is at least one empty seat, and at least one person sitting.
 * Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
 * Return that maximum distance to closest person.
 * Example 1:
 * Input: [1,0,0,0,1,0,1]
 * Output: 2
 * Explanation:
 * If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
 * If Alex sits in any other open seat, the closest person has distance 1.
 * Thus, the maximum distance to the closest person is 2.
 *
 * 在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的。
 * 至少有一个空座位,且至少有一人坐在座位上。
 * 亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
 * 返回他到离他最近的人的最大距离。
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/maximize-distance-to-closest-person
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Author xiaof
 * @Date 2019/7/10 23:27
 * @Version 1.0
 **/
public class MaxDistToClosest {
    public int solution(int[] seats) {
        //说白了就是求间距最大的中间位置,也就是连续0最长的子串
        int result = 0, n = seats.length, lastNull = -1;
        for(int i = 0; i < seats.length; ++i) {
            if(seats[i] == 1) {
                //遇到人,计算上一个位置到当前位置的空格最大值
                //如果小于0,那就是第一次,也就是可以做开头位置
                result = lastNull < 0 ? i : Math.max((i - lastNull) / 2, result);
                lastNull = i; //遇到人,记录目前最后一次遇到的人的时候
            }
        }

        //判断最后一个位置
        result = Math.max(result, n - lastNull - 1);

        return result;
    }

    public static void main(String args[]) {
        int[] A = {1,0,0,0,1,0,1};
        int[] B = {5};
        int k = 1;
        System.out.println(new MaxDistToClosest().solution(A));
    }
}

 

posted @ 2019-07-11 00:05  cutter_point  阅读(197)  评论(0编辑  收藏  举报