Last active
August 29, 2015 14:18
-
-
Save sontl/0213c9237e9dddcfbd9f to your computer and use it in GitHub Desktop.
Codility - Counting: FrogRiverOne
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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