• 55. Jump Game(中等)


    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.

    Determine if you are able to reach the last index.

    For example:

    A = [2,3,1,1,4], return true.
    
    A = [3,2,1,0,4], return false.
    

    这不是我们小时候玩的跳棋,所以我理解错了题意.所谓正确题意是,如第一个例子:
    A[0] = 2, 代表 index = 1, 2 的元素我们都可以跳到(我原来以为得必须跳到 index = 2 的位置呢). 我们把能达到的最远索引保存在变量reach中. A[1] = 3, A[2] = 1, 那么选最大的, 则 reach = i + A[i] = 1 + 3.

    说是贪心法.
    精简的意思就是:
    reach 是前沿阵地位置, i 是后方补给当前所在位置, 补给只在 reach 所定的安全区域逐步前行, i 能否到达数组最后一个元素, 完全取决于 reach 所能到达的位置. 但 i 在前行的过程中, 符合条件的话可以更新 reach. 有时候,虽然 i 还没到达数组最后元素, 但当前的reach >= n - 1了, i 就不需要向前走了(循环被 break), 因为reach已经表明共产主义肯定能实现, i 显然就没有向下走的必要了.

    人家想法,自己代码:
    牛就牛在这想法是(O(n))的,而笨想法是(O(n^2))的.
    (O(n)) time, (O(1)) extra space.

    bool canJump(vector<int>& A) {
    	const int n = A.size();
    
    	int i = 0;
    	// 扫描reach范围内, i + A[i] 所能到达的最远位置.
    	// 若能超过当前reach,则更新reach.
    	for (int reach = 0; i < n && i <= reach; i++) {
    		reach = max(i + A[i], reach);
    		if (reach >= n - 1)
    			return true;
    	}
    	// 2个事能导致退出循环, 既上面的循环条件
    	// 不等于n, 只能因为 reach < i, reach 鞭长莫及了
    	return i == n;
    }
    
  • 相关阅读:
    Android属性动画
    android 保存配置文档
    android 不自动弹出虚拟键盘
    android 常用代码
    android imageswitcher gallery 根据数据库内图片名字进行查看/删除
    android 文件内容和 textview 操作
    用 java 语言获取 1N 的不重复随机数
    android 数据库 备份还原
    解决Ubuntu 输入法不显示
    android 添加文本内容到sqlite表中
  • 原文地址:https://www.cnblogs.com/ZhongliangXiang/p/7435759.html
Copyright © 2020-2023  润新知