marscode第二场算法比赛(ABCD)

134 阅读2分钟

A.财富评估项目

思路:计算每行的和,求个最大值。

import java.util.Arrays;

public class A {

    public static int solution(int[][] financials) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here
        int ans = 0;
        for (int[] f : financials) {
            ans = Math.max(ans, Arrays.stream(f).sum());
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(solution(new int[][]{new int[]{2, 4, 6}, new int[]{4, 3, 2}}) == 12);
        System.out.println(solution(new int[][]{new int[]{3, 9}, new int[]{8, 2}, new int[]{5, 6}}) == 12);
        System.out.println(solution(new int[][]{new int[]{5, 5, 9, 3}, new int[]{6, 2, 3, 1}, new int[]{3, 4, 10, 2}}) == 22);
    }
}

B.礼物价值

思路:数据范围比较小,dfs记忆化搜索跑多重背包。

import java.util.Arrays;

public class B {

    static int[][] dp;
    public static int solution(int[][] giftPackages, int k) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here
        int n = giftPackages.length;
        dp = new int[n][k+1];
        for (int[] row : dp) {
            Arrays.fill(row, -1);
        }
        return dfs(0, 0, giftPackages, k);
    }

    static int dfs(int i, int v, int[][] g, int k) {
        if (i >= g.length) return v <= k ? 0: Integer.MIN_VALUE;
        if (v > k) return Integer.MIN_VALUE;
        if (dp[i][v] != -1) return dp[i][v];
        int a = dfs(i + 1, v, g, k);
        int b = 0;
        for (int j = 1; j <= g[i][0]; j++) {
            b = Math.max(b, dfs(i + 1, v + j, g, k) + j * g[i][1]);
        }
        return dp[i][v] = Math.max(a, b);
    }

    public static void main(String[] args) {
        System.out.println(solution(new int[][]{new int[]{2, 5}, new int[]{1, 3}, new int[]{3, 2}}, 3) == 13);
        System.out.println(solution(new int[][]{new int[]{3, 9}, new int[]{2, 4}, new int[]{4, 8}}, 6) == 51);
        System.out.println(solution(new int[][]{new int[]{3, 1}, new int[]{2, 6}, new int[]{4, 5}}, 5) == 27);
        System.out.println(solution(new int[][]{new int[]{1, 7}, new int[]{3, 8}, new int[]{2, 10}}, 4) == 36);
    }


}

C.最优远足问题

思路:二分答案 + BFS。

import java.util.ArrayDeque;

public class C {

    static int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    static boolean check(int[][] h, int mid) {
        int m = h.length;
        int n = h[0].length;
        var q = new ArrayDeque<int[]>();
        q.offer(new int[]{0, 0});
        var vis = new boolean[m][n];
        vis[0][0] = true;
        while (!q.isEmpty()) {
            int[] p = q.poll();
            int x = p[0], y = p[1];
            if (x == m - 1 && y == n - 1) return true;
            for (int[] dir : dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];
                if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
                if (!vis[nx][ny] && Math.abs(h[x][y] - h[nx][ny]) <= mid) {
                   q.offer(new int[]{nx, ny});
                   vis[nx][ny] = true;
                }
            }
        }
        return false;
    }

    public static int solution(int[][] heights) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here

        int l = 1;
        int r = 101;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (check(heights, mid)) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        System.out.println(solution(new int[][]{new int[]{5, 1, 2}, new int[]{4, 6, 2}, new int[]{1, 3, 8}}) == 5);
        System.out.println(solution(new int[][]{new int[]{3, 9, 8, 7, 4, 2}, new int[]{5, 6, 5, 3, 2, 1}, new int[]{7, 4, 6, 7, 5, 9}}) == 4);
        System.out.println(solution(new int[][]{new int[]{2, 5}, new int[]{1, 4}}) == 3);
    }

}

D.画作

思路:dp计算方案数,f[i][j] += f[i-1][j] * (i-1)。

public class D {

    public static int solution(int n, int k) {
        // PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
        // write code here
        final int MOD = 1000000007;
        long[][] dp = new long[n+1][k+1];
        dp[0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= k; j++) {
                dp[i][j] = dp[i-1][j-1];
                dp[i][j] += dp[i-1][j] * (i-1);
                dp[i][j] %= MOD;
            }
        }
        return (int) dp[n][k];
    }

    public static void main(String[] args) {
        System.out.println(solution(4, 2) == 11);
        System.out.println(solution(6, 3) == 225);
        System.out.println(solution(7, 4) == 735);
        System.out.println(solution(9, 5) == 22449);
    }
}