상세 컨텐츠

본문 제목

Codility Lesson3 - FrogJmp

코딩테스트/코딜리티

by 허허지니 2023. 8. 8. 11:00

본문

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.

작은 개구리는 도로의 다른 쪽으로 가고 싶어합니다. 그 개구리는 현재 위치 X에 위치하고 있고 Y보다 크거나 같은 위치에 가고 싶어합니다. 그 작은 개구리는 항상 고정된 거리인 D를 점프합니다.

Count the minimal number of jumps that the small frog must perform to reach its target.

작은 개구리가 목표물에 도달하기 위해 수행해야 하는 최소 점프 횟수를 계산합니다.

Write a function:

class Solution { public int solution(int X, int Y, int 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.

세 개의 정수 X, Y, D가 주어지면 위치 X에서 Y 이상의 위치로 최소 점프 수가 반환됩니다.

For example, given:

  X = 10
  Y = 85
  D = 30
the function should return 3, because the frog will be positioned as follows:

이 함수는 다음과 같이 위치하기 때문에 3을 반환해야 합니다:

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
Write an efficient algorithm for the following assumptions:

다음 가정에 대한 효율적인 알고리즘을 작성합니다:

X, Y and D are integers within the range [1..1,000,000,000];
X ≤ Y.

X, Y 및 D는 [1..1,000,000,000,000] 범위 내의 정수입니다;
X ≤ Y입니다.


2023.08.08
class Solution {
    public int solution(int X, int Y, int D) {
        int answer = 0;
        int distance = Y-X;

        answer = distance / D;

        if(distance % D > 0) {
            answer += 1;
        }

        return answer;
    }
}

관련글 더보기