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. A simple dispiction of dijkstra

    前言 \(SPFA\)算法由于它上限 \(O(NM) = O(VE)\)的时间复杂度,被卡掉的几率很大.在算法竞赛中,我们需要一个更稳定的算法:\(dijkstra\). 什么是\(dijkstra\ ...

  2. err Invalid input of type: 'dict'. Convert to a byte, string or number first

    一个问题引发的血案: 用python向redis写入数据报错: redis.exceptions.DataError: Invalid input of type: 'dict'. Convert t ...

  3. 海底高铁(洛谷 P3406)

    题目背景 大东亚海底隧道连接着厦门.新北.博艾.那霸.鹿儿岛等城市,横穿东海,耗资1000亿博艾元,历时15年,于公元2058年建成.凭借该隧道,从厦门可以乘坐火车直达台湾.博艾和日本,全程只需要4个 ...

  4. php 进制转换base_convert

    16进制 转为 8进制 base_convert(number,frombase,tobase); 参数 描述 number 必需.规定要转换的数. frombase 必需.规定数字原来的进制.介于 ...

  5. 缺陷的优先程度(Priority)

    测试人员希望程序员什么时间哪个版本修改该bug (1)Urgent 立即修改否则影响开发进度 (2)Veryhigh 本版本修改 (3)High 下个版本修改 (4)Medium 发布前修改 (5)L ...

  6. hdu5438 Ponds[DFS,STL vector二维数组]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu5438 题干 代码和解释 解答本题时参考了一篇代码较短的博客,比较有意思,使用了STL vector二维数组. 可以结合下面的示例代码理解: ...

  7. [C++] explicit关键字使用方法

    C++中,构造函数可以用作自动类型转换,但是这种转换不一定是程序所需要的,有时会导致错误的类型转换. 下面的代码,在mian函数中,将一个整形赋值为对象类型. #include "iostr ...

  8. 将爬取的实习僧网站数据传入HDFS

     一.引言: 作为一名大三的学生,找实习对于我们而言是迫在眉睫的.实习作为迈入工作的第一步,它的重要性不言而喻,一份好的实习很大程度上决定了我们以后的职业规划. 那么,一份好的实习应该考量哪些因素呢? ...

  9. pycharm 当有多个.py文件在开发环境中时,如何操作可以保证运行当前面对自己的文件?

    Alt+shift+F10选择自己的py文件,执行就可以了.

  10. vue---splitpane分割

    使用splitpane可以对窗口进行拆分,这个splitpane组件还是比较好用的, 首先安装: npm install vue-splitpane 引入使用: import splitPane fr ...