-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathavoidObstacles.js
More file actions
57 lines (38 loc) · 1.4 KB
/
Copy pathavoidObstacles.js
File metadata and controls
57 lines (38 loc) · 1.4 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function solution(inputArray) {
inputArray.sort((a,b) => a-b)
let minimalLengthOfTheJump = 1
const lastPoint = inputArray[inputArray.length-1]
jump()
function jump() {
let pointToJump = minimalLengthOfTheJump
for (let index = 1; pointToJump <= lastPoint; index++) {
pointToJump = minimalLengthOfTheJump * index
if (inputArray.includes(pointToJump)) {
minimalLengthOfTheJump++
jump()
}
}
}
console.log(minimalLengthOfTheJump)
return minimalLengthOfTheJump
}
const inputArray = [2, 3]
solution(inputArray)
// Codewriting
// 300
// You are given an array of integers representing coordinates of obstacles situated on a straight line.
// Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.
// Find the minimal length of the jump enough to avoid all the obstacles.
// Example
// For inputArray = [5, 3, 6, 7, 9], the output should be
// solution(inputArray) = 4.
// Check out the image below for better understanding:
// Input/Output
// [execution time limit] 4 seconds (js)
// [input] array.integer inputArray
// Non-empty array of positive integers.
// Guaranteed constraints:
// 2 ≤ inputArray.length ≤ 1000,
// 1 ≤ inputArray[i] ≤ 1000.
// [output] integer
// The desired length.