OneCompiler

Minimum number of jumps to reach the end of an array

253
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OneCompiler
{
	public class Program
	{
		public static void Main(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			
			int[] arr = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			var minimumJumps = MinimumJumps(arr);
			
			Console.WriteLine(minimumJumps);
		}
		
		private static int StringToInteger(string s)
		{
		  return int.Parse(s);
		}
		
		private static int MinimumJumps(int[] arr)
		{
		  int n = arr.Length;
		  
		  if (n == 0 || arr[0] == 0)
		  {
		    return int.MaxValue;
		  }
		  else
		  {
		    int[] jumps = new int[n];
		    
		    jumps[0] = 0;
		    
		    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);
		        }
		      }
		    }
		    
		    return jumps[n - 1];
		  }
		}
	}
}