[Codility] Lesson 3 - Time Complexity : FrogJmp
Lesson 3 - Time Complexity : FrogJmp
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:
For example, given: 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に単純に加算し、加算回数を求めることで値が得られる.しかしこれを行うと効率的にTIME OUTが発生します. ビットの方式ではなく、他の方法はYからXの距離を減算するのにどれだけのDが必要かによって答えを出す.
📌 質問する
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:📝 に答える
💻 インプリメンテーション
static class Solution{
public int solution(int X, int Y, int D){
return (int) Math.ceil((double)(Y - X) / D);
}
}
Reference
この問題について([Codility] Lesson 3 - Time Complexity : FrogJmp), 我々は、より多くの情報をここで見つけました https://velog.io/@haeny01/Codility-Lesson-3-Time-Complexity-FrogJmpテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol