Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试题目|木头切割问题 #23

Open
Rain120 opened this issue Mar 12, 2022 · 1 comment
Open

面试题目|木头切割问题 #23

Rain120 opened this issue Mar 12, 2022 · 1 comment

Comments

@Rain120
Copy link
Owner

Rain120 commented Mar 12, 2022

题目描述

给定长度为n的数组,每个元素代表一个木头的长度,木头可以任意截断,从这堆木头中截出至少k个相同长度为m的木块。已知k,求max(m)。

ps: 截断的长度必须是整数

输入两行,第一行n, k,第二行为数组序列。输出最大值。

输入

5 5
4 7 2 10 5

输出

4

解释:如图,最多可以把它分成5段长度为4的木头

image

ps:数据保证有解,即结果至少是1。

@Rain120
Copy link
Owner Author

Rain120 commented Mar 12, 2022

function CutWood(nums, m) {
    let count = 0;

    while (nums.length) {
        const num = nums.shift();
        if (num >= m) {
            const rest = num - m;
            count++;

            rest && nums.push(rest);
        }
    }

    return count;
}

CutWood([4, 7, 2, 10, 5], 5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant