zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

    Given an array of non-negative integers, you are initially positioned at the first index of the array.

    Each element in the array represents your maximum jump length at that position.

    Your goal is to reach the last index in the minimum number of jumps.

    For example:
    Given array A = [2,3,1,1,4]

    The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

    Subscribe to see which companies asked this question

    思路:

    1)用一个dp数组来记录到达当前位置的最小步数

    package dp;
    
    public class JumpGameII {
    
        public int jump(int[] nums) {        
            int len = nums.length;
            int[] dp = new int[len];
            for (int i = 0; i < len; ++i)
                dp[i] = Integer.MAX_VALUE;
            dp[0] = 0;
            // In order to save time, we don't visit the elements which already been visited.
            int start = 1;
            for (int i = 0; i < len - 1; ++i) {
                start = go(i, nums[i], start, dp);
            }
            
            return dp[len-1];
        }
        
        // Get next start position.
        private int go(int current, int step, int start, int[] dp) {
            int i = start;
            for (; i <= current + step && i < dp.length; ++i) {
                if (dp[i] > dp[current] + 1)
                    dp[i] = dp[current] + 1;
            }
            
            return i;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] nums = { 2,3,1,1,4 };
            JumpGameII j = new JumpGameII();
            System.out.println(j.jump(nums));
        }
    
    }

    2)不用数组,就直接往前移动,第一次扫走一步能到的地方,第二次扫走二步能到的地方,在这个过程中,只要碰到了最后一个位置,就终止,证明是最小步数。

    package dp;
    
    public class JumpGameII {
    
        public int jump(int[] nums) {
            int len; 
            if ((len = nums.length) == 1) return 0;
            int count = 0;
            int start = 0;
            int end = 0;
            while (end < len) {
                int mostRight = 0;
                ++count;
                for (int j = start; j <= end; ++j) {
                    if (j + nums[j] >= len - 1)
                        return count;
                    if (j + nums[j] > mostRight)
                        mostRight = j + nums[j];
                }
                
                start  = end + 1;
                end = mostRight;
            }
            
            return count;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] nums = { 2,3,1,1,4 };
            JumpGameII j = new JumpGameII();
            System.out.println(j.jump(nums));
        }
    
    }
  • 相关阅读:
    weak引用变量是否线程安全
    VMware vSphere HyperVisor安装过程记录
    【转载】VMWare ESXi 5.0和vSphere Client安装和配置
    Wmware桥接网络虚拟机无法上网的问题
    虚拟机移动后重启网络时提示Device does not seem to be present
    Eclipse maven工程 Missing artifact com.sun:tools:jar:1.5.0:system 解决方法
    【转】使用JIRA搭建企业问题跟踪系统【个人推荐】
    使用XAMPP本地安装Wordpress博客
    【转载】维度表和事实表的区别
    maven工程的如何进行代码调试
  • 原文地址:https://www.cnblogs.com/null00/p/5022937.html
Copyright © 2011-2022 走看看