2020 Buckle Cup! Code Your Future Spring National Programming Competition Individual Competition

Four of the five questions were done. The fourth question was thought to be overtime. Finally, with the mentality to try it, it passed.

Looking at the ranking a little bit, starting from 98, all contestants who only did four questions, it seems that the problem solving time is still very critical.

Record the entry.

The following questions are all from the leetcode website: https://leetcode-cn.com/

Take coins

https://leetcode-cn.com/contest/season/2020-spring/problems/na-ying-bi/

Table there is nheap currency buckle force, the number of each heap stored in an array coinsin. We can choose any pile at a time, take one or two of them, and find the minimum number of times to get all the deductions.

Example 1:

Enter:[4,2,1]

Output:4

Interpretation: The first pile of power deductions needs to be taken at least 2 times, the second pile needs to be taken at least once, the third pile needs to be taken at least once, a total of 4 times to get it.

Example 2:

Enter:[2,3,10]

Output:8

limit:

  • 1 <= n <= 4
  • 1 <= coins[i] <= 10

Nothing to say, check-in questions, each pile is calculated separately.

    public int minCount(int[] coins) {
        int sum = 0;
        for(int i = 0; i < coins.length; i++){
            sum += (coins[i] + 1) / 2;
        }
        return sum;
    }

2. Pass the message

https://leetcode-cn.com/contest/season/2020-spring/problems/chuan-di-xin-xi/

Child A is playing an information game with his friends. The rules of the game are as follows:

  1. There are n players, all the player numbers are 0 ~ n-1, and the number of the child A is 0
  2. Each player has a fixed number of other players who may transmit information (or may not). The relationship of transmitting information is unidirectional (for example, A can transmit information to B, but B cannot transmit information to A).
  3. Each round of information must be passed to another person, and the information can pass through the same person repeatedly

Given the total number of players n, as well as by [玩家编号,对应可传递玩家编号]two-dimensional array consisting of relationship relation. Small return information A (number 0) through the ktransmission to the partner number is the number of small programs at n-1 wheel; if not reach, returns 0.

Example 1:

Enter:n = 5, relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]], k = 3

Output:3

Explanation: The message starts at the small A number 0 and passes through 3 rounds to reach the number 4. There are 3 schemes, namely 0-> 2-> 0-> 4, 0-> 2-> 1-> 4, 0-> 2-> 3-> 4.

Example 2:

Enter:n = 3, relation = [[0,2],[2,1]], k = 2

Output:0

Explanation: The message cannot be passed to the number 2 from small A through 2 rounds

limit:

  • 2 <= n <= 10
  • 1 <= k <= 5
  • 1 <= relation.length <= 90, 且 relation[i].length == 2
  • 0 <= relation[i][0],relation[i][1] < n 且 relation[i][0] != relation[i][1]

Dynamic planning: use a DP array to record, dp [i] [j] records the i-th round, and transmits the plan to the j-th child.

State transition equation

 

 

Where path [k] [j] indicates whether there is a direct transmission path from the child with the number k to the child with the number j, if so, then path [k] [j] = 1, if not, then path [k] [j] = 1.

public int numWays(int n, int[][] relation, int k) {
    int ans = 0;
    int[][] dp = new int[k+1][n];
    dp[0][0] = 1;
    for(int i = 1; i <= k; i++){
        for(int j = 0; j < n; j++){
            for(int p = 0; p < relation.length; p++){
                if(relation[p][1] == j){
                    dp[i][j] += dp[i-1][relation[p][0]];
                }
            }
        }
    }
    return dp[k][n-1];
}

3. Story trigger time

https://leetcode-cn.com/contest/season/2020-spring/problems/ju-qing-hong-fa-shi-jian/

In strategy games, players often need to develop their own forces to trigger various new plots. There are three main attributes of a force, namely civilization level ( C), resource reserve ( R) and population ( H). At the beginning of the game (day 0), the values ​​of the three attributes are all 0.

As the game progresses, the player's three properties each day will be a corresponding increase , we use a two-dimensional array increaseto represent the increase each day. Each element of this array is a two-dimensional one-dimensional array of length 3, for example, [[1,2,1],[3,4,2]]the first day of three properties are increased 1,2,1while the increase in the next day, respectively 3,4,2.

All trigger conditions also used the story of a two-dimensional array requirementsrepresentation. Each element of this array is a two-dimensional one-dimensional array of length 3, the conditions for triggering a story c[i], r[i], h[i], if the current C >= c[i]and R >= r[i]and H >= h[i]then the story will be triggered.

According to the information given, please calculate the trigger time of each plot and return it as an array. If a certain plot will not be triggered, the corresponding trigger time of the plot is -1.

Example 1:

Enter: increase = [[2,8,4],[2,5,0],[10,9,8]] requirements = [[2,11,3],[15,10,7],[9,17,12],[8,1,14]]

Output: [2,-1,3,-1]

Explanation:

Initially, C = 0, R = 0, H = 0

On day 1, C = 2, R = 8, H = 4

On the second day, C = 4, R = 13, H = 4, then trigger the plot 0

On the third day, C = 14, R = 22, H = 12, and then trigger the plot 2

Plots 1 and 3 cannot be triggered.

Example 2:

Enter: increase = [[0,4,5],[4,8,8],[8,6,1],[10,10,0]] requirements = [[12,11,16],[20,2,6],[9,2,6],[10,18,3],[8,14,9]]

Output: [-1,4,3,3,3]

Example 3:

Enter: increase = [[1,1,1]] requirements = [[0,0,0]]

Output: [0]

limit:

  • 1 <= increase.length <= 10000
  • 1 <= requirements.length <= 100000
  • 0 <= increase[i] <= 10
  • 0 <= requirements[i] <= 100000

The inscription is not good, the code looks too redundant and unsightly, and the readability is poor.

The specific idea is that all requirements are defined as a class in the form of (subscript, demand), and later I feel that it is not necessary to define it requirements.length. It can be done with three two-dimensional arrays int [ ] [2]. Then according to the order of the three resources, from small to large.

Use a map to record the time when the requirements meet a certain resource requirement (it seems to be an array here, because the length is fixed, there is no need to hash), and a times array to record the number of times the requirements meet the resource.

If a certain requirement meets three resource requirements, that is, times [i] == 3, then the time recorded in the map to satisfy the last of the three resources is the satisfaction time of the entire requirements.

public class Requirements{

    int index;
    int[] requirements;

    public Requirements(int index, int[]requirements){
        this.index = index;
        this.requirements = requirements;
    }

}

public int[] getTriggerTime(int[][] increase, int[][] requirements) {
    int[] ans = new int[requirements.length];
    Arrays.fill(ans,-1);
    Requirements[] requirements1 = new Requirements[requirements.length];
    Requirements[] requirements2 = new Requirements[requirements.length];
    Requirements[] requirements3 = new Requirements[requirements.length];
    HashMap<Integer,Integer> hashmap = new HashMap<>();
    int[] times = new int[requirements.length];

    for (int i = 0; i < requirements.length; i++) {
        if (requirements[i][0] == 0 && requirements[i][1] == 0 && requirements[i][2] == 0){
            ans[i] = 0;
        }
        requirements1[i] = new Requirements(i,requirements[i]);
        requirements2[i] = new Requirements(i,requirements[i]);
        requirements3[i] = new Requirements(i,requirements[i]);
    }
    Arrays.sort(requirements1, new Comparator<Requirements>() {
        @Override
        public int compare(Requirements o1, Requirements o2) {
            return o1.requirements[0] - o2.requirements[0];
        }
    });
    Arrays.sort(requirements2, new Comparator<Requirements>() {
        @Override
        public int compare(Requirements o1, Requirements o2) {
            return o1.requirements[1] - o2.requirements[1];
        }
    });
    Arrays.sort(requirements3, new Comparator<Requirements>() {
        @Override
        public int compare(Requirements o1, Requirements o2) {
            return o1.requirements[2] - o2.requirements[2];
        }
    });

    int C = 0, R = 0, H = 0;
    int in1 = 0;
    int in2 = 0;
    int in3 = 0;
    for (int i = 0; i < increase.length; i++) {
        C += increase[i][0];
        R += increase[i][1];
        H += increase[i][2];
        while (in1 < requirements.length && C >= requirements1[in1].requirements[0]){
            times[requirements1[in1].index]++;
            hashmap.put(requirements1[in1].index,i+1);
            in1++;
        }
        while (in2 < requirements.length && R >= requirements2[in2].requirements[1]){
            times[requirements2[in2].index]++;
            hashmap.put(requirements2[in2].index,i+1);
            in2++;
        }
        while (in3 < requirements.length && H >= requirements3[in3].requirements[2]){
            times[requirements3[in3].index]++;
            hashmap.put(requirements3[in3].index,i+1);
            in3++;
        }
    }
    for (int i = 0; i < requirements.length; i++) {
        if (times[i] == 3 && ans[i] == -1){
            ans[i] = hashmap.get(i);
        }
    }
    return ans;
}

4. Minimum number of jumps

https://leetcode-cn.com/contest/season/2020-spring/problems/zui-xiao-tiao-yue-ci-shu/

In order to give some rewards to the students who brushed the questions, the buckle team introduced a spring game machine. By a gaming machine Nspecial springs in a row, numbered 0to N-1. There is a small ball in the initial number 0of the spring at. If the ball number is in the ispring at, by pressing the spring, the ball can choose the right ejection  jump[i]distance, to the left or left ejection spring arbitrary position. That is, the number of ipressing springs, the pellets can shells 0to i-1any spring or i+jump[i]springs (if i+jump[i]>=N, then eject the ball machine). When the ball is at the spring number 0, it can no longer bounce to the left.

In order to get a reward, you need to pop the ball out of the machine. Request a minimum of how many times the press spring, the ball may be numbered from 0the pop-up the entire machine spring, that number right across the N-1spring.

Example 1:

Enter:jump = [2, 5, 1, 1, 1, 1]

Output:3

Explanation: The small Z needs to press the spring at least 3 times, the order in which the balls arrive in order is 0-> 2-> 1-> 6, and finally the ball pops out of the machine.

limit:

  • 1 <= jump.length <= 10^6
  • 1 <= jump[i] <= 10000

At first glance, I saw this question as a simple dp, doing it, doing it for more than an hour, and then wondering whether it is related to the monotonous stack.

It was almost time for the last time, and I submitted it with an O (n ^ 2) code. It actually passed. I estimate that there are two possibilities. One is that my code is actually not O (n ^ 2), but I thought It ’s O (n ^ 2), because I did n’t think it was particularly clear in the end.

Another possibility is O (n ^ 2) can be passed, but seeing 1 <= jump.length <= 10^6 the feeling is not likely.

My general idea is that pressing first can only move to the right. Find the number of moves for each location.

int[] dp = new int[jump.length];
for(int i = jump.length - 1; i >= 0; i--){
    if (i+jump[i] >= jump.length){
        dp[i] = 1;
    }
    else {
        dp[i] = dp[i+jump[i]] + 1;
    }
}

Then use a min array record, min [i] represents the minimum value from subscript 0 to subscript i (including i).

int[] min = new int[jump.length];
min[0] = dp[0];
for (int i = 1; i < jump.length; i++) {
    min[i] = Math.min(min[i-1],dp[i]);
}

Finally, it is updated from back to front, and the final minimum value is obtained through judgment.

This while statement was added last, because I always thought it would time out. . . . .

for (int i = jump.length - 1; i >= 0; i--) {
    if (i+jump[i] >= jump.length){
        dp[i] = 1;
    }
    else {
        dp[i] = Math.min(Math.min(dp[i],min[i] + 1),dp[i+jump[i]] + 1);
        int k = i + 1;
        while (k < jump.length && dp[k] > dp[i]){
            dp[k] = dp[i] + 1;
            k++;
        }
    }
}

Because in the process of doing, it is found that the minimum number of movements that can jump out of the current position is determined in the first calculation from the back to the front. The reason is:

 
dp[i] = Math.min(Math.min(dp[i],min[i] + 1),dp[i+jump[i]] + 1);
 

In this code, min [i] is not true min [i], assuming that the value obtained by min [i] is the value of subscript x, if there are j <i and dp [j] <min [i], then the previously obtained dp [i] is wrong.

Here is an example.

int[] jump = new int[]{2, 6, 1, 1, 1, 1, 4, 1, 1, 1};

When the input array is {2, 6, 1, 1, 1, 1, 4, 1, 1, 1}, the correct move should be, from the subscript 0-> 2-> 1-> 7-> 6-> End. There are 5 jumps in total.

And if I follow my first thoughts.

The dp array found is:

6 3 5 4 3 2 1 2 2 1

You can see that the answer is wrong. The root cause is that when dp [2] is updated, dp [1] has not been updated at this time, so the updated value of dp [2] is not the final value, so it is an error. of. Then dp [0] is naturally wrong.

In order to solve this problem, while judgment is added, after the current dp [i] becomes smaller, it is necessary to search backward and update the dp [i] obtained before.

Maybe this method looks like a two-layer loop but not O (n ^ 2), because what may be performed in while is a two-pointer operation, which is to assume that j = length-1 is the rightmost boundary at the beginning. Each time a while is searched and updated backward from i to j, j is moved to the position of i after the update, and the next while loop will only traverse to the previous position. This actually only traverses once. After the competition, I tried to prove this point. If I can prove this point, it can show that the method is O (n) time complexity, but it is still too much. Wait until Leetcode comes up with a solution before learning.

 

Complete code:

public int minJump(int[] jump) {
    int[] dp = new int[jump.length];
    for(int i = jump.length - 1; i >= 0; i--){
        if (i+jump[i] >= jump.length){
            dp[i] = 1;
        }
        else {
            dp[i] = dp[i+jump[i]] + 1;
        }
    }
    int[] min = new int[jump.length];
    min[0] = dp[0];
    for (int i = 1; i < jump.length; i++) {
        min[i] = Math.min(min[i-1],dp[i]);
    }
    for (int i = jump.length - 1; i >= 0; i--) {
        if (i+jump[i] >= jump.length){
            dp[i] = 1;
        }
        else {
            dp[i] = Math.min(Math.min(dp[i],min[i] + 1),dp[i+jump[i]] + 1);
            int k = i + 1;
            while (k < jump.length && dp[k] > dp[i]){
                dp[k] = dp[i] + 1;
                k++;
            }
        }
    }if (jump[0] >= jump.length){
        return 1;
    }
    else {
        return dp[0];
    }
}

5. Binary tree task scheduling

In the end, there was no time. After reading it, I had no idea.

https://leetcode-cn.com/contest/season/2020-spring/problems/er-cha-shu-ren-wu-diao-du/

 

Guess you like

Origin www.cnblogs.com/liusandao/p/12730146.html