LeetCode LCP 06. 拿硬币
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。
直接法:
class Solution {
public:
int minCount(vector<int>& coins) {
int res = 0;
for (int i : coins) {
res += (i + 1) / 2;
}
return res;
}
};
时间复杂度O(n),空间复杂度O(1)。