<p>You are given a binary matrix <code>matrix</code> of size <code>m x n</code>, and you are allowed to rearrange the <strong>columns</strong> of the <code>matrix</code> in any order.</p>

<p>Return <em>the area of the largest submatrix within </em><code>matrix</code><em> where <strong>every</strong> element of the submatrix is </em><code>1</code><em> after reordering the columns optimally.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40536-pm.png" style="width: 500px; height: 240px;" />
<pre>
<strong>Input:</strong> matrix = [[0,0,1],[1,1,1],[1,0,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> You can rearrange the columns as shown above.
The largest submatrix of 1s, in bold, has an area of 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40852-pm.png" style="width: 500px; height: 62px;" />
<pre>
<strong>Input:</strong> matrix = [[1,0,1,0,1]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can rearrange the columns as shown above.
The largest submatrix of 1s, in bold, has an area of 3.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[1,1,0],[1,0,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>matrix[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
    int getMax(vector<int>& v, int n) {
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            ans = max(ans, v[i] * (i + 1));
        }
        return ans;
    }

public:
    int largestSubmatrix(vector<vector<int>>& matrix) {
        int n = matrix.size();
        int m = matrix[0].size();
        queue<pair<int, int>> q, temp;
        pair<int, int> value;
        // vector<int> heights(m, 0);
        vector<int> tempVector;
        int cnt;
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (i == 0) {
                for (int j = 0; j < m; ++j) {
                    if (matrix[i][j]) {

                        q.push(make_pair(1, j));
                    }
                }
            } else {
                cnt = 1;
                while (!q.empty()) {
                    value = q.front();
                    ans = max(ans, cnt * value.first);
                    ++cnt;
                    q.pop();
                    if (matrix[i][value.second]) {
                        temp.push(make_pair(value.first + 1, value.second));
                    }
                }
                for (int j = 0; j < m; ++j) {
                    if (matrix[i][j] && (!matrix[i - 1][j])) {
                        temp.push(make_pair(1, j));
                    }
                }
                while (!temp.empty()) {
                    // tempVector.push_back(temp.front().first);
                    q.push(temp.front());
                    temp.pop();
                }
            }

            // for (int j = 0; j < m; ++j) {
            //     if (matrix[i][j]) {
            //         heights[j] += matrix[i][j];
            //     } else {
            //         heights[j] = 0;
            //     }
            // }
            // temp = heights;
            // sort(temp.rbegin(), temp.rend());
            // ans = max(ans, getMax(tempVector, tempVector.size()));
        }
        cnt = 1;
        while (!q.empty()) {
            value = q.front();
            ans = max(ans, cnt * value.first);
            ++cnt;
            q.pop();
        }
        return ans;
    }
};
96000