thảo luận Leetcode + Codeforces, Competitive programming contest. Đường tới Guardian + Candidate Master.

  • Người tạo chủ đề Người tạo chủ đề freedom.9
  • Ngày bắt đầu Ngày bắt đầu
trình cùi k làm contest nhưng được cái chăm copy paste để solve daily problems, thế lày là chuẩn bị có áo rồi à các tiền bối
1751288594963.png
 
cho mình tham khảo công thức DP bài đấy với. mình bị ngu DP
 
cho mình tham khảo công thức DP bài đấy với. mình bị ngu DP
À mình ko làm bottom-up, làm top-down
Java:
class Solution {
    public int minXor(int[] nums, int k) {
        int[][] memo = new int[nums.length + 1][k + 1];
        for (int i = 0; i < nums.length; i++) {
            Arrays.fill(memo[i], -1);
        }
        return tryToPartition(0, k, nums, memo);
    }

    public int tryToPartition(int index, int remainPartition, int[] nums, int[][] memo) {
        if (index == nums.length && remainPartition == 0) {
            return 0;
        }

        if (index == nums.length || remainPartition == 0) {
            return Integer.MAX_VALUE;
        }

        if (memo[index][remainPartition] != -1) {
            return memo[index][remainPartition];
        }

        int curXor = 0;
        int maximumXor = curXor;
        int answer = Integer.MAX_VALUE;
        for (int i = index; i <= nums.length - remainPartition; i++) {
            curXor ^= nums[i];
            maximumXor = Math.max(
                curXor,
                tryToPartition(i + 1, remainPartition - 1, nums, memo)
            );

            answer = Math.min(answer, maximumXor);
        }

        memo[index][remainPartition] = answer;
        return answer;
    }


}
 
À mình ko làm bottom-up, làm top-down
Java:
class Solution {
    public int minXor(int[] nums, int k) {
        int[][] memo = new int[nums.length + 1][k + 1];
        for (int i = 0; i < nums.length; i++) {
            Arrays.fill(memo[i], -1);
        }
        return tryToPartition(0, k, nums, memo);
    }

    public int tryToPartition(int index, int remainPartition, int[] nums, int[][] memo) {
        if (index == nums.length && remainPartition == 0) {
            return 0;
        }

        if (index == nums.length || remainPartition == 0) {
            return Integer.MAX_VALUE;
        }

        if (memo[index][remainPartition] != -1) {
            return memo[index][remainPartition];
        }

        int curXor = 0;
        int maximumXor = curXor;
        int answer = Integer.MAX_VALUE;
        for (int i = index; i <= nums.length - remainPartition; i++) {
            curXor ^= nums[i];
            maximumXor = Math.max(
                curXor,
                tryToPartition(i + 1, remainPartition - 1, nums, memo)
            );

            answer = Math.min(answer, maximumXor);
        }

        memo[index][remainPartition] = answer;
        return answer;
    }


}
Bác rating bao nhiêu thế? trông DP sướng z
 

Thống kê chủ đề

Ngày tạo
freedom.9,
Người trả lời cuối
deple20k,
Trả lời
1.686
Lượt xem
107.092
Quay lại
Lên đầu trang