Skip to content

Instantly share code, notes, and snippets.

@ShubhamRwt
Created July 21, 2023 16:58
Show Gist options
  • Save ShubhamRwt/31158a3e4a8983f36e8c176fd04657ee to your computer and use it in GitHub Desktop.
Save ShubhamRwt/31158a3e4a8983f36e8c176fd04657ee to your computer and use it in GitHub Desktop.
answer
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