[C++] Linear Solution Explained, ~100% Time, ~30% Space
I am not sure how I am supposed to use less memory than this, but okay.\n\nThe idea is pretty simple and we just have to work through the series incrementing ou
I am not sure how I am supposed to use less memory than this, but okay.\n\nThe idea is pretty simple and we just have to work through the series incrementing our accumulator variable `res` for all the elements minus the last one so that `res` increases as the minimum value between the distance with the next point in time (`s[i + 1] - s[i]`) and the duration (`d`).\n\nIn order for it to work, we need to deal with the edge case of an empty series (no attacks => `0` damage) and finally we need to return `res + d`, since the last attack will always be in full :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    int findPoisonedDuration(vector<int>& s, int d) {\n        // support variables\n        int res = 0, len = s.size() - 1;\n        // edge case\n        if (len == -1) return res;\n        // computing the overall poison for all attacks but last\n        for (int i = 0; i < len; i++) res += min(s[i + 1] - s[i], d);\n        return res + d;\n    }\n};\n```
['C', 'C++']