【LEETCODE】56、数组分类,适中级别,题目:62、63、1035

package y2019.Algorithm.array.medium;

/**
 * @ClassName UniquePathsWithObstacles
 * @Description TODO 63. Unique Paths II
 *
 * A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).
 * The robot can only move either down or right at any point in time.
 * The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).
 * Now consider if some obstacles are added to the grids. How many unique paths would there be?
 *
 * Input:
 * [
 *   [0,0,0],
 *   [0,1,0],
 *   [0,0,0]
 * ]
 * Output: 2
 * Explanation:
 * There is one obstacle in the middle of the 3x3 grid above.
 * There are two ways to reach the bottom-right corner:
 * 1. Right -> Right -> Down -> Down
 * 2. Down -> Down -> Right -> Right
 *
 * 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
 * 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/unique-paths-ii
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * 网格中的障碍物和空位置分别用 1 和 0 来表示。
 *
 *
 * 1, 1, 1
 * 1, 0, 1
 * 1, 1, 2
 *
 * @Author xiaof
 * @Date 2019/7/15 22:00
 * @Version 1.0
 **/
public class UniquePathsWithObstacles {

    public int solution(int[][] obstacleGrid) {
        //这个题很有动态规划的倾向
        //上一个位置到最后一个位置有几个走法
        //a[i][j] = a[i - 1][j] + a[i][j -1] 分别只能向右向下
        int res[][] = new int[obstacleGrid.length][obstacleGrid[0].length];

        //初始化,如果遇到障碍,那么那个位置不可到达为0
        //左边只有一种走法,向下
        int h = 1,l = 1;
        for(int i = 0; i < obstacleGrid.length; ++i) {
            if(obstacleGrid[i][0] == 1) {
                h = 0;
            }
            res[i][0] =  h;
        }
        for(int j = 0; j < obstacleGrid[0].length; ++j) {
            if(obstacleGrid[0][j] == 1) {
                l = 0;
            }
            res[0][j] = l;
        }

        //进行动态规划
        for(int i = 1; i < obstacleGrid.length; ++i) {
            for(int j = 1; j < obstacleGrid[i].length; ++j) {
                res[i][j] = obstacleGrid[i][j] == 1 ? 0 : res[i - 1][j] + res[i][j - 1];
            }
        }

        return res[obstacleGrid.length - 1][obstacleGrid[obstacleGrid.length - 1].length - 1];

    }

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

}
package y2019.Algorithm.array.medium;

/**
 * @ClassName UniquePaths
 * @Description TODO 62. Unique Paths
 * A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).
 * The robot can only move either down or right at any point in time.
 * The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).
 * How many possible unique paths are there?
 *
 * Input: m = 3, n = 2
 * Output: 3
 * Explanation:
 * From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
 * 1. Right -> Right -> Down
 * 2. Right -> Down -> Right
 * 3. Down -> Right -> Right
 *
 * 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
 * 问总共有多少条不同的路径?
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/unique-paths
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Author xiaof
 * @Date 2019/7/15 22:28
 * @Version 1.0
 **/
public class UniquePaths {

    public int solution(int m, int n) {
        //这个题很有动态规划的倾向
        //上一个位置到最后一个位置有几个走法
        //a[i][j] = a[i - 1][j] + a[i][j -1] 分别只能向右向下
        int res[][] = new int[m][n];

        //初始化,如果遇到障碍,那么那个位置不可到达为0
        //左边只有一种走法,向下
        int h = 1,l = 1;
        for(int i = 0; i < m; ++i) {
            res[i][0] =  h;
        }
        for(int j = 0; j < n; ++j) {
            res[0][j] = l;
        }

        //进行动态规划
        for(int i = 1; i < m; ++i) {
            for(int j = 1; j < n; ++j) {
                res[i][j] = res[i - 1][j] + res[i][j - 1];
            }
        }

        return res[m - 1][n - 1];
    }
}
package y2019.Algorithm.array.medium;

/**
 * @ClassName MaxUncrossedLines
 * @Description TODO 1035. Uncrossed Lines
 *
 * We write the integers of A and B (in the order they are given) on two separate horizontal lines.
 * Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that:
 * A[i] == B[j];
 * The line we draw does not intersect any other connecting (non-horizontal) line.
 * Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.
 * Return the maximum number of connecting lines we can draw in this way.
 *
 * Example 1:
 *
 * Input: A = [1,4,2], B = [1,2,4]
 * Output: 2
 * Explanation: We can draw 2 uncrossed lines as in the diagram.
 * We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.
 *
 * 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。
 * 现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且我们绘制的直线不与任何其他连线(非水平线)相交。
 * 以这种方法绘制线条,并返回我们可以绘制的最大连线数。
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/uncrossed-lines
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 *
 * @Author xiaof
 * @Date 2019/7/15 22:34
 * @Version 1.0
 **/
public class MaxUncrossedLines {

    public int solution(int[] A, int[] B) {
        //不能相交也就是用过的数前面就不能进行连接
        //每当A出i个数,B出j个数的时候,可以进行连接的情况是,当第i\和j的位置正好可以连线
        //如果不能,那么就分别加上A的i个数,和机上B的j个的时候取最大的一遍
        //res[i][j] = max{res[i - 1][j], res[i][j - 1]} or res[i][j] = res[i - 1][j - 1] + 1
        int m = A.length, n = B.length, dp[][] = new int[m + 1][n + 1];

        //初始化,默认为0
        for(int i = 1; i <= m; ++i) {
            //A使用几个数
            for(int j = 1; j <= n; ++j) {
                //这个循环代表B使用几个数
                if(A[i - 1] == B[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        return dp[m][n];
    }

}

原文地址:https://www.cnblogs.com/cutter-point/p/11192239.html

时间: 2024-08-01 22:32:08

【LEETCODE】56、数组分类,适中级别,题目:62、63、1035的相关文章

leetcode旋转数组查找 二分查找的变形

http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return it

LeetCode --- 56. Merge Intervals

题目链接:Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

LeetCode OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个0-若干个1-若干个2这样的序列. Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integ

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum =

LeetCode:数组中的第K个最大元素【215】

LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度. 题目分析 我们主要来学习一个新的集合类型--优先队列.优先队列作用是保证每次取

【LEETCODE】57、数组分类,适中级别,题目:969、442、695

package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array.medium * @ClassName: PancakeSort * @Author: xiaof * @Description: TODO 969. Pancake Sorting *

【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 charac

【LeetCode】数组--合并区间(56)

写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组(Array)是一种线性表数据结构.它用一组连续的内存空间,来存储一组具有相同类型的数据.在每一种编程语言中,基本都会有数组这种数据类型.不过,它不仅仅是一种编程语言中的数据类型,还是一种最基础的数据结构. 贪心算法回顾: [LeetCode]贪心算法--买卖股票的最佳时机II(122) [LeetC