Skip to content

Commit

Permalink
June14: DP state transition w/ fixed size array
Browse files Browse the repository at this point in the history
Time: O(N), Space: O(1), DP practice
  • Loading branch information
aucker committed Jun 14, 2024
1 parent 72103d1 commit 26d202b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions daily/June14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <bits/stdc++.h>

#include <limits>
using namespace std;

class Solution {
public:
/**
* @brief LC: 2786: Visit Array positions to max score
* This is a DP problem, with optimized solution
* Time: O(N), Space: O(1)
* a vector w/ fixed size like 2 is Space complexity of O(1)
* But a vector space complexity is usually O(N)
*
* @param nums
* @param x
* @return long long
*/
long long maxScore(vector<int>& nums, int x) {
// vector<long long> dp = {INT_MIN, INT_MIN};
vector<long long> dp(2, numeric_limits<int>::min());
dp[nums[0] % 2] = nums[0];
for (int i = 1; i < nums.size(); i++) {
int state = nums[i] % 2;
dp[state] = max(dp[state] + nums[i], dp[abs(state - 1)] + nums[i] - x);
}
return max(dp[0], dp[1]);
}

// long long maxScoreOP(vector<int>& nums, int x) {
// bool isEven = nums[0] % 2 == 0;
// long long oddMax = 0, evenMax = 0;
// if (isEven) evenMax = nums[0];
// else oddMax = nums[0];

// for (size_t i = 1; i < nums.size(); i++) {
// isEven = nums[i] % 2 == 0;
// if (isEven) evenMax = max(evenMax + nums[i], oddMax + nums[i] - x);
// else oddMax = max(oddMax + nums[i], evenMax + nums[i] - x);
// }
// return max(oddMax, evenMax);
// }
};

0 comments on commit 26d202b

Please sign in to comment.