| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
- html 기초
- 반응형웹
- ObjectOutputStream
- 미디어쿼리
- iframe 태그
- 퍼셉트론
- 반응형 웹 프로젝트
- oracle
- html 프로젝트
- 예제
- inline
- rnn
- java
- FileWriter
- HTML
- CSS
- Database
- Codility
- BorderLayout
- g검정
- 푸리에 변환
- 메서드
- 사전학습
- FFT
- 상속
- css 기초
- 파이썬
- Position
- GridLayout
- FlowLayout
- Today
- Total
도라에몽주머니
[Codility/Java] 문제 4. FrogJmp 본문

Task
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:
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.
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
Write an efficient algorithm for the following assumptions:
- X, Y and D are integers within the range [1..1,000,000,000];
- X ≤ Y.
Solution
class Solution {
public int solution(int X, int Y, int D) {
int dist = Y - X;
int result = 0;
if(dist <= 0) result = 0;
if(dist%D == 0) result = dist/D;
else result = dist/D + 1;
return result;
}
}
Notes
처음에는 Y가 X보다 작거나 같은 경우를 고려하지 못해서 정확도가 88%밖에 되지 않았다.
코드를 제출하고 문제를 발견해 다시 정확도를 100%로 올렸지만 FrogJmp 같은 비교적 단순한 문제는 실수를 줄이도록 예외처리에 좀 더 신경써야겠다.
'Algorithm' 카테고리의 다른 글
| [Codility/Java] 문제 6. TapeEquilibrium (0) | 2022.12.06 |
|---|---|
| [Codility/Java] 문제 5. PermMissingElem (0) | 2022.11.24 |
| [Codility/Java] 문제 3. OddOccurrencesInArray (0) | 2022.11.22 |
| [Codility/Java] 문제 2. CyclicRotation (0) | 2022.11.17 |
| [Codility/Java] 문제 1. BinaryGap (0) | 2022.11.04 |