【每日一题】Leetcode - 1425. Constrained Subsequence Sum

Question

Leetcode - 1425. Constrained Subsequence Sum

Train of thought

  • dp[i] = j : j represents the maximum sum that last index is i, dp[0] = nums[0].
  • Review k elements, and find maximum.
class Solution {
    
    public int constrainedSubsetSum(int[] nums, int k) {
        int n = nums.length;

        //  dp[i] = j : j represents the maximum sum that last index is i
        int[] dp = new int[n];
        dp[0] = nums[0];

        //  loop to calculate the value of dp array
        int maxSum = dp[0];
        for (int i = 1; i < n; i++) {
            //  review k elements, and find maximum
            int maxAgo = Integer.MIN_VALUE, mE = Math.max(i - k, 0);
            for (int m = i - 1; m >= mE; m--) {
                if (dp[m] > maxAgo) {
                    maxAgo = dp[m];
                }
            }
            //  calculate dp[i]
            if (maxAgo < 0) {
                dp[i] = nums[i];
            }
            else {
                dp[i] = maxAgo + nums[i];
            }
            //  update maximum
            if (dp[i] > maxSum) {
                maxSum = dp[i];
            }
        }
        return maxSum;
    }
    
}

在这里插入图片描述

Optimize

We can optimize the cost time of find maximum element by remember first maximum and second maximum, special pay attention to step by step looping the array to calculate it that mark sure we can first time to find the second maximum.

class Solution {
    
    public int constrainedSubsetSum(int[] nums, int k) {
        int n = nums.length;

        //  dp[i] = j : j represents the maximum sum that last index is i
        int[] dp = new int[n];
        dp[0] = nums[0];

        //  loop to find the value of dp array
        int maxSum = dp[0], maxAgo = dp[0], maxAgoIndex = 0, secondMaxAgo = Integer.MIN_VALUE, secondMaxAgoIndex = -1;
        for (int i = 1; i < n; i++) {
            //  change maximum to second maximum
            if (i - maxAgoIndex == k + 1) {
                maxAgo = secondMaxAgo;
                maxAgoIndex = secondMaxAgoIndex;
                //nums:  100, -10, -10, -10, -2, -2, -10, -100, 15, -5, -10, 10,  2, -10,  5, 20
                //dp  :  100,  90,  90,  80, 88, 86,  78,  -14, 15, 10,   5, 20, 22,  12, 27, 47
                //  maybe has skip element 78:  greater 88, second greater 86
                if (secondMaxAgoIndex + 1 != i) {
                    secondMaxAgo = dp[secondMaxAgoIndex + 1];
                    secondMaxAgoIndex++;
                }
                else {
                    secondMaxAgoIndex = -1;
                }
            }
            //  calculate dp[i]
            if (maxAgo < 0) {
                dp[i] = nums[i];
            }
            else {
                dp[i] = maxAgo + nums[i];
            }
            //  update maximum
            if (dp[i] > maxSum) {
                maxSum = dp[i];
            }
            //  get greater ago
            if (dp[i] >= maxAgo) {
                maxAgo = dp[i];
                maxAgoIndex = i;
                secondMaxAgoIndex = -1;
                continue;
            }
            //  init second maximum
            if (secondMaxAgoIndex == -1) {
                secondMaxAgo = dp[i];
                secondMaxAgoIndex = i;
                continue;
            }
            //  update second maximum
            if (dp[i] >= secondMaxAgo) {
                secondMaxAgo = dp[i];
                secondMaxAgoIndex = i;
            }
        }
        return maxSum;
    }
    
}

【每日一题】Leetcode - 1425. Constrained Subsequence Sum_第1张图片

你可能感兴趣的:(每日一题,leetcode,算法,java,动态规划)