1419. 数青蛙_medium

解题思路

  1. croak 一定是按照顺序的
  2. croak 的字母个数一定是一样的
    否则返回 -1;

代码

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        if (croakOfFrogs.size() % 5 != 0) {
            return -1;
        }

        int res = 0;
        int frogNum = 0;
        vector<int> num(4);
        unordered_map<char, int> mpC = {{'c', 0}, {'r', 1}, {'o', 2}, {'a', 3}, {'k', 4}};
        for (char c : croakOfFrogs) {
            int tmp = mpC[c];
            if (tmp == 0) {
                num[tmp]++;
                frogNum++;
                if (frogNum > res) {
                    res = frogNum;
                }
            } else {
                if (num[tmp - 1] == 0) {
                    return -1;
                }

                num[tmp - 1]--;
                if (tmp == 4) {
                    frogNum--;
                } else {
                    num[tmp]++;
                }
            }
        }
        if (frogNum > 0) {
            return -1;
        }
        return res;
    }
};

1419. 数青蛙_medium_第1张图片

你可能感兴趣的:(#,《leetcode的刷题周记》,c++)