Given n pieces of wood with lengthL[i](integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.

Notice

You couldn't cut wood into float length.

If you couldn't get >=_k_pieces, return0.

Have you met this question in a real interview?

Yes

Example

ForL=[232, 124, 456],k=7, return114.

Challenge

O(n log Len), where Len is the longest length of the wood.

line 17 variable name L , not A check variable name

line 34 vector, not Vector,

check 1 error while reading code empty() instead of empty

class Solution {

public:

/\*\* 

 \*@param L: Given n pieces of wood with length L\[i\]

 \*@param k: An integer

 \*return: The maximum length of the small pieces.

 \*/

int woodCut\(vector<int> L, int k\) {

    // write your code here

    int maxLen = 0;

    for \(auto &len : L\) {

        maxLen = std::max\(len, maxLen\);

    }

    int start = 1, end = maxLen;

    while \(start + 1 < end\) {

        int mid = start + \(end - start >> 1\);

        if \(count\(L, mid\) >= k\) {

            start = mid;

        } else {

            end = mid;

        }

    }

    if \(count\(L, end\) >= k\) {

        return end;

    }

    if \(count\(L, start\) >= k\) {

        return start;

    }

    return 0;

}

private:

// how many pieces can if cut in len

int count\(vector<int> &L, int len\) {

    int res = 0;

    for\(auto &woodLen : L\) {

        res += woodLen / len;

    }

    return res;

}

};

results matching ""

    No results matching ""