You are viewing a single comment's thread. Return to all comments →
C++ Space Optimized Solution :
int cost(vector<int> b){ int n = int(b.size()); int prev_0 = 0, prev_1 = 0; int ans0 = 0, ans1 = 0; for(int i = 1; i < n ; ++i){ ans0 = max(prev_0+abs(b[i]-b[i-1]), prev_1+abs(1-b[i])); ans1 = prev_0 + abs(1 - b[i-1]); prev_0 = ans0; prev_1 = ans1; } return max(ans0, ans1); }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Cost
You are viewing a single comment's thread. Return to all comments →
C++ Space Optimized Solution :