首页 > 其他分享 >leetcode 2448

leetcode 2448

时间:2022-10-24 17:02:24浏览次数:43  
标签:tmp cost int 2448 note num leetcode

首先需要这个结论.

 

 

而这里 a_ia
i

为任意正整数,我们便可以直接将其 **拆为 **a_ia
i

个系数为 11 的绝对值表达式的和。接下来只需要考虑全体的中位数即可(采用关于数据本身排序,再记录累积的 costcost 何时突破总 costcost 的一半)。

作者:小羊肖恩
链接:https://leetcode.cn/circle/discuss/uO4WuN/view/CUy95z/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
tmp = sorted(zip(nums, cost))
tot, note = sum(cost), 0
for num, c in tmp:
note += c
if note > tot // 2:
chosen = num
break
return sum(c * abs(num - chosen) for num, c in tmp)

作者:小羊肖恩
链接:https://leetcode.cn/circle/discuss/uO4WuN/view/CUy95z/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签:tmp,cost,int,2448,note,num,leetcode
From: https://www.cnblogs.com/zhangbo2008/p/16822007.html

相关文章