30 Day Challenge Day 17 | Leetcode 624. Maximum Distance in Arrays
题解
Easy
我倒是觉得不太容易,即使看了答案,也不确定这是令人信服的解法。直到看了动态图的讲解,所以还是画图更直观。
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
int res = 0, min_val = arrays[0][0], max_val = arrays[0][arrays[0].size()-1];
for(int i = 1; i < arrays.size(); i++) {
res = max(res, max(arrays[i][arrays[i].size()-1] - min_val, max_val - arrays[i][0]));
min_val = min(min_val, arrays[i][0]);
max_val = max(max_val, arrays[i][arrays[i].size()-1]);
}
return res;
}
};