What is wring in the solution?


#1

int Solution::candy(vector &arr) {

int min_i = 0;
for (int i = 1; i < arr.size(); i++) {
    if (arr[i] < arr[min_i])
        min_i = i;
}
int ans = 1, curr_can = 1;
for (int i = min_i - 1; i >= 0; --i) {
    if (arr[i] > arr[i + 1]) {
        curr_can++;
        ans = ans + curr_can;
    } else {
        curr_can = 1;
        ans += curr_can;
    }
}

curr_can = 1;
for (int i = min_i + 1; i < arr.size(); ++i) {
    if (arr[i] > arr[i - 1]) {
        curr_can++;
        ans = ans + curr_can;
    } else {
        curr_can = 1;
        ans += curr_can;
    }
}

return ans;

}


#2

Did you figure out the problem with the above approach? I used the same approach and getting wrong answer in one of the test cases.