leetcode 最大矩形和
1.枚举法(超时)
public class Solution {
public int largestRectangleArea(int[] height) {
int max=-1;
for(int i=0;i<height.length;i++)
{
int len=1;
int k=i-1;
while(k>=0&&height[k]<=height[i])
{
k--;
len++;
}
k=i+1;
while(k<height.length&&height[k]<=height[i])
{
len++;
k++;
}
if(max<len)
{
max=len;
}
}
return max;
}
}
2.单调栈(AC),其实模拟了第一种方法,nyoj做过,效率为线性。写的代码有点长,但是很好理解
class node
{
int d; //di
int h;// higtht
} public class Solution {
public int largestRectangleArea(int[] height) {
int len=height.length;
if(len==0) return 0;
node n[]=new node[len];
for(int i=0;i<len;i++)
{
n[i]=new node();
n[i].d=1;
n[i].h=height[i]; }
Stack<node> s=new Stack<node>();
s.push(n[0]);
int max=n[0].h;
for(int j=1;j<len;j++)
{
node t=s.peek();
if(n[j].h>=t.h)
{
s.push(n[j]);
}
else
{
int with=0;
while(!s.isEmpty()&&s.peek().h>n[j].h)
{
t=s.pop();
with+=t.d;
if(with*t.h>max) max=with*t.h; }
n[j].d+=with;
s.push(n[j]); } }
int with=0;
while(!s.isEmpty())
{
node t=s.pop();
with=with+t.d;
int temp=t.h*with; if(temp>max) max=temp; } return max;
}}
3.最大01矩阵()。使用上述四项,枚举每一行。
class node
{
int d; //di
int h;// higtht
} public class Solution {
public int largest(int[] height) {
int len=height.length;
if(len==0) return 0;
node n[]=new node[len];
for(int i=0;i<len;i++)
{
n[i]=new node();
n[i].d=1;
n[i].h=height[i]; }
Stack<node> s=new Stack<node>();
s.push(n[0]);
int max=n[0].h;
for(int j=1;j<len;j++)
{
node t=s.peek();
if(n[j].h>=t.h)
{
s.push(n[j]);
}
else
{
int with=0;
while(!s.isEmpty()&&s.peek().h>n[j].h)
{
t=s.pop();
with+=t.d;
if(with*t.h>max) max=with*t.h; }
n[j].d+=with;
s.push(n[j]); } }
int with=0;
while(!s.isEmpty())
{
node t=s.pop();
with=with+t.d;
int temp=t.h*with; if(temp>max) max=temp; } return max;
}
public int maximalRectangle(char[][] matrix) {
if(matrix.length==0) return 0; int len=matrix[0].length;
int ans[]=new int[len];
for(int i=0;i<len;i++)
{
ans[i]=matrix[0][i]-'0';
}
int max=largest(ans); for(int i=1;i<matrix.length;i++)
{
for(int j=0;j<matrix[0].length;j++)
{
if(matrix[i][j]=='0') ans[j]=0; else
{
ans[j]+=1;
} } int t=largest(ans);
if(max<t) max=t; } return max; }
}
https://oj.leetcode.com/problems/maximal-rectangle/
leetcode 最大矩形和的更多相关文章
- LeetCode:矩形区域【223】
LeetCode:矩形区域[223] 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. 示例: 输入: -3, 0, 3, 4, ...
- LeetCode子矩形查询
LeetCode 子矩形查询 题目描述 请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作: upda ...
- LeetCode 223. 矩形面积(Rectangle Area)
223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...
- Leetcode 363.矩形区域不超过k的最大数值和
矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...
- [LeetCode] 223.矩形面积
题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...
- LeetCode 836. 矩形重叠
题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左 ...
- Java实现 LeetCode 836 矩形重叠(暴力)
836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...
- Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和
363. 矩形区域不超过 K 的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,- ...
- Java实现 LeetCode 223 矩形面积
223. 矩形面积 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. Rectangle Area 示例: 输入: -3, 0, 3, 4 ...
随机推荐
- 记录平时code点滴,这次是通过一张充满异样字符的表,对数据表中的每一列进行清理,比double quotation的issue难多了!
需要提供对象: 一张需要被替换字符的表. 通过游标结合动态SQL对某一张特定表的所有列进行更新,主要是对其列值的异常字符处理. dbo.Characters_need_to_be_replaced c ...
- SQL几个有点偏的语句
SQL语句是一种集合操作,就是批量操作,它的速度要比其他的语言快,所以在设计的时候很多的逻辑都会放在sql语句或者存储过程中来实现,这个是一种设计思想.但是今天我们来讨论另外一个话题.Sql页提供了丰 ...
- 枚举N行N列的自然数列
数据库环境:SQL SERVER 2005 现有一个需求,要枚举1-50个自然数,分10行5列展示.如图,
- jQuery 效果方法
jQuery 效果方法 下面的表格列出了所有用于创建动画效果的 jQuery 方法. 方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被 ...
- Lucene 排序 Sort与SortField
在sql语句中,有升序和降序排列.在Lucene中,同样也有. Sort里的属性 SortField里的属性 含义 Sort.INDEXORDER SortField.FIELD_DOC 按照索引的顺 ...
- 使用enable_shared_from_this示例
/*测试enable_shared_from_this*/ #include <iostream> #include <boost/smart_ptr/shared_ptr.hpp& ...
- Git (2)
要使用Git首先遇到的问题是怎么把文件加到库中. 很简单. 新建一个目录,然后git init. 完成上述工作之后的唯一改动是在当前目录下生成了一个.git的子目录.这个子目录是一个集中的数据库,包含 ...
- 子网/ip/子网掩码
IP地址由网络地址和主机地址组成 而现在IP由“子网掩码”通过子网网络地 址细分出 A,B,C类更小的网络.这种方式 实际上就是将原来的A类,B类,C类等分类 中的的主机地址部分用作子网地址,可以 将 ...
- Quartz1.8.5例子(七)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- 从UI Automation看Windows平台自动化测试原理
前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...