Skip to content

Commit

Permalink
added Minimum Time to Make Rope Colorful problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 committed Nov 8, 2022
1 parent 36cea33 commit 1282885
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Dynamic Programming/Minimum Time to Make Rope Colorful.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution
{
public:
int minCost(string nums, vector<int> &neededTime)
{
int n = nums.size(), i = 0;
long long res = 0;
while (i < n)
{
long long currSum = neededTime[i];
long long maxValue = neededTime[i];
bool flag = false;
while (i + 1 < n && nums[i] == nums[i + 1])
{
currSum += neededTime[i + 1];
if (maxValue < neededTime[i + 1])
{
maxValue = neededTime[i + 1];
}
i++;
flag = true;
}

if (flag)
{
res = res + currSum - maxValue;
}
else
{
i++;
}
}
return res;
}
};

0 comments on commit 1282885

Please sign in to comment.