题解 | #最大放牛数#
最大放牛数
https://www.nowcoder.com/practice/5ccfbb41306c445fb3fd35a4d986f8b2
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pasture int整型vector * @param n int整型 * @return bool布尔型 */ bool canPlaceCows(vector<int>& pasture, int n) { // write code here for (int i = 0;i < pasture.size() - 2;++i) { if (pasture[i] == 1 && pasture[i + 1] == 0 && pasture[i + 2] == 0) { n--; pasture[i + 2] = 1; } if (pasture[i] == 0 && pasture[i + 1] == 0 && pasture[i + 2] == 0) { n -= 2; pasture[i] = 1; pasture[i + 2] = 1; } } return (n <= 0) ? true : false; } };