Can anyone tell me why this code is giving output 32
instead of 23
for test case
"bbbaaa" [4,9,3,8,8,9]
I tried dry running it 2 times,still can’t find any mistake in it.
Code:-
class Solution {
public:
int minCost(string s, vector<int>& neededTime) {
int i=0;
int maxTime=0;
int currentTotalTime=0;
int ans=0;
while(i<neededTime.size())
{
if(i<neededTime.size()-1&&s[i]==s[i+1])
{
currentTotalTime=0;
maxTime=0;
while((i<s.length()&&s[i]==s[i+1])||(i>0&&s[i]==s[i-1]))
{
currentTotalTime+=neededTime[i];
maxTime=max(neededTime[i],maxTime);
i++;
}
ans+=currentTotalTime-maxTime;
}
else
i++;
}
return ans;
}
};
Problem Link:-