문제
A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
Write a function:
function solution(X, Y, D);
that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.
For example, given:
X = 10 Y = 85 D = 30
the function should return 3, because the frog will be positioned as follows:
- after the first jump, at position 10 + 30 = 40
- after the second jump, at position 10 + 30 + 30 = 70
- after the third jump, at position 10 + 30 + 30 + 30 = 100
Assume that:
- X, Y and D are integers within the range [1..1,000,000,000];
- X ≤ Y.
풀이
X와Y 사이의 거리를 D로 나눈만큼 점프한 후 Y에 도착하지 못했다면 한번 더 점프하도록 구현했다.
function solution(X, Y, D) {
let result = Math.floor((Y-X)/D);
if ((Y-X) % D !== 0) {
result++;
}
return result;
}
다른 풀이
function solution(X, Y, D) {
return Math.ceil((Y - X) / D);
}
참고 https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
'알고리즘' 카테고리의 다른 글
TapeEquilibrium_codility (0) | 2022.03.11 |
---|---|
PermMissingElem_codility (0) | 2022.02.21 |
OddOccurrencesInArray_Codility (0) | 2022.02.16 |
CyclicRotation_Codility (0) | 2022.02.15 |
Remove Nth Node From End of List - leetcode (0) | 2021.11.04 |