Coding Test – Snail wants to climb

https://www.acmicpc.net/problem/2869

In the beginning, I thought it’s so simple test because one while loop can solve it.

But after I failure the test cases, I check one more time and they want “0.25” second of optimization.

Spoiler

I…


This content originally appeared on DEV Community and was authored by JaeHonity

https://www.acmicpc.net/problem/2869

test1

Image description

In the beginning, I thought it's so simple test because one while loop can solve it.

But after I failure the test cases, I check one more time and they want "0.25" second of optimization.

Spoiler

I found the math logic.

each day, Snail climbs up and fell down, but other way, Snail is fell down and climb up.

So there's this logic is available.

n = days

-fall * (n - 1) + climb  * n >= max
-fall * n + fall + climb * n >= max
-fall * n + climb * n >= max - fall
(climb-fall) * n >= max - fall
n = (max - fall) / (climb - fall)

and without loop, just one time of calculation is solving the problem!


This content originally appeared on DEV Community and was authored by JaeHonity


Print Share Comment Cite Upload Translate Updates
APA

JaeHonity | Sciencx (2025-07-02T05:44:22+00:00) Coding Test – Snail wants to climb. Retrieved from https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/

MLA
" » Coding Test – Snail wants to climb." JaeHonity | Sciencx - Wednesday July 2, 2025, https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/
HARVARD
JaeHonity | Sciencx Wednesday July 2, 2025 » Coding Test – Snail wants to climb., viewed ,<https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/>
VANCOUVER
JaeHonity | Sciencx - » Coding Test – Snail wants to climb. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/
CHICAGO
" » Coding Test – Snail wants to climb." JaeHonity | Sciencx - Accessed . https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/
IEEE
" » Coding Test – Snail wants to climb." JaeHonity | Sciencx [Online]. Available: https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/. [Accessed: ]
rf:citation
» Coding Test – Snail wants to climb | JaeHonity | Sciencx | https://www.scien.cx/2025/07/02/coding-test-snail-wants-to-climb/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.