Incomplete test? Wrong code got AC?


  • 0
    L

    Here is my AC code. I ignored the condition that 2 adjacent kids have same rating but still got AC.

    class Solution {
    public:
        int candy(vector<int> &ratings) {
            int len = ratings.size();
            if(len == 0) return 0;
            if(len == 1) return 1;
            vector<int> c(len, 1);
            for(int i = 1; i < len; ++i) c[i] = ratings[i] > ratings[i-1] ? c[i-1]+1 : c[i];
            for(int i = len-2; i >= 0; --i) c[i] = ratings[i] > ratings[i+1] ? max(c[i+1]+1, c[i]) : c[i];
            int ret = 0;
            for(int i = 0; i < len; ++i) ret += c[i];
            return ret;
        }
    }; 
    

    I found the test case {1, 2, 2} and the given answer is 4. Isn't it 5? If a kid have same ratings with his neighbor, shouldn't be assigned the same number of candies?


  • 0
    F

    Read the question carefully and you will see why.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.