class Solution { public: int numSquares(int n) { vector<int> c; for (int i = 1; i*i <=n; i++) c.push_back(i*i); queue<int> q; q.push(0); int lv = 0; while (!q.empty()) { int qs = q.size(); lv++; while (qs-- > 0) { int cur = q.front(); q.pop(); for (int i : c) { if (cur + i == n) { return lv; } else if (cur + i > n) break; else q.push(cur+i); } } } return lv; } };