1. package y2019.Algorithm.array.hard;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.array.hard
  6. * @ClassName: MaximalRectangle
  7. * @Author: xiaof
  8. * @Description: TODO 85. Maximal Rectangle
  9. * Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
  10. *
  11. * Input:
  12. * [
  13. * ["1","0","1","0","0"],
  14. * ["1","0","1","1","1"],
  15. * ["1","1","1","1","1"],
  16. * ["1","0","0","1","0"]
  17. * ]
  18. * Output: 6
  19. *
  20. * 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
  21. *
  22. * 如果是只包含的话,那么就需要计算面积
  23. *
  24. * @Date: 2019/7/26 16:54
  25. * @Version: 1.0
  26. */
  27. public class MaximalRectangle {
  28.  
  29. public int solution(char[][] matrix){
  30. //这里就是一行不一行的遍历,然后通过每一行的数据的
  31. if(matrix == null || matrix.length <= 0) {
  32. return 0;
  33. }
  34.  
  35. int rowLen = matrix.length;
  36. int colLen = matrix[0].length; // 行
  37.  
  38. int left[] = new int[colLen], right[] = new int[colLen], height[] = new int[colLen];
  39.  
  40. //初始化
  41. for(int j = 0; j < colLen; ++j) {
  42. right[j] = colLen;
  43. }
  44.  
  45. //初始化right,用来标识这个直方图的右边坐标位置
  46. int maxA = 0; //最大面积
  47. for(int i = 0; i < rowLen; ++i) {
  48. int curLeft = 0, curRight = colLen;
  49. //循环遍历没一行
  50. for(int j = 0; j < colLen; ++j) {
  51. if(matrix[i][j] == '1') {
  52. //如果是1,那么就是++高度
  53. height[j]++;
  54. } else {
  55. height[j] = 0;//否则这个直方图的高度目前为0
  56. }
  57. }
  58.  
  59. //计算左边坐标,从0开始,因为每当出现1的时候,我们就累加进去,如果没有出现1,是0,那么我们要把坐标起始位置向右边移动一位
  60. //如果是1,那么以最大值为准,因为上一层的为1 的低位在这一层为0,那么就断掉了,不再这个直方图中
  61. for(int j = 0; j < colLen; ++j) {
  62. if(matrix[i][j] == '1') {
  63. left[j] = Math.max(curLeft, left[j]);
  64. } else {
  65. //如果这个位置不是1,那么就要移动起始坐标
  66. left[j] = 0;
  67. curLeft = j + 1;
  68. }
  69. }
  70.  
  71. //计算直方图右边坐标,这个应该从1开始,计算右边一直在矩阵内的坐标
  72. for(int j = colLen - 1; j >= 0; --j) {
  73. if(matrix[i][j] == '1') {
  74. //这边取最小值,因为上一层有,这一层没有,那么就往左边缩
  75. right[j] = Math.min(curRight, right[j]);
  76. } else {
  77. //如果这个位置不是1,那么就要移动起始坐标
  78. right[j] = colLen;
  79. curRight = j;
  80. }
  81. }
  82.  
  83. //计算最大面积
  84. for(int j = 0; j < colLen; ++j) {
  85. maxA = Math.max(maxA, (right[j] - left[j]) * height[j]);
  86. }
  87.  
  88. }
  89.  
  90. return maxA;
  91.  
  92. }
  93.  
  94. public static void main(String args[]) {
  95. char data[][] = {{'1'}};
  96. char data2[][] = {
  97. {'1','0','1','0','0'},
  98. {'1','0','1','1','1'},
  99. {'1','1','1','1','1'},
  100. {'1','0','0','1','0'}
  101. };
  102. MaximalRectangle fuc = new MaximalRectangle();
  103. System.out.println(fuc.solution(data2));
  104. }
  105.  
  106. }
  1. package y2019.Algorithm.array.hard;
  2.  
  3. /**
  4. * @ClassName FindMedianSortedArrays
  5. * @Description TODO 4. Median of Two Sorted Arrays
  6. *
  7. * There are two sorted arrays nums1 and nums2 of size m and n respectively.
  8. * Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
  9. * You may assume nums1 and nums2 cannot be both empty.
  10. *
  11. * nums1 = [1, 3]
  12. * nums2 = [2]
  13. *
  14. * The median is 2.0
  15. *
  16. * 参考:https://www.youtube.com/watch?v=LPFhl65R7ww
  17. * @Author xiaof
  18. * @Date 2019/7/28 16:32
  19. * @Version 1.0
  20. **/
  21. public class FindMedianSortedArrays {
  22.  
  23. public double solution(int[] nums1, int[] nums2) {
  24. if(nums1.length > nums2.length) {
  25. return solution(nums2, nums1); //我们需要根据长度小的中间位置计算另外一个长的位置的分割点
  26. }
  27.  
  28. //获取总长
  29. int x = nums1.length;
  30. int y = nums2.length;
  31.  
  32. //计算区间
  33. int low = 0, hight = x; //根据第一个数组进行分割
  34. while (low <= hight) {
  35. //只要瞒住条件,就一直循环,核心思想还是二分查找
  36. //开始分割
  37. int partitionx = (low +hight) / 2;
  38. int partitiony = (x + y + 1) / 2 - partitionx; //计算第二个数组的分割点,对于和是奇数的情况+1,可以向右边移动一位保证中间数据在左边,这样就不用判断左右了
  39.  
  40. //计算两个数组中被分割的临近中间位置的数据,如果分割位置是0,那也就左边没有元素不用比较计算,直接设置最小值
  41. int maxLeftx = partitionx == 0 ? Integer.MIN_VALUE : nums1[partitionx - 1];
  42. int minRightx = partitionx == x ? Integer.MAX_VALUE : nums1[partitionx]; //右边最小位置
  43.  
  44. int maxLefty = partitiony == 0 ? Integer.MIN_VALUE : nums2[partitiony - 1];
  45. int minRighty = partitiony == y ? Integer.MAX_VALUE : nums2[partitiony];
  46.  
  47. //分三种情况,第一张找到了
  48. if(maxLeftx <= minRighty && maxLefty <= minRightx) {
  49. //因为本身已经排好顺序,那么只要瞒住这个条件,那么就可以说这2个集合正好被分成了2块
  50. //判断一共是奇数个还是偶数个
  51. if((x + y) % 2 == 0) {
  52. //偶数求平均值
  53. return ((double) Math.max(maxLeftx, maxLefty) + Math.min(minRightx, minRighty)) / 2;
  54. } else {
  55. return (double) Math.max(maxLeftx, maxLefty);
  56. }
  57. } else if (maxLeftx > minRighty) {
  58. //如果左边的第一个序列的最大值比第二个序列右边最小值大,说明不是中分的数据,说明第一个序列分割的位置太大了,我们把这个元素划归到右边去
  59. hight = partitionx - 1;
  60. } else {
  61. //maxLefty 》 minRightx 右边第一个序列的最小值比下面的序列的最大值小,说明高位给低了
  62. low = partitionx + 1;
  63. }
  64. }
  65.  
  66. //最后出现异常情况,那就是如果序列没排序,那就GG
  67. return -1;
  68.  
  69. }
  70.  
  71. public static void main(String args[]) {
  72. int input1[] = {1,3};
  73. int input2[] = {2};
  74. FindMedianSortedArrays fuc = new FindMedianSortedArrays();
  75. System.out.println(fuc.solution(input1, input2));
  76. }
  77.  
  78. }
  1. package y2019.Algorithm.array.hard;
  2.  
  3. /**
  4. * @ClassName LargestRectangleArea
  5. * @Description TODO 84. Largest Rectangle in Histogram
  6. * Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
  7. * @Author xiaof
  8. * @Date 2019/7/28 21:44
  9. * @Version 1.0
  10. **/
  11. public class LargestRectangleArea {
  12.  
  13. public int solution(int[] heights) {
  14. if(heights == null || heights.length <= 0) {
  15. return 0;
  16. }
  17.  
  18. //求最大面积,参考85. Maximal Rectangle ,可以用同样的思路求解
  19. //同样我们需要找到这个直方图的左边界,右边界,然后整合所有直方图的最大值
  20. int left[] = new int[heights.length], right[] = new int[heights.length];
  21.  
  22. //遍历所有直方图,并根据当前直方图获取左右两边的位置 left
  23. left[0] = -1;
  24. for(int i = 1; i < heights.length; ++i) {
  25. int p = i - 1;
  26. while (p >= 0 && heights[p] >= heights[i]) {
  27. // --p;
  28. //这里不用每次都遍历,直接使用上次的结果就可以了
  29. p = left[p];
  30. }
  31. //设置这个最左边的坐标
  32. left[i] = p;
  33. }
  34.  
  35. //设置右边
  36. right[heights.length - 1] = heights.length;
  37. for(int i = heights.length - 2; i >= 0; --i) {
  38. int p = i + 1;
  39. while (p < heights.length && heights[p] >= heights[i]) {
  40. // ++p;
  41. //这里不用每次都遍历,直接使用上次的结果就可以了
  42. p = right[p];
  43. }
  44. right[i] = p;
  45. }
  46.  
  47. //求面积maxa = Math.max{maxa, heights[i] * (right[i] - left[i] - 1)}
  48. int maxa = 0;
  49. for(int i = 0; i < heights.length; ++i) {
  50. maxa = Math.max(maxa, heights[i] * (right[i] - left[i] - 1));
  51. }
  52.  
  53. return maxa;
  54.  
  55. }
  56.  
  57. public static void main(String args[]) {
  58. int data[] = {2,1,5,6,2,3};
  59. LargestRectangleArea fuc = new LargestRectangleArea();
  60. System.out.println(fuc.solution(data));
  61. }
  62.  
  63. }

【LEETCODE】63、数组分类,hard级别,题目:85、4、84的更多相关文章

  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] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

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

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

  5. LeetCode一维数组的动态和

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

  6. 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035

    package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...

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

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

  8. 【LEETCODE】54、数组分类,简单级别,题目:605、532

    数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...

  9. 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747

    真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...

随机推荐

  1. REdis一致性方案探讨

    REdis功能强大众所周知,能够大幅简化开发和提供大并发高性能,但截止到REdis-5.0.5仍然存在如下几大问题: 一致性问题 这是由于REdis的主从复制采用的是异步复制,异常时可能发生主节点的数 ...

  2. linux命令之------rm命令

    rm命令 1)    作用:用于删除一个文件或者目录: 2)    -i:删除前逐一询问确认: 3)    -f:即使原档案属性设为只读,亦直接删除,无需逐一确认: 4)-r:将目录及以下之档案亦逐一 ...

  3. Cookie 的 SameSite 属性

    转自http://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html Chrome 51 开始,浏览器的 Cookie 新增加了一个SameSit ...

  4. player: 初始化分析

    //1. //cocos 程序开始运行时执行的函数 bool AppDelegate::applicationDidFinishLaunching() { // initialize director ...

  5. mvn常见参数命令讲解

    关于-N -N,--non-recursive Do not recurse into sub-projects 意思是,不递归到子项目(子模块). 举例: 一个父项目下Father面有3个子项目A. ...

  6. 运维笔记--Linux查找指定目录下包含某字符串的文件

    待整理: 参考:http://blog.sina.com.cn/s/blog_53d496960102xg5c.html 例: find /home/logstash/ -type f | xargs ...

  7. Linux如何将未分配的硬盘挂载出来

    情景说明 客户给了几台服务器,说500G硬盘,但到手操作的时候,使用命令查看,发现只有不到200的硬盘 [root@localhost ~]# df -h Filesystem Size Used A ...

  8. android 桌面图标添加数字角标

    是否支持角标并不与手机厂商有关,而是你当前使用的launcher开发厂商有关. 方法实现: import android.app.Application; import android.app.Not ...

  9. 浅入深出ETCD之【集群部署与golang客户端使用】

    前言 之前说了etcd的简介,命令行使用,一些基本原理.这次来说说现实一点的集群部署和golang版本的客户端使用.因为在实际使用过程中,etcd的节点肯定是需要2N+1个进行部署的,所以有必要说明一 ...

  10. Numa 常用命令

    1. 查看numa相关信息,包括每个node内存大小,每个node中的逻辑cpu: numactl --hardware