<p>You are given <code>m</code> <code>arrays</code>, where each array is sorted in <strong>ascending order</strong>.</p>

<p>You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers <code>a</code> and <code>b</code> to be their absolute difference <code>|a - b|</code>.</p>

<p>Return <em>the maximum distance</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arrays = [[1,2,3],[4,5],[1,2,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
</pre>

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

<pre>
<strong>Input:</strong> arrays = [[1],[1]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>m == arrays.length</code></li>
	<li><code>2 &lt;= m &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arrays[i].length &lt;= 500</code></li>
	<li><code>-10<sup>4</sup> &lt;= arrays[i][j] &lt;= 10<sup>4</sup></code></li>
	<li><code>arrays[i]</code> is sorted in <strong>ascending order</strong>.</li>
	<li>There will be at most <code>10<sup>5</sup></code> integers in all the arrays.</li>
</ul>

class Solution {
public:
    int maxDistance(vector<vector<int>>& arrays) {
        int n = arrays.size();
        std::vector<int> a(n, 0);
        std::vector<int> b(n, 0);

        for(int i = 0; i < n; ++i) {
            a[i] = arrays[i].front();
            b[i] = arrays[i].back();
        }

        std::vector<int> left_min_a(n, std::numeric_limits<int>::max());
        std::vector<int> left_min_b(n, std::numeric_limits<int>::max());
        std::vector<int> right_min_a(n, std::numeric_limits<int>::max());
        std::vector<int> right_min_b(n, std::numeric_limits<int>::max());

        left_min_a[0] = a[0];
        left_min_b[0] = b[0];
        for(int j = 2; j < n; ++j) {
            left_min_a[j-1] = std::min(left_min_a[j-2], a[j-1]);
            left_min_b[j-1] = std::min(left_min_b[j-2], b[j-1]);
        }

        right_min_a[n-1] = a[n-1];
        right_min_b[n-1] = b[n-1];
        for(int j = n - 2; j >= 0; --j) {
            right_min_a[j] = std::min(right_min_a[j+1], a[j]);
            right_min_b[j] = std::min(right_min_b[j+1], b[j]);
        }

        int result {};
        result = std::max({
            result,
            a[0] - right_min_b[1],
            b[0] - right_min_a[1]
        });

        for(int index = 1; index < n; ++index) {
            result = std::max(
                {
                    result,
                    b[index] - left_min_a[index-1],
                    index + 1 < n ? b[index] - right_min_a[index+1] : 0,
                    a[index] - left_min_b[index-1],
                    index + 1 < n ? a[index] - right_min_b[index+1] : 0
                }
            );
        }

        return result;
    }
};
114370