Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 879 Bytes

FrogRiverOne.md

File metadata and controls

25 lines (19 loc) · 879 Bytes

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

Solution (JavaScript)

Similar to the previous problem, our approach is to loop through A and everytime we encounter a position store it in a second array along with the time, effectively reversing the array. We can then loop through the second array and find the maximum time value before we get to X

Test Score: 100%

function solution(X, A) {
    let B = []
    
    for (let i = 0; i < A.length; ++i) {
        if ( !B[A[i]] ) B[A[i]] = i
    }
    
    let max_time = -1
    for (let i = 1; i <= X; ++i) {
        if ( typeof B[i] === "undefined" ) return -1
        if ( B[i] > max_time ) max_time = B[i] 
    }
    
    return max_time
}