C# greedy


  • 0
    Y
    //[2,3,1,1,4],[3,3,1,1,4]
    public class Solution {
        public int Jump(int[] nums) {
            int n=nums.Length;
            if(n<2)
                return 0;
            if(n==2)
                return 1;
            //greedy
            int curIn=n-1;
            int steps=0;
            while(curIn>0)
            {
                for(int i=0;i<curIn;i++)
                {
                    if(nums[i]>=curIn-i)
                    {
                        steps+=1;
                        curIn=i;
                    }
                }
            }
            return steps;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.