Problem Statement
You have survived a shipwreck and need to swim to the land. You can swim at a speed of at most V meters per second. The ocean's stream moves you with a constant speed of U meters per second.
Imagine a Cartesian system of coordinates in which you are at point (0,0), and the ocean's stream moves in the positive direction of the x-axis.
You are given four integers x, y, V and U. Return a double representing the minimum time in seconds in which you can reach point (x, y).
If this point can't be reached, return -1.
Definition
- Class:
- Survived
- Method:
- minTime
- Parameters:
- int, int, int, int
- Returns:
- double
- Method signature:
- double minTime(int x, int y, int V, int U)
- (be sure your method is public)
Notes
- Your return value must have an absolute or relative error less than 1e-9.
Constraints
- x and y will be between -100 and 100, inclusive.
- U and V will be between 0 and 100, inclusive.
Examples
1
-1
1
1
Returns: 1.0
If you swim directly down (in the negative direction of the y-axis) for one second, you will have gone forward one meter. By that time, the ocean's stream will have taken you one meter in the positive direction of the x-axis, leaving you at point (1, -1).1
1
1
0
Returns: 1.4142135623730951
1
1
0
1
Returns: -1.0
9
3
2
3
Returns: 2.0593413823019864
0
0
0
0
Returns: 0.0
5
12
24
26
Returns: 1.3