[codility]Frog-River-One

来源:互联网 发布:字幕软件排行 编辑:程序博客网 时间:2024/05/08 03:13
// you can also use includes, for example:// #include <algorithm>#include<climits>int solution(int X, vector<int> &A) {    // write your code in C++98    //...out of range case    if(X > A.size()) return -1;    //...keep record of the earlist falling time of each position    vector<int> timeOfPos(X+1, -1);    for(int i = 0; i < A.size(); ++i)        timeOfPos[A[i]] = timeOfPos[A[i]] == -1 ? i : min(i, timeOfPos[A[i]]);    //...check if the frog can reach to the otherside    int maxSingleTime = INT_MIN;    for(int i = 1; i < timeOfPos.size(); ++i)        if(timeOfPos[i] == -1) return -1;        else maxSingleTime = max(timeOfPos[i], maxSingleTime);    //...return the result    return maxSingleTime;}

原创粉丝点击