## 问题1

-
有这么一组木头(用数组int[]表示),木头长度>=1且长短不一
-
木头只能切短、不能拼接
-
给定一个要求的木头长度len和一组木头woods,要求将woods切成长度均为len的木头,请问最多能切出多少根?
### 解题思路
题目不难,因为只能切短不能拼接,所以直接循环遍历woods,分别将每根木头切成要求的长度
叠加每根木头能切出的要求长度木头的数量,即可求解
### 代码实现
public static Integer cutWoods2SpecifyLen(int[] woods, int len) {
if (woods == null || woods.length == 0) {
return 0;
}
int result = 0;
for (int wood : woods) {
result += wood / len;
}
return result;
}
时间复杂度O(n)
空间复杂度O(1)
测试验证一下
public static void main(String[] args) {
int[] woods = {100, 110, 50, 60, 100, 90};
int specifyLen = 30;
System.out.println(Arrays.toString(woods));
System.out.printf("给定一组木头,要求切成%d长度,最多能切成%d根%n", specifyLen, cutWoods2SpecifyLen(woods, specifyLen));
}

## 问题2

本文介绍两种木头切割问题的算法实现:一是给定长度求可切出的等长木头数量;二是给定数量求最长等长木头长度。通过遍历与二分查找解决实际问题。
最低0.47元/天 解锁文章
869

被折叠的 条评论
为什么被折叠?



