【LEETCODE】63、数组分类,hard级别,题目:85、4、84
package y2019.Algorithm.array.hard; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.hard
* @ClassName: MaximalRectangle
* @Author: xiaof
* @Description: TODO 85. Maximal Rectangle
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
*
* Input:
* [
* ["1","0","1","0","0"],
* ["1","0","1","1","1"],
* ["1","1","1","1","1"],
* ["1","0","0","1","0"]
* ]
* Output: 6
*
* 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
*
* 如果是只包含的话,那么就需要计算面积
*
* @Date: 2019/7/26 16:54
* @Version: 1.0
*/
public class MaximalRectangle { public int solution(char[][] matrix){
//这里就是一行不一行的遍历,然后通过每一行的数据的
if(matrix == null || matrix.length <= 0) {
return 0;
} int rowLen = matrix.length;
int colLen = matrix[0].length; // 行 int left[] = new int[colLen], right[] = new int[colLen], height[] = new int[colLen]; //初始化
for(int j = 0; j < colLen; ++j) {
right[j] = colLen;
} //初始化right,用来标识这个直方图的右边坐标位置
int maxA = 0; //最大面积
for(int i = 0; i < rowLen; ++i) {
int curLeft = 0, curRight = colLen;
//循环遍历没一行
for(int j = 0; j < colLen; ++j) {
if(matrix[i][j] == '1') {
//如果是1,那么就是++高度
height[j]++;
} else {
height[j] = 0;//否则这个直方图的高度目前为0
}
} //计算左边坐标,从0开始,因为每当出现1的时候,我们就累加进去,如果没有出现1,是0,那么我们要把坐标起始位置向右边移动一位
//如果是1,那么以最大值为准,因为上一层的为1 的低位在这一层为0,那么就断掉了,不再这个直方图中
for(int j = 0; j < colLen; ++j) {
if(matrix[i][j] == '1') {
left[j] = Math.max(curLeft, left[j]);
} else {
//如果这个位置不是1,那么就要移动起始坐标
left[j] = 0;
curLeft = j + 1;
}
} //计算直方图右边坐标,这个应该从1开始,计算右边一直在矩阵内的坐标
for(int j = colLen - 1; j >= 0; --j) {
if(matrix[i][j] == '1') {
//这边取最小值,因为上一层有,这一层没有,那么就往左边缩
right[j] = Math.min(curRight, right[j]);
} else {
//如果这个位置不是1,那么就要移动起始坐标
right[j] = colLen;
curRight = j;
}
} //计算最大面积
for(int j = 0; j < colLen; ++j) {
maxA = Math.max(maxA, (right[j] - left[j]) * height[j]);
} } return maxA; } public static void main(String args[]) {
char data[][] = {{'1'}};
char data2[][] = {
{'1','0','1','0','0'},
{'1','0','1','1','1'},
{'1','1','1','1','1'},
{'1','0','0','1','0'}
};
MaximalRectangle fuc = new MaximalRectangle();
System.out.println(fuc.solution(data2));
} }
package y2019.Algorithm.array.hard; /**
* @ClassName FindMedianSortedArrays
* @Description TODO 4. Median of Two Sorted Arrays
*
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
* You may assume nums1 and nums2 cannot be both empty.
*
* nums1 = [1, 3]
* nums2 = [2]
*
* The median is 2.0
*
* 参考:https://www.youtube.com/watch?v=LPFhl65R7ww
* @Author xiaof
* @Date 2019/7/28 16:32
* @Version 1.0
**/
public class FindMedianSortedArrays { public double solution(int[] nums1, int[] nums2) {
if(nums1.length > nums2.length) {
return solution(nums2, nums1); //我们需要根据长度小的中间位置计算另外一个长的位置的分割点
} //获取总长
int x = nums1.length;
int y = nums2.length; //计算区间
int low = 0, hight = x; //根据第一个数组进行分割
while (low <= hight) {
//只要瞒住条件,就一直循环,核心思想还是二分查找
//开始分割
int partitionx = (low +hight) / 2;
int partitiony = (x + y + 1) / 2 - partitionx; //计算第二个数组的分割点,对于和是奇数的情况+1,可以向右边移动一位保证中间数据在左边,这样就不用判断左右了 //计算两个数组中被分割的临近中间位置的数据,如果分割位置是0,那也就左边没有元素不用比较计算,直接设置最小值
int maxLeftx = partitionx == 0 ? Integer.MIN_VALUE : nums1[partitionx - 1];
int minRightx = partitionx == x ? Integer.MAX_VALUE : nums1[partitionx]; //右边最小位置 int maxLefty = partitiony == 0 ? Integer.MIN_VALUE : nums2[partitiony - 1];
int minRighty = partitiony == y ? Integer.MAX_VALUE : nums2[partitiony]; //分三种情况,第一张找到了
if(maxLeftx <= minRighty && maxLefty <= minRightx) {
//因为本身已经排好顺序,那么只要瞒住这个条件,那么就可以说这2个集合正好被分成了2块
//判断一共是奇数个还是偶数个
if((x + y) % 2 == 0) {
//偶数求平均值
return ((double) Math.max(maxLeftx, maxLefty) + Math.min(minRightx, minRighty)) / 2;
} else {
return (double) Math.max(maxLeftx, maxLefty);
}
} else if (maxLeftx > minRighty) {
//如果左边的第一个序列的最大值比第二个序列右边最小值大,说明不是中分的数据,说明第一个序列分割的位置太大了,我们把这个元素划归到右边去
hight = partitionx - 1;
} else {
//maxLefty 》 minRightx 右边第一个序列的最小值比下面的序列的最大值小,说明高位给低了
low = partitionx + 1;
}
} //最后出现异常情况,那就是如果序列没排序,那就GG
return -1; } public static void main(String args[]) {
int input1[] = {1,3};
int input2[] = {2};
FindMedianSortedArrays fuc = new FindMedianSortedArrays();
System.out.println(fuc.solution(input1, input2));
} }
package y2019.Algorithm.array.hard; /**
* @ClassName LargestRectangleArea
* @Description TODO 84. Largest Rectangle in Histogram
* 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.
* @Author xiaof
* @Date 2019/7/28 21:44
* @Version 1.0
**/
public class LargestRectangleArea { public int solution(int[] heights) {
if(heights == null || heights.length <= 0) {
return 0;
} //求最大面积,参考85. Maximal Rectangle ,可以用同样的思路求解
//同样我们需要找到这个直方图的左边界,右边界,然后整合所有直方图的最大值
int left[] = new int[heights.length], right[] = new int[heights.length]; //遍历所有直方图,并根据当前直方图获取左右两边的位置 left
left[0] = -1;
for(int i = 1; i < heights.length; ++i) {
int p = i - 1;
while (p >= 0 && heights[p] >= heights[i]) {
// --p;
//这里不用每次都遍历,直接使用上次的结果就可以了
p = left[p];
}
//设置这个最左边的坐标
left[i] = p;
} //设置右边
right[heights.length - 1] = heights.length;
for(int i = heights.length - 2; i >= 0; --i) {
int p = i + 1;
while (p < heights.length && heights[p] >= heights[i]) {
// ++p;
//这里不用每次都遍历,直接使用上次的结果就可以了
p = right[p];
}
right[i] = p;
} //求面积maxa = Math.max{maxa, heights[i] * (right[i] - left[i] - 1)}
int maxa = 0;
for(int i = 0; i < heights.length; ++i) {
maxa = Math.max(maxa, heights[i] * (right[i] - left[i] - 1));
} return maxa; } public static void main(String args[]) {
int data[] = {2,1,5,6,2,3};
LargestRectangleArea fuc = new LargestRectangleArea();
System.out.println(fuc.solution(data));
} }
【LEETCODE】63、数组分类,hard级别,题目:85、4、84的更多相关文章
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)
这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- LeetCode:数组中的第K个最大元素【215】
LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...
- LeetCode一维数组的动态和
一维数组的动态和 题目描述 给你一个数组 nums.数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]...nums[i]). 请返回 nums 的动态和. 示例 1: ...
- 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035
package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...
- 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73
这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...
- 【LEETCODE】54、数组分类,简单级别,题目:605、532
数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...
- 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747
真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...
随机推荐
- [Android] Android studio gradle 插件的版本号和 gradle 的版本号 的对应关系
[Android] Android studio gradle 插件的版本号和 gradle 的版本号 的对应关系 本博客地址: wukong1688 本文原文地址:https://www.cnblo ...
- 原创:Kmeans算法实战+改进(java实现)
kmeans算法的流程: EM思想很伟大,在处理含有隐式变量的机器学习算法中很有用.聚类算法包括kmeans,高斯混合聚类,快速迭代聚类等等,都离不开EM思想.在了解kmeans算法之前,有必要详 ...
- GoCN每日新闻(2019-11-02)
GoCN每日新闻(2019-11-02) GoCN每日新闻(2019-11-02) 1. Go 1.13.4 and Go 1.12.13 are released https://groups.go ...
- shell 命令行参数(基本)
命令行参数 $0 表示程序名.$1 至 \$9则是位置参数.$# 表示参数的个数.$* 将所有参数当做一个整体来引用$@ 把每个参数作为一个字符串返回,可以使用for循环来遍历$? 最近一个执行的命令 ...
- HustOJ二次开发之修改相关Logo
比如将如图中的HUSTOJ进行修改: 在Linux上修改,通过关键字搜索,会获取如下两个重要文件,找到都有的文字进行修改即可: grep -rn "HUSTOJ" * cd /ho ...
- git clone指定branch或tag
git clone指定branch或tag发布时间:October 28, 2018 // 分类: // No Comments 取完整: git clone https://github.com/a ...
- Linux权限:提示-bash: ./startup.sh: Permission denied的解决方案
Linux权限:提示-bash: ./startup.sh: Permission denied的解决方案 Linux上启动Tomcat,结果弹出:-bash: ./startup.sh: Permi ...
- 解决若依linux启动ERROR - unregister mbean error javax.management.InstanceNotFoundException: com.alibaba.druid:type=
项目中使用druid对数据库连接池进行管理,在本地及测试环境均无问题,但是上了生产环境后,每当tomcat第一次启动时,日志未报错,但是页面总是出不来,在关闭tomcat时,看日志,发现报错如下: E ...
- chmod: changing permissions of ‘/etc/fstab': Read-only file system
给passwd文件加权限,修改/etc/fstab目录下所有的文件夹属性为可写可读可执行,执行以下命令:chomd 777 /etc/fstab 的时候提示错误: chmod: changing pe ...
- shell 备份mysql
shell脚本备份mysql,放在crontab中,可以作为每日测试用数据库备份 #!/bin/bash string_time=`date +%Y%m%d%H%M`; file_path=`date ...