Problem solution · C++

Find the Maximum Sequence Value of Array

Find the Maximum Sequence Value of Array: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc LeetCode Solutions
Length
54 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Find the Maximum Sequence Value of Array, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 54 lines of C++ from the credited upstream file 3287.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 7 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind the Maximum Sequence Value of Array · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int maxValue(vector<int>& nums, int k) {    // left[i][j][x] := true if it's possible to get an OR value of x by    // selecting j numbers from nums[0..i]    const vector<vector<vector<bool>>> left = getPossibleORs(nums, k);    // right[i][j][x] := true if it's possible to get an OR value of x by    // selecting j numbers from nums[i..n - 1]    vector<vector<vector<bool>>> right =        getPossibleORs({nums.rbegin(), nums.rend()}, k);    ranges::reverse(right);     int ans = 0;     for (int i = k - 1; i + k < nums.size(); ++i)      for (int a = 0; a <= kMaxXor; ++a)        for (int b = 0; b <= kMaxXor; ++b)          if (left[i][k][a] && right[i + 1][k][b])            ans = max(ans, a ^ b);     return ans;  }  private:  static constexpr int kMaxXor = 128;   // Gets all possible OR values till each index and number of numbers.  vector<vector<vector<bool>>> getPossibleORs(const vector<int>& nums, int k) {    // dp[i][j][x] := true if it's possible to get an OR value of x by selecting    // j numbers from nums[0..i]    vector<vector<vector<bool>>> dp(        nums.size(), vector<vector<bool>>(k + 1, vector<bool>(kMaxXor + 1)));     dp[0][1][nums[0]] = true;     // No number is selected.    for (int i = 0; i < nums.size(); ++i)      dp[i][0][0] = true;     for (int i = 1; i < nums.size(); ++i)      for (int j = 1; j <= k; ++j)        for (int x = 0; x <= kMaxXor; ++x) {          // 1. Skip the current number.          if (dp[i - 1][j][x])            dp[i][j][x] = true;          // 2. Pick the current number.          if (dp[i - 1][j - 1][x])            dp[i][j][nums[i] | x] = true;        }     return dp;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗