当前位置:网站首页>LeetCode 2333. 最小差值平方和(贪心)
LeetCode 2333. 最小差值平方和(贪心)
2022-08-02 17:48:00 【Michael阿明】
1. 题目
给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,长度为 n 。
数组 nums1 和 nums2 的 差值平方和 定义为所有满足 0 <= i < n 的 (nums1[i] - nums2[i])^2 之和。
同时给你两个正整数 k1 和 k2 。你可以将 nums1 中的任意元素 +1 或者 -1 至多 k1 次。
类似的,你可以将 nums2 中的任意元素 +1 或者 -1 至多 k2 次。
请你返回修改数组 nums1 至多 k1 次且修改数组 nums2 至多 k2 次后的最小 差值平方和 。
注意:你可以将数组中的元素变成 负 整数。
示例 1:
输入:nums1 = [1,2,3,4], nums2 = [2,10,20,19], k1 = 0, k2 = 0
输出:579
解释:nums1 和 nums2 中的元素不能修改,因为 k1 = 0 和 k2 = 0 。
差值平方和为:(1 - 2)^2 + (2 - 10)^2 + (3 - 20)^2 + (4 - 19)^2 = 579 。
示例 2:
输入:nums1 = [1,4,10,12], nums2 = [5,8,6,9], k1 = 1, k2 = 1
输出:43
解释:一种得到最小差值平方和的方式为:
- 将 nums1[0] 增加一次。
- 将 nums2[2] 增加一次。
最小差值平方和为:
(2 - 5)^2 + (4 - 8)^2 + (10 - 7)^2 + (12 - 9)^2 = 43 。
注意,也有其他方式可以得到最小差值平方和,但没有得到比 43 更小答案的方案。
提示:
n == nums1.length == nums2.length
1 <= n <= 10^5
0 <= nums1[i], nums2[i] <= 10^5
0 <= k1, k2 <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-sum-of-squared-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题
- 答案只与 对应元素的 非零 绝对值 有关系
- 对这些绝对值进行排序,大的优先减少
class Solution:
def minSumSquareDiff(self, nums1: List[int], nums2: List[int], k1: int, k2: int) -> int:
diff = list(filter(lambda x : x!=0, [abs(x-y) for x, y in zip(nums1, nums2)]))
# 非零 绝对值
diff.sort(reverse=True) # 逆序排列
if len(diff) == 0:
return 0
k = k1+k2 # 总的调整次数
diff.append(0) # 代码编写简单需要
prevnum = diff[0]
ct = 1 # 前面相同元素的个数
i = 0
while i < len(diff)-1:
delta = diff[i]-diff[i+1] # 跟后一个的差值
if delta == 0:
ct += 1 # 相同值的元素个数 + 1
else: # 遇到有下降台阶了
if k >= delta*ct: # k 的次数,够ct个数降到下个元素的值
prevnum = diff[i+1]
k -= delta*ct
ct += 1
else: # 不够, 结束了
delta = k//ct # 把剩余的 k 次 均匀分给 ct 个数
prevnum -= delta # 每个数还能降低 delta
left = k - delta*ct # 剩余的次数,每个数的值还能 -1
return int(math.pow((prevnum-1), 2)*left + math.pow(prevnum, 2)*(ct-left) + sum([x*x for x in diff[ct:]]))
i += 1
return 0
188 ms 31.4 MB Python3
1648 题是一样的思路,可以试试
https://leetcode.cn/problems/sell-diminishing-valued-colored-balls/
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
边栏推荐
- How a "cloud" can bring about new changes in the industry
- 一文看懂推荐系统:概要01:推荐系统的基本概念
- NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
- shell中awk命令的if条件语句引入外置变量
- 2021年下半年软件设计师上午真题
- Since September, China has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo
- 嵌入式Qt-做一个秒表
- golang刷leetcode 经典(4) 实现跳表
- 安全至上:落地DevSecOps最佳实践你不得不知道的工具
- redis总结_分布式缓存
猜你喜欢

什么是SVN(Subversion)?

How Tencent architects explained: The principle of Redis high-performance communication (essential version)

redis总结_分布式缓存

动力电池扩产潮,宁德时代遭围剿

深圳地铁16号线二期进入盾构施工阶段,首台盾构机顺利始发

【案例】2D变换-旋转动画

Security First: Tools You Need to Know to Implement DevSecOps Best Practices

redis summary_distributed cache

The days of patching are more difficult than the days of writing code

牛津硕士进碳圈,高瓴红杉经纬一起投了
随机推荐
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
MySQL基本查询和运算符
php弱类型-攻防世界lottery
千万级QPS下服务如何才能平滑启动
详细教学——1688关键词搜索API操作流程
golang刷leetcode动态规划(9)不同路径 II
C#里如何简单的校验时间格式
小程序毕设作品之微信体育馆预约小程序毕业设计成品(5)任务书
2021年下半年软件设计师上午真题
HDF驱动框架的API(2)
小程序毕设作品之微信体育馆预约小程序毕业设计成品(6)开题答辩PPT
二叉查找树的查找
vulnhub W34kn3ss: 1
Google Earth Engine APP—— 一个不用写代码可以直接下载相应区域的1984-2021年的GIF遥感影像动态图
暴跌99.7%后,谁还在买卖「二舅币」?
How Tencent architects explained: The principle of Redis high-performance communication (essential version)
Ubuntu系统下用docker安装oracle
2022安全员-C证考试题库模拟考试平台操作
有关代购系统搭建的那点事
How a "cloud" can bring about new changes in the industry