-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpRockClimbing.cs
46 lines (39 loc) · 1.18 KB
/
dpRockClimbing.cs
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
using System;
class GFG
{
static int minJumps(int[] arr, int n)
{
// jumps[n-1] will hold the
// result
int []jumps = new int[n];
// if first element is 0,
if (n == 0 || arr[0] == 0)
// end cannot be reached
return int.MaxValue;
jumps[0] = 0;
// Find the minimum number of
// jumps to reach arr[i]
// from arr[0], and assign
// this value to jumps[i]
for (int i = 1; i < n; i++)
{
jumps[i] = int.MaxValue;
for (int j = 0; j < i; j++)
{
if (i <= j + arr[j] && jumps[j] != int.MaxValue )
{
jumps[i] = Math.Min(jumps[i], jumps[j] + 1);
break;
}
}
}
return jumps[n - 1];
}
// Driver program
public static void Main()
{
int []arr = {1, 3, 6, 1, 0, 9};
Console.Write("Minimum number of jumps to reach end is : "+
minJumps(arr,arr.Length));
}
}