首页 > 学院 > 开发设计 > 正文

算法设计与应用基础:第一周(1)

2019-11-08 00:55:44
字体:
来源:转载
供稿:网友

495. Teemo Attacking

Description Submission SolutionsTotal Accepted: 4627Total Submissions: 8953Difficulty: MediumContributors: love_FDU_llp

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.

My solution

codes:

class Solution {public:    int findPoisonedDuration(vector<int>& timeSeries, int duration) {        int size=timeSeries.size();        int ans=size*duration;        for(int i=1;i<size;i++)        {            if(timeSeries[i]-timeSeries[i-1]<duration)            {                ans-=duration-(timeSeries[i]-timeSeries[i-1]);            }        }        //int ans1=ans;        return ans;    }};

解题思路:循环遍历timeSeries每找到相邻两个数字差小于duration,就修改相应的ans值(初始值设为size*duration)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表