-
-
Save ShubhamRwt/31158a3e4a8983f36e8c176fd04657ee to your computer and use it in GitHub Desktop.
answer
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 canReachDestination(trackLength, spells, k) { | |
let currentSpeed = 0; | |
let timeTaken = 0; | |
for (const spell of spells) { | |
currentSpeed = Math.min(k, spell); // Set the current speed to either k or the spell's speed, whichever is smaller | |
timeTaken += 1; | |
const remainingDistance = trackLength - timeTaken * currentSpeed; | |
if (remainingDistance <= 0) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function minimumSpeedToReachDestination(trackLength, spells) { | |
spells.sort((a, b) => a - b); // Sort the spells array in ascending order | |
let left = 0; | |
let right = Math.max(...spells) * trackLength; | |
while (left < right) { | |
const mid = Math.floor((left + right) / 2); | |
if (canReachDestination(trackLength, spells, mid)) { | |
right = mid; | |
} else { | |
left = mid + 1; | |
} | |
} | |
return left; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment