<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 {
private:
  std::vector<std::vector<int>>* arrays_ptr;  

  std::vector<int> local_maxs;
  std::vector<int> local_mins;

  int max_num;
  int min_num;

  int max_distance = 0;

  void find_max(){
    local_maxs.resize(arrays_ptr -> size(), INT_MIN);
    local_mins.resize(arrays_ptr -> size(), INT_MAX);

    for(size_t i = 0; i < arrays_ptr -> size(); i++){
      local_maxs.at(i) = arrays_ptr -> at(i).back();
    }
  }

  void find_min(){
    for(size_t i = 0; i < arrays_ptr -> size(); i++){
      local_mins.at(i) = arrays_ptr -> at(i).at(0);
    }
  }

  // THIS USES GREEDY
  void traverse(){
    min_num = local_mins.at(0);
    max_num = local_maxs.at(0);

    for(int i = 0; i < local_mins.size(); i++){
      if(i != 0){
        std::cout << "min: " << min_num << " local_min: " << local_mins.at(i) << '\n';
        std::cout << "max: " << max_num << " local_max: " << local_maxs.at(i) << '\n';
        std::cout << "local_max - min: " << abs(local_maxs.at(i) - min_num) << '\n';
        std::cout << "max - local_min: " << abs(max_num - local_mins.at(i)) << '\n';
        if(abs(local_maxs.at(i) - min_num > max_distance)){
          max_distance = local_maxs.at(i) - min_num;
        }
        if(abs(max_num - local_mins.at(i) > max_distance)){
          max_distance = max_num - local_mins.at(i);
        }
  
        if(min_num > local_mins.at(i)){
          min_num = local_mins.at(i);
        }
        if(max_num < local_maxs.at(i)){
          max_num = local_maxs.at(i);
        }
      }
    }
  }

  void display(std::vector<int> array){
    for(int element : array){
      std::cout << element << ' ';
    }
    std::cout << '\n';
  }

public:
  int maxDistance(vector<vector<int>>& arrays) {
    arrays_ptr = &arrays;

    find_max();
    find_min();
    traverse();

    return max_distance;
  }
};
109714