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. 处理 MySQL 因为 SLAVE 崩溃导致需要手动跳过 GTID 的问题 | 关于 GTID

    今天发生了与之前某篇博客相似的问题,有同学在不同步的 binlog 库中使用语句 database.table 命令对表进行 drop 导致 master 丢弃该表但是从库并未能同步到该操作.并且后续 ...

  2. ICEM——对msh文件或者cas文件重新划分边界

    原视频下载地址:https://pan.baidu.com/s/1jIoKSuy 密码: m3uv

  3. 微信小程序之如何定义页面标题

    效果图: 这个标题是在哪里定义的呢?type.js核心代码如下(通常这段代码放在onLoad函数体内): wx.setNavigationBarTitle({ title: "支出类型列表& ...

  4. 第2课第7节_Java面向对象编程_内部类_P【学习笔记】

    摘要:韦东山android视频学习笔记  1.什么是内部类:在类的内部定义一个类,内部类可以访问类的私有属性 class Outer{ ; class Inner{ public void print ...

  5. Learning Context Graph for Person Search

    Learning Context Graph for Person Search 2019-06-24 09:14:03 Paper:http://openaccess.thecvf.com/cont ...

  6. Nginx可以做什么?(转载)

    本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得,欢迎留言交流. Nginx能做什么 —— ...

  7. CEF 远程调试

    转载:https://www.cnblogs.com/TianFang/p/9906786.html 转载:https://stackoverflow.com/questions/29117882/d ...

  8. C语言 运算符优先级

    规律小结: 结合方向只有三个是从右往左,其余都是从左往右. 所有双目运算符中只有赋值运算符的结合方向是从右往左. 另外两个从右往左结合的运算符也很好记,因为它们很特殊:一个是单目运算符,一个是三目运算 ...

  9. ES6新增函数总结和range函数实现

    Array.from  类数组,Set,字符串转为数组 Array.of   不定参数转为数组 Array.prototype.fill(value,[start],[end]) 对数组在指定范围填充 ...

  10. 使用协方差矩阵的特征向量PCA来处理数据降维

    取2维特征,方便图形展示 import matplotlib.pyplot as plt from sklearn.decomposition import PCA from sklearn.data ...