Sherlock and Cost Discussions | Algorithms | HackerRank
  • + 0 comments

    tricky question, the reasoning is far longer than the algorithm

    O(n)

    int cost(vector<int> B) {
        pair<int, int> Q = {0, 0};
        for (int i=1; i < B.size(); i++) Q = {max(Q.first, Q.second + B[i-1] - 1), max(Q.first + B[i] - 1, Q.second + abs(B[i]- B[i-1]))};
        return max(Q.first, Q.second);
    }