// 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;
}
[codility]Frog-River-One
最新推荐文章于 2019-08-17 20:17:15 发布
本文介绍了一种使用C++编程解决青蛙跳过障碍物问题的方法,通过优化数据结构和算法,实现了在给定数组中找到青蛙能够到达的最远位置。利用`algorithm`和`climits`库,通过记录每个位置的最早跌落时间,最终确定青蛙能否到达终点并返回其最短路径。

504

被折叠的 条评论
为什么被折叠?



