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];
} }

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

  1. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  2. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

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

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  4. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  5. Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)

    Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...

  6. LeetCode一维数组的动态和

    一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...

  7. 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73

    这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...

  8. 【LEETCODE】62、数组分类,hard级别,题目:42、128

    package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...

  9. 【LEETCODE】60、数组分类,适中级别,题目:75、560、105

    package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...

随机推荐

  1. zabbix server内存突然飙升

    2019年10月16日22:20:58 十点二十突然内存占满,top查询一个httpd进程占了79%,查询httpd.error.log发现 [Wed Oct 16 10:24:57.578643 2 ...

  2. FOREIGN KEY 外键约束; UNIQUE和PRIMARY KEY 主键约束、CREATE INDEX建立索引的使用

    1)foreign key 是个约束,意思是说如果你给A字段设置了外键约束,以后你要往A字段插入数据,这个数据一定是要在foreign key 后面跟的那个字段中存在的值.这个的意义就是约束了数据的完 ...

  3. spring boot +dubbo+zookeeper

    dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 结合本公司的开发也是用的dubbo这款优秀的框架,加上 最近工作重心的.所以对于dubbo的 ...

  4. SpringBoot异步及线程池配置

    异步方法注解@Async 在SpringBoot中进行异步处理,可以使用异步注解@Async和@EnableAsync. @Async注解表示异步,如:@Async("asyncServic ...

  5. 如何使用Salt Pillar

    作者言 Salt的网站上有两篇关于Pillar的文档(一,二),其中一篇内容很少,我觉得写成一篇文章更合适.本文的逻辑结构没有参照官方文档,而是根据我自己对Pillar的理解组织内容,希望能够把这个概 ...

  6. pip常用命令(转载)

    用阿里云服务器,使用pip安装第三方库的时候卡的要死.所以我就想pip能不能安装本地的包. 找到了这篇博客: http://me.iblogc.com/2015/01/01/pip%E5%B8%B8% ...

  7. SpringBoot使用jasypt加解密密码

    在我们的服务中不可避免的需要使用到一些秘钥(数据库.redis等) 开发和测试环境还好,但生产如果采用明文配置讲会有安全问题,jasypt是一个通用的加解密库,我们可以使用它. <depende ...

  8. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  9. jdbc如何处理事务

    Connection提供了事务处理的方法,通过调用setAutoCommit(false)可以设置手动提交事务:当事务完成后用commit()显式提交事务:如果在事务处理过程中发生异常则通过rollb ...

  10. Sword libcurl库CURLE_COULDNT_CONNECT错误

    CURL: CURLE_COULDNT_CONNECT问题分析 测试环境描述在使用libcurl写http客户端进行压力测试的时候会遇到curl_easy_perform()返回CURLE_COULD ...