Jan 18 - Jump Game; Array; Greedy;

一开始以为是用DP解决,create a boolean array to store the status which mark whether the person can get to the end from current position.

But, by this methods, we would encounter the worst case, which is there‘re hugh number of index and from which person would finally jump to a trap, i.e., nums[target] = 0. In this case, if we use dp, we need to look through all the previous boolean value and the possibility(the index value larger than the current index value). Thus, the runtime is likely O(n^3).

Greedy, we traverse the array from the len-1 to 0 which is same as DP, the only difference is that when we find a nums[i] = 0 index, the boolean value of the index would be false, we then look through all the pos before this trap pos, use a loop to get all the consecutive positions that would eventually jump to the trap and mark the corresponding status to be false.

Code:

public class Solution {
    public boolean canJump(int[] nums) {
        int len = nums.length;
        if(len == 0) return false;
        if(len == 1) return true;
        boolean[] posJump = new boolean[len];
        int i = len-1;
        posJump[i] = true;
        //int target = len-1;

        while(i>=1){
            if(i-1+nums[i-1] >= i){
                posJump[i-1] = true;
                i--;
            }
            else{
                posJump[i-1] = false;
                int sink = i-1;
                i = i-1;
                while(i>=1 && (i-1)+nums[i-1] <= sink){
                    posJump[--i] = false;
                }
                if(i >= 1) posJump[i-1] = true;
                i--;
            }
        }

        return posJump[0];
    }

    /*
    public boolean toPos(int[] nums, int len, int index, boolean[] flag){
        int steps = nums[index];
        if(index + steps >= len-1) {
            flag[index] = true;
            return true;
        }
        if(index < len-1 && steps == 0){
            flag[index] = false;
            return false;
        }
        //int num_step = new int[steps];
        for(int i = 1; i <= steps; i++){
            flag[index] = flag[index] || toPos(nums, len, index+i, flag);
            if(flag[index]) return flag[index];
        }
        return false;
    }
    */
}

  

时间: 2025-02-01 16:48:05

Jan 18 - Jump Game; Array; Greedy;的相关文章

55. Jump Game (Array; Greedy)

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 =

Jan 18 - Maximum Subarray; DAC; DP; Array;

Iteration: public class Solution { public int maxSubArray(int[] nums) { int start = 0, end = nums.length-1; //return sumSubArray(nums, 0, Integer.MIN_VALUE, start, end); int max = Integer.MIN_VALUE; int sum = 0; while(start <= end){ sum += nums[start

Jan 18 - Spiral Matrix; 2D Array;

代码: public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> resultList = new ArrayList<>(); int row = matrix.length; if(row == 0) return resultList; int col = matrix[0].length; if(col == 0) return resultL

JavaScript基础18——js的Array对象

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>js的Array对象</title> 6 <script type="text/javascript"> 7 var arr = [1,2,3,4,5]; 8 document.write("数组:" + arr); 9 //

Jan 11 - Contains Duplicate; Array; Traverse; Integer;

遍历数组找出有没有重复的元素 首先想到用一个数组记录出现的元素的个数 代码: public class Solution { public boolean containsDuplicate(int[] nums) { if(nums.length == 0) return true; int[] checkElement = new int[Integer.MAX_VALUE]; for(int i = 0; i < nums.length; i++){ if(checkElement[num

Jan 19 - Unique Paths; Array; DP;

first of all, we're goin to create a 2D array(int[][] grid) to store the number of paths to each position. we get the minimum value of m and n, mark it as min, then look through the row and column of grid[i][i] in each loop, when i == 0, we know we a

149. Max Points on a Line (Array; Greedy)

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路:对于某一点来说,在经过该点的直线中选取节点数量最多的直线:对于全局来说,必定是某个局部点满足条件的直线之一=>局部最优解也是全局最优解=>贪心法. struct Point { int x; int y; Point() : x(0), y(0) {} Point(int a, int b)

Jan 12 - Summary Ranges;Array;String

Functions of StringBuilder: 代码: public class Solution { public List<String> summaryRanges(int[] nums) { List<String> list = new ArrayList<>(); if(nums.length == 0) return list; int head = 0; int k = -1; StringBuilder sb = new StringBuild

Jan 17 - Permutations; BackTracking; Array; Recursion;

代码: public class Solution { List<List<Integer>> resultList = new ArrayList<>(); boolean flag = false; public List<List<Integer>> permute(int[] nums) { int len = nums.length; if(len == 0) return resultList; Arrays.sort(nums);