-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson03(TimeComplexity)-FrogJmp.cpp
More file actions
28 lines (25 loc) · 1.03 KB
/
Copy pathLesson03(TimeComplexity)-FrogJmp.cpp
File metadata and controls
28 lines (25 loc) · 1.03 KB
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
// 1. 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:
* 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.
*
* Write an efficient algorithm for the following assumptions:
* • X, Y and D are integers within the range [1..1,000,000,000];
* • X ≤ Y.
*/
#include <cmath>
int frogJmp(int X, int Y, int D)
{
// Divide the distance to be covered (Y - X) by the distance of each jump (D).
// Use std::ceil to round up the result to the nearest integer.
// This ensures that the frog reaches or surpasses the target position.
return std::ceil((Y - X) / (double)D);
}