FrogRiverOne

Find the earliest time when a frog can jump to the other side of a river

My solution written in C++

#include <vector>

using namespace std;

int solution(int X, vector<int> &A) {
   
   long long sum = (long long) X* ((long long) (X+1)) / 2;

   vector<bool> seen(X);

   for ( size_t i = 0; i < A.size(); ++i )
   {
      int item = A[i];
      if ( ! seen[item - 1] )
      {
         seen[item-1] = true;
         sum -= item;

         if ( sum < 1 )
            return i;
      }
   }

   return -1;
}
Comments