Skip to content

Instantly share code, notes, and snippets.

@sontl
Last active August 29, 2015 14:18
Show Gist options
  • Save sontl/0213c9237e9dddcfbd9f to your computer and use it in GitHub Desktop.
Save sontl/0213c9237e9dddcfbd9f to your computer and use it in GitHub Desktop.
Codility - Counting: FrogRiverOne
function solution(X, A) {
// write your code in JavaScript (Node.js 0.12)
var count = new Array(X);
for ( var i=0; i < A.length; i++) {
var info = {
minute : i,
count : 1
};
if (!count[A[i]])
count[A[i]] = info;
else {
count[A[i]].count ++;
if (count[A[i]].minute > info.minute) {
count[A[i]]. minute = info.minute;
}
}
}
var latestTime = 0;
for ( var i=1; i < count.length; i++) {
if (!count[i]) {
return -1
} else {
if (latestTime < count[i].minute ) {
latestTime = count[i].minute;
}
}
}
return latestTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment