【每日一题】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;
    }
    
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值