clueless coding // TODO: be smarter

LeetCode Squirrel Simulation: CPP Solution



TL;DR Code Solution


int distanceBetween(vector<int>& a, vector<int>& b) {
    return abs(a[1] - b[1]) + abs(a[0] - b[0]);
}

int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
    int result = 0;
    int maxDiff = INT_MIN;
    for (int i = 0; i < nuts.size(); i++) {
        int current = distanceBetween(tree, nuts[i]);
        result += 2 * current;
        int startDiff = distanceBetween(squirrel, nuts[i]);
        maxDiff = max(current - startDiff, maxDiff);
    }
    return result - maxDiff;
}