Anybody can help me ? Run Custom case is good but submit will get Runtime Error


  • 0

    my code is here:

    class Solution {
    public:
            static bool first0Then1(const string& x, const string& y)
        {
            size_t zerosOfX = 0, onesOfX = 0, zerosOfY = 0, onesOfY = 0;
            zerosOfX = std::count(x.begin(), x.end(), '0');
            onesOfX = x.size() - zerosOfX;
            zerosOfY = std::count(y.begin(), y.end(), '0');
            onesOfY = y.size() - zerosOfY;
            if(zerosOfX != zerosOfY) return (zerosOfX < zerosOfY);
            else return (onesOfX <= onesOfY);
        }
    
        static bool first1Then0(const string& x, const string& y)
        {
            size_t zerosOfX = 0, onesOfX = 0, zerosOfY = 0, onesOfY = 0;
            zerosOfX = std::count(x.begin(), x.end(), '0');
            onesOfX = x.size() - zerosOfX;
            zerosOfY = std::count(y.begin(), y.end(), '0');
            onesOfY = y.size() - zerosOfY;
            if(onesOfX != onesOfY) return (onesOfX < onesOfY);
            else return (zerosOfX <= zerosOfY);
        }
    
        int findMaxForm(vector<string>& strs, int m, int n) {
            if(m < n)sort(strs.begin(), strs.end(), first0Then1);
            else sort(strs.begin(), strs.end(), first1Then0);
            int count = 0;
            for (int i = 0, size = strs.size(); i < size && m >= 0 && n >= 0; ++i) {
                string each = strs[i];
                size_t zerosNums = std::count(each.begin(), each.end(), '0');
                size_t onesNums = each.size() - zerosNums;
                if(m >= zerosNums && n >= onesNums){
                    count++;
                    m -= zerosNums;
                    n -= onesNums;
                }else {
                    break;
                }
            }
            return count;
        }
    };
    

    the case I get Runtime Error is :

    ["11","11","0","0","10","1","1","0","11","1","0","111","11111000","0","11","000","1","1","0","00","1","101","001","000","0","00","0011","0","10000"]
    90
    66
    

    But when I run it as a custom testcase , it will get the right result.
    Can anybody help me about this, I'm so confused.


  • 0

    BTW, the right result about that case is 29


Log in to reply
 

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