Kotlin
\n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/417\n\n#### Problem TLDR\n\nMax area of 1 submatrix after sorting columns optimally\n\n#
![image.png](https://assets.leetcode.com/users/images/18a04410-cce3-43cd-9b69-97d67cbc5378_1700977163.3572178.png)\n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/417\n\n#### Problem TLDR\n\nMax area of `1` submatrix after sorting columns optimally\n\n#### Intuition\n\nUse hint :(\nOk, if we store the heights of the columns we can analyse each row independently, by choosing the largest heights first. The area will be `height * width`, where width will be the current position:\n![image.png](https://assets.leetcode.com/users/images/0bfd7ec0-9bdb-434e-a6ab-d40a236c3812_1700977461.3205564.png)\n\n![image.png](https://assets.leetcode.com/users/images/bf0a2a94-dee9-4f14-8dc9-aee7bd98a40b_1700977446.7752476.png)\n\n#### Approach\n\nWe can reuse the matrix, but don\'t do this in a production code without a warning.\n\n#### Complexity\n\n- Time complexity:\n$$O(nmlog(m))$$\n\n- Space complexity:\n$$O(1)$$\n\n#### Code\n\n```kotlin\n\n  fun largestSubmatrix(matrix: Array<IntArray>): Int {\n    for (y in 1..<matrix.size)\n      for (x in 0..<matrix[y].size)\n        if (matrix[y][x] > 0)\n          matrix[y][x] += matrix[y - 1][x]\n    var max = 0\n    for (row in matrix) {\n      row.sort()\n      for (x in row.lastIndex downTo 0)\n        max = max(max, row[x] * (row.size - x))\n    }\n    return max\n  }\n\n```
['Sorting', 'Kotlin']