Leetcode 1231: 分享巧克力 Divide Chocolate

这篇博客探讨了如何优化切分巧克力棒,以在与K位朋友分享时获得最大甜度。通过二分搜索算法,确定最佳切割方式,确保你能得到甜度最小的一块,同时最大化总甜度。文章提供了具体例子和时间复杂度分析,并介绍了如何调整二分搜索的边界条件来解决这个问题。
摘要由CSDN通过智能技术生成

中文描述:

你有一个由一些小块组成的巧克力棒。每一小块都有它自己的甜度,由数组sweetness给出。你想要和你的K个朋友分享巧克力,所以你开始切K次把巧克力切成K+1大块,每一大块都由一些连续的小块组成。孔融让梨,你会吃掉那块甜度最少的,然后把其他的分给你的朋友。设计算法优化切巧克力的方法,找出你能得到的最大甜度

题目描述:

You have one chocolate bar that consists of some chunks. Each chunk has its own sweetness given by the array sweetness.
You want to share the chocolate with your K friends so you start cutting the chocolate bar into K+1 pieces using K cuts, each piece consists of some consecutive chunks.
Being generous, you will eat the piece with the minimum total sweetness and give the other pieces to your friends.
Find the maximum total sweetness of the piece you can get by cutting the chocolate bar optimally.

Example 1:

Input: sweetness = [1,2,3,4,5,6,7,8,9], K = 5
Output: 6
Explanation: You can divide the chocolate to [1,2,3], [4,5], [6], [7], [8], [9]

Example 2:

Input: sweetness = [5,6,7,8,9,1,2,3,4], K = 8
Output: 1
Explanation: There is only one way to cut the bar into 9 pieces.

Example 3:

Input: sweetness = [1,2,2,1,2,2,1,2,2], K = 2
Output: 5
Explanation: You can divide the chocolate to [1,2,2], [1,2,2], [1,2,2]

Constraints:

0 <= K < sweetness.length <= 104
1 <= sweetness[i] <= 105

Time complexity: O O O(

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值