Coding Test/Codility
Codility - Frog Jmp 풀이 (java)
dev-tempcru
2022. 1. 5. 14:33
문제
https://app.codility.com/programmers/lessons/3-time_complexity/frog_jmp/
FrogJmp coding task - Learn to Code - Codility
Count minimal number of jumps from position X to Y.
app.codility.com
- 몇번 점프하면 Y보다 같거나 크게 점프 뛸 수 있을까?
접근방법
- X = 출발점
- Y = 도착점
- D = 1회당 점프 길이
- Y - X 한다음에 D로 나누면 몫이 나온다
- 같거나 크게 점프 뛰어야하므로 Y % D가 0이 아닌 경우라면 1번 더 뛰어야 Y보다 커지도록했다
풀이
public class FrogJmp {
public static void main(String[] args) {
int X = 10;
int Y = 85;
int D = 30;
System.out.println(solution(X, Y, D));
}
public static int solution(int X, int Y, int D) {
int result = 0;
// Init
Y = Y - X;
// logic
result = Y/D;
if (Y % D != 0) {
result++;
}
return result;
}
}