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. 微软安全技术Shim

    Shim是微软系统中一个小型函数库,用于透明地拦截API调用,修改传递的参数.自身处理操作.或把操作重定向到其他地方.Shim主要用于解决遗留应用程序在新版Windows系统上的兼容性问题,但Shim ...

  2. Nodejs中的JavaScript

    一.Ecmascript ①基本语法:if var function Object Array等 ②特别注意:Node.js中没有DOM和BOM 二.核心模块: 1.Node.js为JavaScrip ...

  3. linux date获取时间戳

    linux 时间戳格式 年月日 时分秒: `date ‘+%Y%m%d%H%M%S’`date +%Y%m%d%H%M%S // 年月日 时分秒date +%s // 从 1970年1月1日零点开始到 ...

  4. [RN] React Native 生成 Android APK

    在用模拟器或者真机调试完App后,需要将App打包成Apk发布文件. 下面简单记录下打包步骤: 第一:生成签名密钥 这一步的操作主要是生成需要的签名密钥,供android调用,生成的文件待用 在项目根 ...

  5. Git常用命令及常见报错:You have not concluded your merge (MERGE_HEAD exists)、清理无效的远程追踪分支

    一.常用命令 切换到master分支:git checkout master 查看已有本地及远程分支:git branch -a(先git pull拉下全部数据) 查看远程分支:git branch ...

  6. c博客作业01--顺序分支结构

    0.展示PTA总分 1.本章学习总结 1.1 学习内容总结 1.运算符需注意的要点 '/'的左右两边如果均为整型数,其结果也为整型:'%'的左右两边只能为整型数: 优先级:逻辑运算符<关系运算符 ...

  7. nginx中的upstream使用

    upstream的基本使用 upstream admin{server 127.0.0.1:9090 down;server 127.0.0.1:8080 weight=2;server 127.0. ...

  8. Tcl数学运算

    expr 数学表达式 Tcl支持的数学操作符(优先级按照从高到低): -一元负号 +一元正号 ~按位取反 !逻辑非 *乘 /除 %取余 +加号 -减号 <<左移位 >>右移位 ...

  9. ueditor div style被过滤 解决办法

    上周开发中有用到开源的富文本编辑器UEditor,在使用的过程中遇到了样式被过滤无法显示问题,经过一番折腾终解决,此外,还有一些关于获取前台界面元素的一些总结. 1. UEditor样式被过滤无法显示 ...

  10. 2019软工实践_Alpha(6/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11913269.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...