最大的矩形 java_最大矩形 - HelloJava菜鸟社区

5ca83f26ebf0e1f37fe1dcd7275e2845.png

6f65f2d938a6286ff8b44cbc58e0e91d.png

export default (arr) => {

let result = []

let reg = /1{2,}/g

// 把二维数组重新表达,把相邻的1提取出来

arr = arr.map(item => {

let str = item.join('')

let r = reg.exec(str)

let rs = []

while (r) {

rs.push([r.index, r.index + r[0].length - 1])

r = reg.exec(str)

}

return rs

})

// 通过递归计算相邻矩阵

let maxRect = (arr, result, n = 1) => {

// 弹出第一行

let top = arr.pop()

// 弹出第二行

let next = arr.pop()

// 记录第一行的每一个起始和截止

let tt

// 记录第二行。。。

let nn

// 记录交叉的其实索引

let start

// 记录交叉的截止索引

let end

let width = 1

let maxWidth = 1

n++

for (let i = 0, il = top.length; i < il; i++) {

tt = top[i]

for (let j = 0, jl = next.length; j < jl; j++) {

nn = next[j]

width = Math.min(tt[1], nn[1]) - Math.max(tt[0], nn[0])

if (width > maxWidth) {

maxWidth = width

start = Math.max(tt[0], nn[0])

end = Math.max(tt[1], nn[1])

}

}

}

// 没有交叉点

if (start === undefined || end === undefined) {

if (n < 3) {

return false

} else {

width = top[0][1] - top[0][0] + 1

if (width > 1) {

result.push((n - 1) * width)

}

}

} else {

arr.push([[start, end]])

maxRect(arr, result, n++)

}

}

while (arr.length > 1) {

// 要值拷贝,因数组在javascript中时引用类型

maxRect([].concat(arr), result)

arr.pop()

}

let max = 0

let item = result.pop()

while (item) {

if (item > max) {

max = item

}

item = result.pop()

}

return max > 0 ? max : -1

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值