1. 程式人生 > >LeetCode題解:Teemo Attacking

LeetCode題解:Teemo Attacking

In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.

You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.

思路: 首先假定所有時間下的攻擊都帶來完全的中毒,之後再把重複的地方去掉。 題解:
int findPoisonedDuration(const std::vector<int>& timeSeries, int duration) {
    int accumulated = duration * timeSeries.size();
    int lastAttackEnd = std::numeric_limits<int>::min();

    for(auto time: timeSeries) {
        if (lastAttackEnd > time) {
            accumulated -= lastAttackEnd - time;
        }
        lastAttackEnd = time + duration;
    }

    return accumulated;
}