leetcode 第141场周赛

https://leetcode.com/contest/weekly-contest-141

A:Duplicate Zeros

模拟即可

/**
 * Created by dezhonger on 2019/6/17
 */
public class Leetcode1089 {
    public void duplicateZeros(int[] arr) {
        int[] a = new int[arr.length];
        int index= 0 ;
        for (int i = 0; index < arr.length && i < arr.length; i++) {
            if (arr[i] == 0) {
                a[index++] = 0;
                if (index == arr.length) break;
                a[index++] = 0;
            } else {
                a[index++] = arr[i];
            }
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] = a[i];
        }
    }
}

B:Largest Values From Labels

按照value排序,然后贪心

import java.util.Arrays;

/**
 * Created by dezhonger on 2019/6/17
 */
public class Leetcode1090 {
    public int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {
        Node[] nodes = new Node[values.length];
        for (int i = 0; i < nodes.length; i++) nodes[i] = new Node(values[i], labels[i]);
        Arrays.sort(nodes);
        int[] cnt = new int[20010];
        int c = 0;
        int s = 0;
        for (int i = 0; c < num_wanted && i < nodes.length; i++) {
            if (cnt[nodes[i].label] < use_limit) {
                s += nodes[i].value;
                cnt[nodes[i].label]++;
                c++;
            }
        }
        return s;
    }

    class Node implements Comparable {
        Node(int value, int label) {
            this.value = value;
            this.label = label;
        }

        @Override
        public int compareTo(Node o) {
            return Integer.compare(o.value, value);
        }

        int value;
        int label;
    }
}

C:Shortest Path in Binary Matrix

裸的bfs

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * Created by dezhonger on 2019/6/17
 */
public class Leetcode1091 {
    public int shortestPathBinaryMatrix(int[][] grid) {
        int N = grid.length;
        if (grid[0][0] == 1 || grid[N - 1][N - 1] == 1) return -1;
        int[] dx = new int[]{-1, -1, -1, 0, 0, 1, 1, 1};
        int[] dy = new int[]{-1, 0, 1, -1, 1, -1, 0, 1};
        boolean[] v = new boolean[10010];
        Queue queue = new ArrayDeque<>();
        queue.add(new Node(0, 0, 1));
        v[0] = true;
        while (!queue.isEmpty()) {
            Node tmp = queue.poll();
            if (tmp.x == N - 1 && tmp.y == N - 1) return tmp.d;
            for (int i = 0; i < 8; i++) {
                int ddx = tmp.x + dx[i];
                int ddy = tmp.y + dy[i];
                if (ddx >= 0 && ddx < N && ddy >= 0 && ddy < N && !v[ddx * N + ddy] && grid[ddx][ddy] == 0) {
                    queue.add(new Node(ddx, ddy, tmp.d + 1));
                    v[ddx * N + ddy] = true;
                }
            }
        }
        return -1;
    }


    class Node {
        Node(int x, int y, int d) {
            this.x = x;
            this.y = y;
            this.d = d;
        }

        int x, y, d;
    }
}

D:Shortest Common Supersequence

本质就是求带输出结果的LCS

/**
 * Created by dezhonger on 2019/6/17
 */
public class Leetcode1092 {


    int[][] flag;
    int len1;
    int len2;
    char[] c1;
    char[] c2;
    int[][] dp;
    StringBuilder sb = new StringBuilder();


    private int cal(int[][] dp, int i, int j) {
        if (i >= 0 && j >= 0) return dp[i][j];
        else return 0;
    }

    private void print(int i, int j) {
        if (i < 0 || j < 0) {
            for (int k = 0; k <= i; k++) sb.append(c1[k]);
            for (int k = 0; k <= j; k++) sb.append(c2[k]);
            return;
        }
        if (flag[i][j] == 0) {
            print(i - 1, j - 1);
            sb.append(c1[i]);
        } else if (flag[i][j] == 1) {
            print(i - 1, j);
            sb.append(c1[i]);
        } else {
            print(i, j - 1);
            sb.append(c2[j]);
        }
    }

    public String shortestCommonSupersequence(String str1, String str2) {

        len1 = str1.length();
        len2 = str2.length();
        c1 = str1.toCharArray();
        c2 = str2.toCharArray();
        dp = new int[len1][len2];
        flag = new int[len1][len2];


        for (int i = 0; i < len1; i++) {
            for (int j = 0; j < len2; j++) {
                if (c1[i] == c2[j]) {
                    dp[i][j] = cal(dp, i - 1, j - 1) + 1;
                    flag[i][j] = 0;
                } else {
                    if (cal(dp, i - 1, j) >= cal(dp, i, j - 1)) {
                        dp[i][j] = cal(dp, i - 1, j);
                        flag[i][j] = 1;
                    } else {
                        dp[i][j] = cal(dp, i, j - 1);
                        flag[i][j] = -1;
                    }
                }
            }
        }
        print(len1 - 1, len2 - 1);
        return sb.toString();
    }
}

 

你可能感兴趣的:(比赛,leetcode)