第 199 场力扣周赛题解

5472. 重新排列字符串

思路:额外开一个字符数组存储以及完成题目要求的移动即可。

class Solution {
    public String restoreString(String s, int[] indices) {

        int n = s.length();
        char[] c = s.toCharArray();

        for (int i = 0; i < n; i++)
            c[indices[i]] = s.charAt(i);

        return new String(c);

    }
}

5473. 灯泡开关 IV

思路:一定是从左到右翻转的,若当前灯是开着的,判断它之前翻转了几次,若是偶数次,则当前仍需要执行翻转操作,对于关着的灯也是一样的。

class Solution {
    public int minFlips(String target) {

        int num = 0;
        int len = target.length();
        for (int i = 0; i < len; i++) {
            if (target.charAt(i) == '1' && num % 2 == 0)
                num++;
            if (target.charAt(i) == '0' && num % 2 != 0)
                num++;
        }

        return num;
        
    }
}

5474. 好叶子节点对的数量

思路:最多只有1024个节点,对于每个节点,我们暴力其左右子树中能够凑出多少个好节点对即可。

class Solution {

    private int ans;
    private List list1;
    private List list2;

    public int countPairs(TreeNode root, int distance) {

        list1=new ArrayList<>();
        list2=new ArrayList<>();

        dfs(root, distance);

        return ans;

    }

    private void dfs(TreeNode root, int dis) {

        if (root == null) return;

        dfs(root.left,dis);
        dfs(root.right,dis);

        list1.clear();
        list2.clear();
        list1 = dfs_node(root.left, 1);
        list2 = dfs_node(root.right, 1);

        Collections.sort(list1);
        Collections.sort(list2);

        int size1 = list1.size();
        int size2 = list2.size();

        for (int i = 0; i < size1; i++) {
            int l = 0, r = size2 - 1, p = -1;
            while (l <= r) {
                int mid = (l + r) / 2;
                if (list2.get(mid) + list1.get(i) <= dis) {
                    p = mid;
                    l = mid + 1;
                } else
                    r = mid - 1;
            }

            ans += p + 1;
        }
    }

    private List dfs_node(TreeNode root, int depth) {

        if (root == null)
            return new ArrayList<>();

        List list = new ArrayList<>();

        if (root.left == null && root.right == null)
            list.add(depth);
        else {
            list.addAll(dfs_node(root.left, depth + 1));
            list.addAll(dfs_node(root.right, depth + 1));
        }

        return list;

    }

}

5462. 压缩字符串 II

思路:动态规划,我们定义dp[i][j]表示前i个字符已经执行了j次删除操作的最小行程长度编码长度。

class Solution {
    public int getLengthOfOptimalCompression(String s, int k) {

        int n = s.length();
        int[][] dp = new int[n + 1][k + 2];

        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= k; j++)
                dp[i][j] = 1000000;

        dp[0][0] = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 0; j <= k; j++) {
                dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i - 1][j]);
                int cnt = 0, del = 0;
                for (int h = i; h <= n; h++) {
                    if (s.charAt(h - 1) == s.charAt(i - 1)) cnt++;
                    else del++;
                    if (j + del <= k)
                        dp[h][j + del] = Math.min(dp[h][j + del], dp[i - 1][j] + (cnt >= 100 ? 4 : cnt >= 10 ? 3 : cnt >= 2 ? 2 : 1));
                }
            }

        return dp[n][k];

    }
}

 

你可能感兴趣的:(第 199 场力扣周赛题解)