zoukankan      html  css  js  c++  java
  • leetcode — jump-game-ii

    /**
     * // Source : https://oj.leetcode.com/problems/jump-game-ii/
     *
     * Created by lverpeng on 2017/7/17.
     *
     * 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.)
     *
     */
    public class JumpGame2 {
    
        /**
         * 找到需要跳的次数最少的方法
         *
         * 贪心算法:保证当前是是最优的,以期结果是最优的
         *
         * 先保证能调到最后的最好的解,然后再计算能跳到当前最好解位置的最好解
         *
         * @param arr
         * @return
         */
        public int jump (int[] arr) {
            int end = arr.length - 1;
            int count = 0;
            while (end > 0) {
                for (int i = 0; i < end; i++) {
                    if (i + arr[i] >= end) {
                        count ++;
                        end = i;
                        break;
                    }
                }
            }
            return count;
        }
    
        public static void main(String[] args) {
            JumpGame2 jumpGame2 = new JumpGame2();
            int[] arr = new int[]{2,3,1,1,4};
            System.out.println("2----->" + jumpGame2.jump(arr));
        }
    }
    
  • 相关阅读:
    跳台阶问题
    腾讯,百度,网易游戏,华为笔面经验
    进程、线程、应用程序之间的关系
    const用法小结
    vc快捷键
    文献阅读以及如何管理
    数据类型转换
    vc Debug Release
    如何阅读文献
    如何提高表达能力
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7482776.html
Copyright © 2011-2022 走看看