3477. 水果成篮 II
Problem: 3477. 水果成篮 II
文章目录
- 思路
- 解题过程
- 复杂度
- Code
思路
枚举每种水果,再枚举篮子的大小,如果大于等于水果的个数,就标记当前位置。
解题过程
枚举完毕如果
palce
仍为false
,ans++
。
复杂度
- 时间复杂度: O(n2)O(n^2)O(n2)
- 空间复杂度: O(1)O(1)O(1)
Code
class Solution {
public:int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {int ans = 0;int n = baskets.size();for (auto fruit : fruits) {bool place = false;for (int j = 0; j < n; j++) {if (fruit <= baskets[j]) {baskets[j] = 0;place = true;break;}}if (!place) {ans++;}}return ans;}
};