LeetCode1425. Constrained Subsequence Sum (Medium) – DP + Heap Appraoch
Tags: 100dayschallengeleet, c, leetcode, leetcodesolution
Published: 2023-11-06 17:14:58

---
title: LeetCode1425. Constrained Subsequence Sum (Medium) – DP + Heap Appraoch
published: True
date: 2023-10-21 11:02:11 UTC
tags: 100DaysChallengeLeet,C,leetcode,Leetcodesolution
canonical_url: https://www.nileshblog.tech/leetcode1425-constrained-subsequence-sum/
---

# **[LeetCode1425: Constrained Subsequence Sum (Medium) – DP + Heap Approach](https://www.nileshblog.tech/leetcode1425-constrained-subsequence-sum/)**

If you're a fan of cracking coding challenges, then you've probably encountered the notorious "Constrained Subsequence Sum" problem on LeetCode. In this blog post, we'll dive deep into this medium-level LeetCode problem and explore a dynamic programming (DP) approach combined with a heap to tackle it. So, are you ready to unravel this intriguing challenge and enhance your coding skills? Let's get started!

## Introduction

The [LeetCode1425](https://www.nileshblog.tech/leetcode1425-constrained-subsequence-sum/) problem, also known as "[Constrained Subsequence Sum](https://www.nileshblog.tech/leetcode1425-constrained-subsequence-sum/)," presents a scenario where you need to find the maximum subsequence sum within a given array. The twist here is that you are limited by the constraint k, which restricts the maximum distance between the elements you can select in your subsequence.

## Prerequisites

Before we jump into the problem-solving, it's essential to have a basic understanding of dynamic programming and heaps (priority queues). If these concepts are new to you, don't worry – we'll explain them as we go along.

## The Main Concept

The main idea behind our approach is to use dynamic programming to keep track of the maximum sum subsequence ending at each element of the input array. To ensure we stay within the constraint k, we use a heap to efficiently retrieve the maximum sum subsequence within the specified range.

Let's break this down further:

1. Initialize an array `dp` to store the maximum subsequence sum ending at each element.

2. Create a max heap to maintain the maximum subsequence sum within the constraint k.

3. Iterate through the input array, updating `dp` and the max heap as follows:

    - For each element, update `dp[i]` with the maximum of `nums[i]` and the maximum subsequence sum within the range `[i - k, i - 1]`. This ensures that we maintain the maximum sum subsequence ending at the current element.

    - Add `dp[i]` to the max heap.

4. The maximum element in the max heap at the end will be our answer, representing the maximum subsequence sum.

## Example

Let's illustrate this approach with an example:

Input: `nums = [10, 2, -10, 5, 20]`, `k = 2`

1. Initialize `dp` and the max heap.
2. Iterate through the array:

    - For `nums[0]`, `dp[0] = 10` since it's the first element.
    - Add `dp[0]` to the max heap.

3. Continue this process for the rest of the array.

4. At the end, the maximum element in the max heap will be our answer.

## Output

In our example, the maximum subsequence sum with the given constraint is 37, and you can find the full code and explanations in this [link](https://www.nileshblog.tech/leetcode1425-constrained-subsequence-sum/).

In conclusion, the LeetCode1425 "Constrained Subsequence Sum" problem challenges your ability to optimize your solution by employing dynamic programming and heaps effectively. By mastering this approach, you can solve a wide range of similar problems and improve your coding skills. Happy coding!