woodcut

http://www.lintcode.com/en/problem/wood-cut/#


二分答案,贪心验证,具有单调性


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 L, int k){
        if(L.empty()) return 0;
        LL low=0, high=*max_element(L.begin(), L.end());
        while(low=k) return high;
                else return low;
            }
            LL mid=(low+high)/2;
            LL ans=0;
            for(auto e: L){
                ans+=e/mid;
            }
            if(ans>=k)  low=mid;
            else high=mid-1;
        }
        return low;
    }
}S;

introsort C++ STL 没有最坏情况

timsort msort isort 综合

dual pivot qsort JDK7 arrays.sort 里面采用 任然有最坏n^2情况

你可能感兴趣的:(数据结构,面试算法)