"Java algorithm" greedy algorithm

Greedy algorithm (also known as the greedy algorithm) means that, when the problem solving, always made in the current appears to be the best choice. In other words, not be considered as a whole the best, he made is the optimal solution in the sense of the local.

A classic case of the greedy algorithm:

Jumping game:

Given a non-negative integer array, you initially located in the first position of the array. Each element in the array represents the maximum length you can jump in that position. Your goal is to use the least number of hops to reach the last position of the array.

For example: [2,3,1,1,4,2,2,1] Obviously shortest route: 2 skip position 3, and then jump to the position 4, can then skip to the last.

:( algorithm thinking green circle indicates the current position, orange indicates to achieve the maximum distance)

2 starting from the first number, to achieve a position of an orange. You can reach the farthest position changes.

Green went 3 position, this position can reach the farthest position becomes the position of the orange 4, the farthest position change, plus a number of steps.

Continue to go green 1 position, this position can reach the farthest position inferior position 4 of the orange, the farthest position does not change, the same number of steps.

Continue to go green 1 position, this position can reach the farthest position inferior position 4 of the orange, the farthest position does not change, the same number of steps.

 

Continue to go green 4 position. This position can reach the farthest position to position 1 orange, the farthest position change, plus a number of steps, and has reached the end, the end of the cycle.

The algorithm:

Time complexity: O (n-) O ( n- ).

Space complexity: O (. 1) O ( . 1 ).

 

code show as below:

public  class Subject92 { 

    public  static  void main (String [] args) {
         int [] = arrInt new new  int [] 2,3,1,1,4,2,1 { }; 
        System.out.println ( new new Subject92 () .jump (arrInt)); 
    } 

    public  int jump ( int [] the nums) {
         // less than or equal to 1 do not need to jump 
        int lengths = nums.length;
         IF (lengths <= 1 ) {
             return 0 ; 
        } 
        int REACH = 0;   // current can go the farthest distance 
        intthe nums = nextreach [0 ];
         int STEP = 0;   // required number of steps 
        for ( int I = 0; I <lengths; I ++ ) {
             // greedy algorithm Core: This step is not possible to obtain more than the number of steps on step , you can then take the new route. 
            = Math.max nextreach (I + the nums [I], nextreach);
             IF (nextreach> =-lengths. 1) return (+ STEP. 1 );
             IF (I == REACH) { 
                STEP ++ ; 
                REACH = nextreach; 
            } 
        } 
        return STEP; 
    } 
}

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/jump-game-ii
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/jssj/p/11815328.html