当前位置: 首页 > news >正文

2024.6.13每日一题

LeetCode

子序列最大优雅度

题目链接:2813. 子序列最大优雅度 - 力扣(LeetCode)

题目描述

给你一个长度为 n 的二维整数数组 items 和一个整数 k

items[i] = [profiti, categoryi],其中 profiticategoryi 分别表示第 i 个项目的利润和类别。

现定义 items子序列优雅度 可以用 total_profit + distinct_categories2 计算,其中 total_profit 是子序列中所有项目的利润总和,distinct_categories 是所选子序列所含的所有类别中不同类别的数量。

你的任务是从 items 所有长度为 k 的子序列中,找出 最大优雅度

用整数形式表示并返回 items 中所有长度恰好为 k 的子序列的最大优雅度。

**注意:**数组的子序列是经由原数组删除一些元素(可能不删除)而产生的新数组,且删除不改变其余元素相对顺序。

示例 1:

输入:items = [[3,2],[5,1],[10,1]], k = 2
输出:17
解释:
在这个例子中,我们需要选出长度为 2 的子序列。
其中一种方案是 items[0] = [3,2] 和 items[2] = [10,1] 。
子序列的总利润为 3 + 10 = 13 ,子序列包含 2 种不同类别 [2,1] 。
因此,优雅度为 13 + 22 = 17 ,可以证明 17 是可以获得的最大优雅度。 

示例 2:

输入:items = [[3,1],[3,1],[2,2],[5,3]], k = 3
输出:19
解释:
在这个例子中,我们需要选出长度为 3 的子序列。 
其中一种方案是 items[0] = [3,1] ,items[2] = [2,2] 和 items[3] = [5,3] 。
子序列的总利润为 3 + 2 + 5 = 10 ,子序列包含 3 种不同类别 [1, 2, 3] 。 
因此,优雅度为 10 + 32 = 19 ,可以证明 19 是可以获得的最大优雅度。

示例 3:

输入:items = [[1,1],[2,1],[3,1]], k = 3
输出:7
解释:
在这个例子中,我们需要选出长度为 3 的子序列。
我们需要选中所有项目。
子序列的总利润为 1 + 2 + 3 = 6,子序列包含 1 种不同类别 [1] 。
因此,最大优雅度为 6 + 12 = 7 。

提示:

  • 1 <= items.length == n <= 105
  • items[i].length == 2
  • items[i][0] == profiti
  • items[i][1] == categoryi
  • 1 <= profiti <= 109
  • 1 <= categoryi <= n
  • 1 <= k <= n

思路

代码

C++
class Solution {
public:long long findMaximumElegance(vector<vector<int>>& items, int k) {// 把利润从大到小排序ranges::sort(items, [](const auto &a, const auto &b) { return a[0] > b[0]; });long long ans = 0, total_profit = 0;unordered_set<int> vis;stack<int> duplicate; // 重复类别的利润for (int i = 0; i < items.size(); i++) {int profit = items[i][0], category = items[i][1];if (i < k) {total_profit += profit; // 累加前 k 个项目的利润if (!vis.insert(category).second) { // 重复类别duplicate.push(profit);}} else if (!duplicate.empty() && vis.insert(category).second) { // 之前没有的类别total_profit += profit - duplicate.top(); // 选一个重复类别中的最小利润替换duplicate.pop();} // else:比前面的利润小,而且类别还重复了,选它只会让 total_profit 变小,vis.size() 不变,优雅度不会变大ans = max(ans, total_profit + (long long) vis.size() * (long long) vis.size());}return ans;}
};
Java
class Solution {public long findMaximumElegance(int[][] items, int k) {// 把利润从大到小排序Arrays.sort(items, (a, b) -> b[0] - a[0]);long ans = 0;long totalProfit = 0;Set<Integer> vis = new HashSet<>();Deque<Integer> duplicate = new ArrayDeque<>(); // 重复类别的利润for (int i = 0; i < items.length; i++) {int profit = items[i][0];int category = items[i][1];if (i < k) {totalProfit += profit; // 累加前 k 个项目的利润if (!vis.add(category)) { // 重复类别duplicate.push(profit);}} else if (!duplicate.isEmpty() && vis.add(category)) { // 之前没有的类别totalProfit += profit - duplicate.pop(); // 选一个重复类别中的最小利润替换} // else:比前面的利润小,而且类别还重复了,选它只会让 totalProfit 变小,vis.size() 不变,优雅度不会变大ans = Math.max(ans, totalProfit + (long) vis.size() * vis.size()); // 注意 1e5*1e5 会溢出}return ans;}
}
http://www.lryc.cn/news/370985.html

相关文章:

  • Linux命令详解(2)
  • iOS ReactiveCocoa MVVM
  • 图文解析ASN.1中BER编码:结构类型、编码方法、编码实例
  • jQuery如何停止动画队列
  • vue3+electron搭建桌面软件
  • oracle常用经典SQL查询
  • Android shell 常用 debug 命令
  • Unity3D Shader数据传递语法详解
  • 计算机组成原理(五)
  • 后端项目实战--瑞吉外卖项目软件说明书
  • LeetCode | 27.移除元素
  • 为什么要选择AWS?AWS的优势有哪些?
  • 【Intel CVPR 2024】通过图像扩散模型生成高质量360度场景,只需要一个语言模型
  • postman教程-21-Newman运行集合生成测试报告
  • 基于条件谱矩的时间序列分析(以轴承故障诊断为例,MATLAB)
  • ArcGIS Pro 3.0加载在线高德地图
  • 服务器防漏扫,主机加固方案来解决
  • Linux2(基本命令2)
  • 拼团+秒杀+优惠折扣+个人免签双端商城源码
  • 【数据结构】双向链表(C语言)
  • 【TensorFlow深度学习】WGAN与DCGAN在图像生成中的应用实例
  • 垫付商贩任务补单平台补单系统网站源码提供
  • vue富文本wangeditor加@人功能(vue2 vue3都可以)
  • ######## redis各章节终篇索引(更新中) ############
  • 一个基于MySQL的数据库课程设计的基本框架
  • 架构设计基本原则
  • 云原生应用开发培训,开启云计算时代的新征程
  • 【数据库设计】宠物商店管理系统
  • 前端 JS 经典:node 的模块查找策略
  • C++中的23种设计模式