[InterviewBit] Flip Array

Flip Array

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Solution::solve(const vector<int> &A) {
int sum = 0, ma = 0;
for(auto a : A) sum += a;
vector<long long> dp(sum + 1, INT_MAX);
dp[0] = 0;
for(auto a : A) {
for(int i = sum; i >= a * 2; i--) {
dp[i] = min(dp[i-2*a] + 1, dp[i]);
}
}
for(int i = sum; i >= 0; i--) {
if(dp[i] < INT_MAX) return dp[i];
}
return -1;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/19/PS/interviewbit/flip-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.