[关闭]
@cxm-2016 2016-11-07T13:45:09.000000Z 字数 3357 阅读 1995

算法:求最大子矩阵的面积

算法 0

版本:0
作者:陈小默
声明:禁止商业,禁止转载

题目:给定一个矩阵matrix,其中数据为整型,求其中全部是非0的所有矩形区域中 ,最大的矩形区域面积。

例如:

  1. import java.util.*
  2. /**
  3. * project:DSA
  4. * Copyright (C) <2016> <陈小默>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * Created by 陈小默 on 16/11/7.
  20. */
  21. class Matrix(val column: Int, val raw: Int, m: IntArray? = null) {
  22. private val arr = IntArray(column * raw)
  23. init {
  24. if (m != null) {
  25. var index: Int
  26. finish@for (r in 0..raw - 1)
  27. for (c in 0..column - 1) {
  28. index = r * column + c
  29. if (index >= m.size) break@finish
  30. arr[index] = m[index]
  31. }
  32. }
  33. }
  34. operator fun set(c: Int, r: Int, value: Int) {
  35. val index = r * column + c
  36. arr[index] = value
  37. }
  38. operator fun get(c: Int, r: Int): Int {
  39. val index = r * column + c
  40. return arr[index]
  41. }
  42. override fun toString(): String {
  43. val builder = StringBuilder().append('[')
  44. var index: Int
  45. for (r in 0..raw - 1) {
  46. if (r != 0) builder.appendln().append(' ')
  47. for (c in 0..column - 1) {
  48. if (c != 0) builder.append('\t')
  49. index = r * column + c
  50. builder.append(arr[index])
  51. }
  52. }
  53. return builder.append(']').toString()
  54. }
  55. }
  56. fun getMaxFromMap(map: HashMap<Int, Int>, index: Int): Int {
  57. var max = 0
  58. var max2: Int
  59. if (index != 0 && !map.containsKey(index)) {
  60. var maxIndex = 0
  61. map.filter { entry -> entry.key > index }.forEach { entry ->
  62. if (entry.value > maxIndex) maxIndex = entry.value
  63. }
  64. map[index] = maxIndex
  65. }
  66. var entry: MutableMap.MutableEntry<Int, Int>
  67. val it = map.iterator()
  68. while (it.hasNext()) {
  69. entry = it.next()
  70. max2 = entry.key * entry.value
  71. if (max2 > max) max = max2
  72. if (index < entry.key) it.remove()
  73. else map[entry.key] = entry.value + 1
  74. }
  75. return max
  76. }
  77. fun getMaxFromRaw(matrix: Matrix, raw: Int): Int {
  78. val map = HashMap<Int, Int>()
  79. var max = 0
  80. var max2: Int
  81. var preIndex = matrix[0, raw]
  82. var index: Int
  83. if (preIndex != 0) map[preIndex] = 1
  84. for (c in 1..matrix.column - 1) {
  85. index = matrix[c, raw]
  86. if (index > preIndex) {
  87. map.keys.forEach { key -> map[key] = map[key]!! + 1 }
  88. map[index] = 1
  89. } else if (index == preIndex) {
  90. if (index == 0) continue
  91. map.keys.forEach { key -> map[key] = map[key]!! + 1 }
  92. } else {
  93. max2 = getMaxFromMap(map, index)
  94. if (max2 > max) max = max2
  95. }
  96. if (c == matrix.column - 1 && index != 0) {
  97. max2 = getMaxFromMap(map, index)
  98. if (max2 > max) max = max2
  99. }
  100. preIndex = index
  101. }
  102. return max
  103. }
  104. fun genMaxMatrix(matrix: Matrix): Matrix {
  105. val mm = Matrix(matrix.column, matrix.raw)
  106. for (r in 0..mm.raw - 1)
  107. for (c in 0..mm.column - 1) {
  108. if (matrix[c, r] == 0) mm[c, r] = 0
  109. else if (r == 0) mm[c, r] = 1
  110. else mm[c, r] = mm[c, r - 1] + 1
  111. }
  112. return mm
  113. }
  114. fun countMatrix(matrix: Matrix): Int {
  115. val help = genMaxMatrix(matrix)
  116. var max = 0
  117. var max2: Int
  118. for (r in 0..help.raw - 1) {
  119. max2 = getMaxFromRaw(help, r)
  120. if (max < max2) max = max2
  121. }
  122. return max
  123. }
  124. fun maxArea(matrix: Matrix): Int {
  125. var maxArea = 0
  126. val raw = IntArray(matrix.column)
  127. for (r in 0..matrix.raw - 1) {
  128. for (c in 0..matrix.column - 1)
  129. raw[c] = if (0 == matrix[c, r]) 0 else raw[c] + 1
  130. println(Arrays.toString(raw))
  131. maxArea = Math.max(maxFromRow(raw), maxArea)
  132. }
  133. return maxArea
  134. }
  135. fun maxFromRow(raw: IntArray): Int {
  136. var maxArea = 0
  137. val stack = Stack<Int>()
  138. var j: Int
  139. var k: Int
  140. var curArea: Int
  141. for (i in 0..raw.size - 1) {
  142. while (!stack.isEmpty() && raw[i] <= raw[stack.peek()]) {
  143. j = stack.pop()
  144. k = if (stack.isEmpty()) -1 else stack.peek()
  145. curArea = (i - k - 1) * raw[j]
  146. maxArea = Math.max(maxArea, curArea)
  147. }
  148. stack.push(i)
  149. }
  150. while (!stack.isEmpty()) {
  151. j = stack.pop()
  152. k = if (stack.isEmpty()) -1 else stack.peek()
  153. curArea = (raw.size - k - 1) * raw[j]
  154. maxArea = Math.max(maxArea, curArea)
  155. }
  156. return maxArea
  157. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注