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. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  2. [译博文]CUDA是什么

    翻译自:https://blogs.nvidia.com/blog/2012/09/10/what-is-cuda-2/ 你可能并没有意识到,GPU的应用有多广泛,它不但用于视频.游戏以及科学研究中, ...

  3. JavaScript高级程序编程(二)

    JavaScript 基本概念 1.区分大小写,变量名test与Test 是两个不同的变量,且函数命名不能使用关键字/保留字, 变量命名规范: 开头字符必须是字母,下划线,或者美元符号,ECMAScr ...

  4. 【loj2983】【WC2019】数树

    题目 两颗\(n\)个点的树T1和T2,有\(y\)种颜色; 现在给每个点染色,要求公共边端点的颜色相同,求: ​ 1.op=0 , T1和T2都确定,求合法染色方案数: ​ 2.op=1 , T1确 ...

  5. vue-cli之路由独立成JS文件之后,如何在路由中获取vuex属性或者设置国际化i18n的当前使用语言

    国际化vue-i18n的使用: import Vue from 'vue'; import VueI18n from 'vue-i18n'; // 引入语言包 import zh from '@/co ...

  6. 【luoguP2999】 [USACO10NOV]巧克力牛奶Chocolate Milk

    题目链接 考虑每条路径都经过的一个点,它可以到达每个出度为零点(终点),且每个入读为零点(起点)都能到达它, 拓扑排序记录下每个结点能到达的出度为零点的个数和沿反边能到达的入读为零点个数,判断是否等于 ...

  7. python 判断一个字符串组合后,是否在另一个字符串中

    code #coding=utf- def getdic(s): dic = {} for i in s: if (i not in dic): dic[i] = else: dic[i] += re ...

  8. 【pytorch 代码】pytorch 网络结构可视化

    部分内容转载自 http://blog.csdn.net/GYGuo95/article/details/78821617,在此表示由衷感谢. 此方法需要安装python-graphviz:  con ...

  9. jQuery跳出each循环:JS报错:illegal break statement

    今天在JS中运用jquery中each写一个简单的循环语句时,在执行跳出循环操作时,遇到JS报错:Uncaught SyntaxError: illegal break statement 非法的br ...

  10. ingress nginx https配置

    3.https配置第一步:制作自签证书 [root@master demo]# openssl genrsa -out tls.key 2048 [root@master demo]# openssl ...