【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));
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章