zoukankan      html  css  js  c++  java
  • leetcode: 贪心

    1. 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[i]值代表,当前能跳的最大值,并不是一定要跳这么远的,比如A[0]=3,那么我们可以跳0到3步。判定这里的判断结束条件应该是:

    记录lastJump的位置,循环i到lastJump,所有位置,找出其中的最大值maxJump =max{ i+A[i]};如果这个区间内的最大值maxJump <= lastJump,那么就返回假,因为没有任何一个位置可以跳出这个位置。

    如果没有这样的位置,一直可以跳到最后,那么就返回为真。

    public boolean canJump(int[] A) {
        if(A.length <= 1)
            return true;
     
        int max = A[0]; //max stands for the largest index that can be reached.
     
        for(int i=0; i<A.length; i++){
            //if not enough to go to next
            if(max <= i && A[i] == 0) 
                return false;
     
            //update max    
            if(i + A[i] > max){
                max = i + A[i];
            }
     
            //max is enough to reach the end
            if(max >= A.length-1) 
                return true;
        }
     
        return false;    
    }
    View Code

    2. jump-game-ii

    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 is2. (Jump1step from index 0 to 1, then3steps to the last index.)

    给定一个非负整数数组,给定的初始化位置在数组的起始位置。数组中的每个元素代表着你能都在此位置跳跃的最大的距离。你的目标是用最少的跳跃数达到数组的末尾。

    比如:给定A = [2,3,1,1,4]达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)

    public int jump(int[] A) {
            if(A==null||A.length==0)
                return 0;
            
            int maxcover = 0;
            int step = 0;
            int lastcover = 0;
            for(int i = 0; i<=maxcover&&i<A.length;i++){
                if(i>lastcover){
                    step++;
                    lastcover = maxcover;
                }
                
                if(A[i]+i>maxcover)
                    maxcover = A[i]+i;
            }
            
            if(maxcover<A.length-1)
                return 0;
            return step;
        }
    View Code

    3. maximum subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

    For example, given the array[−2,1,−3,4,−1,2,1,−5,4],
    the contiguous subarray[4,−1,2,1]has the largest sum =6.

    More practice:

    If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

     这道题要求 求连续的数组值,加和最大。

     试想一下,如果我们从头遍历这个数组。对于数组中的其中一个元素,它只有两个选择:

     1. 要么加入之前的数组加和之中(跟别人一组)

     2. 要么自己单立一个数组(自己单开一组)

     所以对于这个元素应该如何选择,就看他能对哪个组的贡献大。如果跟别人一组,能让总加和变大,还是跟别人一组好了;如果自己起个头一组,自己的值比之前加和的值还要大,那么还是自己单开一组好了。

    所以利用一个sum数组,记录每一轮sum的最大值,sum[i]表示当前这个元素是跟之前数组加和一组还是自己单立一组好,然后维护一个全局最大值即位答案。

    public int maxSubArray(int[] A) {
            int[] sum = new int[A.length];
            
            int max = A[0];
            sum[0] = A[0];
     
            for (int i = 1; i < A.length; i++) {
    
                sum[i] = Math.max(A[i], sum[i - 1] + A[i]);
    
                max = Math.max(max, sum[i]);
    
            }
     
            return max;
        }
    View Code

     

  • 相关阅读:
    OpenStack Nova Release(Rocky to Train)
    5G 与 MEC 边缘计算
    Cinder LVM Oversubscription in thin provisioning
    浅析视频云行业及实现技术
    虚拟化技术实现 — KVM 的 CPU 虚拟化
    虚拟化技术实现 — QEMU-KVM
    云计算与虚拟化技术发展编年史
    计算机组成原理 — FPGA 现场可编程门阵列
    Installutil.exe 注册exe
    ASP.NET MVC- 布署
  • 原文地址:https://www.cnblogs.com/zxqstrong/p/5386232.html
Copyright © 2011-2022 走看看