+ 1
neend help in following code
The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night. My Attempt😁😬 function main() { var depth = parseInt(readLine(), 10); //your code goes here for (k=5; k<=depth; k++ ){ console.log(k+=5) } }
2 ответов
+ 3
i think this will work too
let output = Math.ceil((input - 2) / 5);
this is O(1) i believe..so it should be faster
+ 2
Hello Bilal Ahmad in your code you're not counting the days.
Also, for loop is not the best choice here in my opinion.
Check this out...
function main() {
var depth = 31, day = 0, k = 0;
//your code goes here
while(k<=depth){
day++; // how many days gone already
k+=7; // climbs up by 7
//check if the snail has reach the well or not
if(k>=depth){
break;
}
k-=2; // slips down by 2
}
console.log(day);
}
main();