DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on

1578. Leetcode solution in CPP

class Solution {
public:
    int minCost(string s, vector<int>& cost) {
        int N = s.size(), ans = 0;
        for (int i = 0; i + 1 < N; ++i) {
            if (s[i] != s[i + 1]) continue;
            if (cost[i] > cost[i + 1]) swap(cost[i], cost[i + 1]);
            ans += cost[i];
        }
        return ans;
    }
};
Enter fullscreen mode Exit fullscreen mode

leetcode

challenge

here is the link for the problem:
https://leetcode.com/problems/minimum-time-to-make-rope-colorful/

Top comments (0)