题解 | 最大放牛数
最大放牛数
https://www.nowcoder.com/practice/5ccfbb41306c445fb3fd35a4d986f8b2
import java.util.*;
public class Solution {
// 0 0 0 0 -> 1 0 1 0
// 0 0 0 0 0 -> 1 0 1 0 1
// 0 0 -> 1 0
public boolean canPlaceCows (int[] pasture, int n) {
final int m = pasture.length;
for (int i = 0; i < m; ++i) {
if (pasture[i] == 0 && (i == 0 || pasture[i - 1] == 0) && (i == m - 1 || pasture[i + 1] == 0)) {
pasture[i] = 1;
--n;
}
if (n == 0) {
return true;
}
}
return false;
}
}
SHEIN公司福利 880人发布
查看24道真题和解析