Day 16:3040. 相同分数的最大操作数目II

124 阅读2分钟

Leetcode 相同分数的最大操作数目II

给你一个整数数组 nums ,如果 nums 至少 包含 2 个元素,你可以执行以下操作中的 任意 一个:

  • 选择 nums 中最前面两个元素并且删除它们。
  • 选择 nums 中最后两个元素并且删除它们。
  • 选择 nums 中第一个和最后一个元素并且删除它们。

一次操作的 分数 是被删除元素的和。

在确保** 所有操作分数相同** 的前提下,请你求出 最多 能进行多少次操作。

请你返回按照上述要求 最多 可以进行的操作次数。

image.png

可以理解为一颗三叉树,其中一棵子树选择数组前两个元素,一棵选择第一个和最后的一个元素,最后一棵子树选择最后两个元素。

完整代码

class Solution {
    public int maxOperations(int[] nums) {
        int n = nums.length;
        if (n == 2) return 1;
        int res = maxOperation2(nums, nums[0] + nums[1], 2, n - 1) + 1;
        if (res == nums.length / 2) return res;
        res = Math.max(res, maxOperation2(nums, nums[n - 1] + nums[n - 2], 0, n - 3) + 1);
        if (res == nums.length / 2) return res;
        res = Math.max(res, maxOperation2(nums, nums[0] + nums[n - 1], 1, n - 2) + 1);
        return res;
    }

    public int maxOperation2(int[] nums, int sum, int start, int end) {
        int res = 0;
        if (end - start == 1 && nums[start] + nums[end] == sum) res = 1;
        else if (end - start > 1) {
            // 前两个
            if ((nums[start] + nums[start + 1]) == sum) {
                res = Math.max(res, maxOperation2(nums, sum, start + 2, end) + 1);
            }  
            if (res == nums.length / 2) return res;
            // 最后两个
            if ((nums[end] + nums[end - 1]) == sum) {
                res = Math.max(res, maxOperation2(nums, sum, start, end - 2) + 1);
            }
            if (res == nums.length / 2) return res;
            // 第一个和最后一个
            if ((nums[start] + nums[end]) == sum) {
                res = Math.max(res, maxOperation2(nums, sum, start + 1, end - 1) + 1);
            }
        }
        return res;
    }
}


以上 maxOperation2()函数的调用许多传入了相同的参数,因此浪费了大量时间,最后会超出时间限制。
建一个二维数组保存范围结果。

class Solution {
    int[] nums;
    int[][] memo;

    public int maxOperations(int[] nums) {
        int n = nums.length;
        this.nums = nums;
        this.memo = new int[n][n];
        int res = 0;
        res = Math.max(res, helper(0, n - 1, nums[0] + nums[n - 1]));
        res = Math.max(res, helper(0, n - 1, nums[0] + nums[1]));
        res = Math.max(res, helper(0, n - 1, nums[n - 2] + nums[n - 1]));
        return res;
    }

    public int helper(int i, int j, int target) {
        for (int k = 0; k < nums.length; k++) {
            Arrays.fill(memo[k], -1);
        }
        return dfs(i, j, target);
    }

    public int dfs(int i, int j, int target) {
        if (i >= j) {
            return 0;
        }
        if (memo[i][j] != -1) {
            return memo[i][j];
        }
        int ans = 0;
        if (nums[i] + nums[i + 1] == target) {
            ans = Math.max(ans, dfs(i + 2, j, target) + 1);
        }
        if (nums[j - 1] + nums[j] == target) {
            ans = Math.max(ans, dfs(i, j - 2, target) + 1);
        }
        if (nums[i] + nums[j] == target) {
            ans = Math.max(ans, dfs(i + 1, j - 1, target) + 1);
        }
        memo[i][j] = ans;
        return ans;
    }
}