Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
855 B
4
Indexable
class Solution {
public:
    int maxDistance(vector<vector<int>>& arrays) {
        int ans = 0, szFirstArray = arrays[0].size();
        int minFirst = arrays[0][0], minSec = arrays[0][szFirstArray -1], maxFirst = minFirst, maxSec = minSec;
        
        for(int i=1;i<arrays.size();i++) {
            int sz = arrays[i].size();
            
            int fst = max(abs(arrays[i][0] - minSec), abs(arrays[i][0] - maxSec));
            int sec = max(abs(arrays[i][sz-1] - minFirst), abs(arrays[i][0] - maxFirst));
            
            minFirst = min(minFirst, arrays[i][0]);
            maxFirst = max(maxFirst, arrays[i][0]);
            minSec = min(minSec, arrays[i][sz-1]);
            maxSec = max(maxSec, arrays[i][sz-1]);
            
            ans = max(ans , max(fst, sec));
            
        }
        
        return ans;
    }
};
Leave a Comment