[10.9] 兴业数金 笔试

客观题

闹麻了,单选多选考了一堆安全方向的,直接乱答

编程题

原题在 洛谷

二分法找到木材长度即可,笔者用了闭区间二分,首先降序排序,接着初始化左右界为 nums[k-1] 和 nums[0],注意特判 k-1 越界情况。

class Solution:
    def maxLength(self, nums: List[int], k: int) -> int:
        # write code here
        nums.sort(reverse=True)

        L = nums[min(k, len(nums)) - 1]
        R = nums[0]

        def could_cut(m):
            cut = 0
            for num in nums:
                if num < m or cut >= k:
                    break
                cut += num // m
            # print(nums, "m = {}".format(m))
            return cut >= k

        max_m = 0
        while L <= R:
            M = (R - L) // 2 + L
            if could_cut(M):
                L = M + 1
                max_m = max(max_m, M)
            else:
                R = M - 1

        return max_m

#笔试吐槽##笔试#
全部评论

相关推荐

投递顺丰集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务