Leetcode 1005. K 次取反后最大化的数组和(DAY 116) ---- 贪心算法学习期
发布日期:2021-06-30 22:31:18
浏览次数:2
分类:技术文章
本文共 811 字,大约阅读时间需要 2 分钟。
原题题目
代码实现(首刷自解)
class Solution { public: int largestSumAfterKNegations(vector & nums, int k) { priority_queue,greater > q; int sum = 0; for(const auto& num:nums) q.emplace(num); while(k) { int temp = q.top(); q.pop(); if(temp >= 0) { if(!(k%2)) { q.emplace(temp); break; } else { q.emplace(-temp); break; } } else q.emplace(-temp); --k; } while(!q.empty()) { sum += q.top(); q.pop(); } return sum; }};
转载地址:https://love6.blog.csdn.net/article/details/116980583 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月24日 16时09分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
poj 1936 All in All
2019-04-30
hihocoder 1555 四次方根(矩阵快速幂)
2019-04-30
codeforces 449 D Jzzhu and Numbers(容斥+dp)
2019-04-30
hdu 5450 Traversal(状压dp)
2019-04-30
Gym - 101334E Exploring Pyramids(dp+分治)
2019-04-30
uva 10934 Dropping water balloons(dp)
2019-04-30
hdu 6183 Color it(cdq分治+线段树)
2019-04-30
gym 101137 L Lazy Coordinator(概率)
2019-04-30
Mysql 入门
2019-04-30
Vue 之 axios 篇
2019-04-30
vue 之动画
2019-04-30
js的基础语法
2019-04-30
vue使用iconfont
2019-04-30
linux 查看文件夹权限
2019-04-30
linux tar 备份
2019-04-30
ubuntu中配置任意应用的快捷键
2019-04-30
ln 命令 硬链接
2019-04-30
使用鼠标中键在vim下赋值文本到其它编辑器
2019-04-30
chkconfig使用和级别介绍
2019-04-30