思维题专题
参考:分享丨【题单】贪心算法(基本贪心策略/反悔/区间/字典序/数学/思维/构造)- 讨论 - 力扣(LeetCode)
一、 从特殊到一般
Leetcode 781. 森林中的兔子
思考:“从特殊到一般”找规律
特殊:
一般:
(上取整转变为下取整的方法参见“数学专题”)
Code:
class Solution { public:int numRabbits(vector<int>& answers) {unordered_map<int, int> cnt;for (int x: answers) cnt[x] ++;int res = 0;for (auto& [x, c]: cnt) res += (c + x) / (x + 1) * (x + 1);return res;} };