[2021 Spring Recruitment] Ali Written Exam Real Questions

Young idler, an old beggar!

but

2021.3.6

1411. Number of schemes for coloring the N x 3 grid map
815. Bus route

1

You have an nx 3 grid. You need to color each grid with one of three colors: red, yellow, and green, and make sure that adjacent grids have different colors (that is, grids with the same horizontal or vertical sides) The colors are different). Give you the number of rows in the grid graph n.
Please return the number of the coloring schemes for the grid. Since the answer may be very large, please return the result of the remainder of the answer to 10^9 + 7.
Insert picture description here

Find the law, dynamic planning

class Solution {
    
    
    static final int MOD = 1000000007;

    public int numOfWays(int n) {
    
    
        long fi0 = 6, fi1 = 6;
        for (int i = 2; i <= n; ++i) {
    
    
            long newFi0 = (2 * fi0 + 2 * fi1) % MOD;
            long newFi1 = (2 * fi0 + 3 * fi1) % MOD;
            fi0 = newFi0;
            fi1 = newFi1;
        }
        return (int) ((fi0 + fi1) % MOD);
    }
}

2

Give you an array of routes, representing a series of bus routes, where each routes[i] represents a bus route, and the i-th bus will circulate on it.

For example, the route routes[0] = [1, 5, 7] means that the 0th bus will always follow the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 ->… such a station Route driving.
Now starting from the source station (not on the bus initially), heading to the target station. Only buses can be used during this period.

Find the least number of buses to ride. If it is impossible to reach the terminal station, return -1.

BFS: Breadth first traversal,

import java.util.Arrays;

class Solution {
    
    
    public int numBusesToDestination(int[][] routes, int source, int target) {
    
    
		int maxStationNum = 0;//最大车站编号
        //统计最大车站编号
		for(int[] route:routes){
    
    
			for(int num: route){
    
    
				maxStationNum = Math.max(maxStationNum, num);
			}
		}
        //终点车站比最大的大,即不存在,直接返回
        if(target > maxStationNum){
    
    
            return -1;
        }
        //用来存储每个车站距离source站换车次数
		int[] dp = new int[maxStationNum+1];
		Arrays.fill(dp, maxStationNum+1);//初始设置为不可达,maxStationNum+1表示不可达
        //设置起点可达需要0次
		dp[source] = 0;

        //用来标记车数组中是否发生修改
		boolean updateFlag = true;
        //广度优先遍历
		while(updateFlag){
    
    
            //每一轮遍历车数组
			for(int[] route:routes){
    
    
                //统计当前车次中可达终点的最小次数
				int min = maxStationNum+1;
				for(int num: route){
    
    
					if(dp[num] < min){
    
    
						min = dp[num];
					}
				}
                //根据最小次数,更新同一个车次中,其他站的最小次数
				for(int num: route){
    
    
					if(dp[num] > min+1){
    
    
						dp[num] = min+1;
						updateFlag = false;//标志数组发生修改
					}
				}
			}
            //如果发生修改,即所有数组中站点修改完成,为false
            //如果没有发生修改,即所有数组中站点修改完成,为true
			updateFlag = !updateFlag;//如果全部修改完成,终止while循环
		}
        //返回到达target的最小次数
		if(dp[target] < maxStationNum+1){
    
    
			return dp[target];
		}
		return -1;
	}	
}

2021.3.8

1539. The k missing positive integer
879. Profit plan

1

Give you an array of positive integers arr and an integer k in strict ascending order.

Please find the kth missing positive integer in this array.

Analog counting process

class Solution {
    
    
    public int findKthPositive(int[] arr, int k) {
    
    
        int count = 0;
        int miss = 0;
        for(int i = 0; i < arr.length; i++){
    
    
            count++;
            while(count < arr[i]){
    
    
                miss++;
                if(miss == k){
    
    
                    return count;
                }
                count++;
            }
        }
        return arr[arr.length-1]+k-miss;
    }
}

2

There are n employees in the group, and they can complete a variety of jobs to create profits.

The i-th job will generate profit[i], which requires the participation of group[i] members. If a member is involved in one of these tasks, they cannot participate in the other.

Any subset of work that generates at least minProfit profit is called a profit plan. And the total number of working members is at most n.

How many plans are there to choose from? Because the answer is very large, the result modulo 10^9 + 7 is returned.

dp, the knapsack problem is similar

class Solution {
    
    
    public int profitableSchemes(int G, int P, int[] group, int[] profit) {
    
    
        int mod = 1000_000_007;
        int[][] dp = new int[G + 1][P + 1];
        //dp[i][j]表示,分配j个工人收益i的收益计划数量
        //dp数组从最后一行向前,
        
        dp[0][0] = 1;
        int taskNum = group.length;
        //动态规划
        for (int i = 0; i < taskNum; i++) {
    
    //首先是遍历每一个任务
            int workerNum = group[i];//当前任务的工人数量
            int workProfit = profit[i];//当前任务带来的收益
            for (int j = G; j >= workerNum; j--) {
    
    
                for (int k = P; k >= 0; k--) {
    
    
                    int indexK = 0;
                    if(workProfit < k){
    
    
                        indexK = k - workProfit;
                    }
                    dp[j][k] = (dp[j][k] + dp[j-workerNum][indexK])%mod;
                }
            }
        }
		
        int sum = 0;
        for (int i = 0; i < G+1; i++){
    
    
            sum = (sum + dp[i][P])%mod;
        }
        return sum;
    }
}

2021.3.10

The first question: Leetcode 1388. 3n pieces of pizza were not found

1

Title description:

Simulation question, can only go in one direction, hit the wall to stop, four directions
are not used to reading the input, nextInt needs to be read by nextLine\n, -_-
@ indicates the starting point, # indicates the wall, enter the southeast, northwest direction
input:
3 4 4
@…
.#
……
EAST
SOUTH
WEST
NORTH
output:
1 3

2

Here is a pizza. It consists of 3n pieces of different sizes. Now you and your friends need to divide the pizza according to the following rules:

You pick any piece of pizza.
Alice will pick the next pizza of your choice counterclockwise.
Bob will pick the next pizza of your choice in a clockwise direction.
Repeat the above process until there is no pizza left.
The size of each slice of pizza is represented by the circular array slices in a clockwise direction.

Please return the maximum sum of pizza sizes you can get.
Insert picture description here

Choose n non-adjacent numbers in the circular 3n sequence to maximize the sum of the values

class Solution {
    
    
    public int maxSizeSlices(int[] slices) {
    
    
        int[] slices1 = new int[slices.length - 1];
        System.arraycopy(slices, 1, slices1, 0, slices.length - 1);
        int[] slices2 = new int[slices.length - 1];
        System.arraycopy(slices, 0, slices2, 0, slices.length - 1);
        int ans1 = calculate(slices1);
        int ans2 = calculate(slices2);
        return Math.max(ans1, ans2);
    }

    public int calculate(int[] slices) {
    
    
        int n = slices.length;//序列长度是原本序列长度-1,可选择披萨数量少1
        int choose = (n + 1) / 3;//因为是去掉了头尾计算的,所以可选择披萨要加1
        int[][] dp = new int[n + 1][choose + 1];
        for (int i = 1; i <= n; ++i) {
    
    
            for (int j = 1; j <= choose; ++j) {
    
    
                dp[i][j] = Math.max(dp[i - 1][j], (i - 2 >= 0 ? dp[i - 2][j - 1] : 0) + slices[i - 1]);
            }
        }
        return dp[n][choose];
    }
}

notes

  1. Regarding the ring: Calculate the values ​​of [0, last-1] and [1, last] respectively, and find the maximum
  2. Regarding the difference with Dajiajieshe 2 : In a sequence with a total of n, Dajia 2 can steal n/2 rooms; Pizza can take n/3 slices.

2021.3.12

1

The BFS of
a directed graph tells a directed graph and asks the shortest distance from a to b

Solution: directed graph Dijkstra

2

On the n*n chessboard, there is a "car", move the "car" to the diagonal position, and it cannot be eaten by other "cars" during the movement. Ask for the minimum number of moves
C. Peaceful Rooks

Guess you like

Origin blog.csdn.net/qq_39457586/article/details/114489390