-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1425-Constrained_Subsequence_Sum.cpp
More file actions
104 lines (93 loc) · 2.79 KB
/
1425-Constrained_Subsequence_Sum.cpp
File metadata and controls
104 lines (93 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
* 1425-Constrained_Subsequence_Sum.cpp
* Billy.Ljm
* 21 October 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/constrained-subsequence-sum/
*
* Given an integer array nums and an integer k, return the maximum sum of a
* non-empty subsequence of that array such that for every two consecutive
* integers in the subsequence, nums[i] and nums[j], where i < j, the condition
* j - i <= k is satisfied.
*
* A subsequence of an array is obtained by deleting some number of elements
* (can be zero) from the array, leaving the remaining elements in their
* original order.
*
* ===========
* My Approach
* ===========
* We can essentially remove k adjacent elements from the array to maximise the
* sum. Thus, dynamic programming can be used to consider all removals and find
* the optimal subsequence. To speed things up, we can use a priority queue to
* consider the removals, adding and removing one element into the queue on each
* iteration instead of comparing k elements on every iteration.
*
* This has a time complexity of O(n * log(k)), and space complexity of O(n),
* where n is the size of the array and k is the integer given as an argument.
******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
int constrainedSubsetSum(vector<int>& nums, int k) {
// dp[i] = max sum when including nums[i]
vector<int> dp(nums.size(), 0);
// last k elements of dp as {dp[i], i}
priority_queue<pair<int, int>> pq;
pq.push({ 0, -1 });
// fill up dp table
for (int i = 0; i < nums.size(); i++) {
while (pq.top().second < i - k) pq.pop();
dp[i] = max(0, pq.top().first) + nums[i];
pq.push({ dp[i], i });
}
// find answer
return *max_element(dp.begin(), dp.end());
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
vector<int> nums;
int k;
// test case 1
nums = { 10,2,-10,5,20 };
k = 2;
std::cout << "constrainedSubsetSum(" << nums << "," << k << ") = ";
std::cout << sol.constrainedSubsetSum(nums, k) << std::endl;
// test case 2
nums = { -1,-2,-3 };
k = 1;
std::cout << "constrainedSubsetSum(" << nums << "," << k << ") = ";
std::cout << sol.constrainedSubsetSum(nums, k) << std::endl;
// test case 3
nums = { 10,-2,-10,-5,20 };
k = 2;
std::cout << "constrainedSubsetSum(" << nums << "," << k << ") = ";
std::cout << sol.constrainedSubsetSum(nums, k) << std::endl;
return 0;
}