[Context] 2020 LC National Spring Season Programming Contest

C_01 take coins

There are n stacks of coins on the table, and the number of each stack is stored in the array coins. 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.

输入:[4,2,1]
输出:4
解释:第一堆力扣币最少需要拿 2 次,第二堆最少需要拿 1 次,第三堆最少需要拿 1 次,总共 4 次即可拿完。

示例 2:
输入:[2,3,10]
输出:8

限制:
1 <= n <= 4
1 <= coins[i] <= 10

Method 1: Simulation

public int minCount(int[] coins) {
    int sum = 0;
    for (int coin : coins) {
        if (coin % 2 == 0) {
            sum += coin/2;
        } else {
            sum += coin/2 + coin%2;
        }
    }
    return sum;
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( 1 ) O (1)

B_02 Transfer information

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

  • There are n players, all the player numbers are 0 ~ n-1, and the number of the child A is 0
  • 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).
  • 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, and a two-dimensional array relation consisting of [player number, corresponding to the player number that can be passed]. The number of solutions returned from little A (number 0) to the partner with number n-1 through k rounds; if it cannot be reached, it returns 0.

输入:n = 5, relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]], k = 3
输出:3
解释:信息从小 A 编号 0 处开始,经 3 轮传递,到达编号 4。共有 3 种方案,
分别是 0->2->0->4, 0->2->1->4, 0->2->3->4。

Method 1: dfs

Construction, dfs ...

List<List<Integer>> g;
int tot, N, K;
public int numWays(int n, int[][] edges, int k) {
    N = n;
    K = k;
    g = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        g.add(new ArrayList<>());
    }
    for (int[] e : edges) {
        g.get(e[0]).add(e[1]);
    }
    dfs(k, 0);
    return tot;
}
void dfs(int id, int k) {
    if (k == 0) {
        if (id == N-1)
            tot++;
        return;
    }
    for (int nei : g.get(id)) {
        dfs(nei, k-1);
    }
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( n ) O (n)

Method 2: bfs

Positive ring: There will be a ring in this question, which will keep walking in the ring, resulting in a timeout, so when k <0, we discard those points.

List<List<Integer>> g;
int tot, N;
public int numWays(int n, int[][] edges, int k) {
    N = n;
    g = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        g.add(new ArrayList<>());
    }
    for (int[] e : edges) {
        g.get(e[0]).add(e[1]);
    }
    bfs(k);
    return tot;
}
private void bfs(int k) {
    Queue<Integer> q = new LinkedList<>();
    q.add(0);
    while (!q.isEmpty()) {
        int s = q.size();
        while (s-- > 0) {
            int id = q.poll();
            if (k == 0 && id == N-1)
                tot++;
            if (k < 0)
                continue;
            for (int nei : g.get(id)) {
                q.add(nei);
            }
        }
        k--;
    }
}

Complexity analysis

  • time complexity: O ( n ) O (n)
  • Space complexity: O ( n ) O (n)

B_03

method one:


Complexity analysis

  • time complexity: O ( ) O()
  • Space complexity: O ( ) O()

A_04

method one:


Complexity analysis

  • time complexity: O ( ) O()
  • Space complexity: O ( ) O()

Published 714 original articles · praised 199 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/105601396