题解 | #最大放牛数#
最大放牛数
https://www.nowcoder.com/practice/5ccfbb41306c445fb3fd35a4d986f8b2
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pasture int整型一维数组
* @param n int整型
* @return bool布尔型
*/
public boolean canPlaceCows (int[] pasture, int n) {
int len=pasture.length;
int cnt=0;
if(pasture[0]==0&&pasture[1]==0){
pasture[0]=1;
cnt++;
}
for(int i=1;i<len-1;i++){
if(pasture[i-1]==0&&pasture[i+1]==0&&pasture[i]==0){
cnt++;
pasture[i]=1;
}
}
if(pasture[len-2]==0&&pasture[len-1]==0){
pasture[len-1]=1;
cnt++;
}
return cnt>=n;
}
}
查看11道真题和解析