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

Leetcode3026. 最大好子数组和

Every day a Leetcode

题目来源:3026. 最大好子数组和

解法1:哈希 + 前缀和

哈希表 hash = unordered_map<int, vector<long long>> 存储数组 nums 的元素 x 及其到 x 为止的前缀和 preSum。

遍历数组 nums,设当前元素为 x,前缀和为 sum:

  1. 在哈希表中寻找键 x + k,若找到,更新答案 ans = max(ans, sum + x - *min_element(it->second.begin(), it->second.end()));
  2. 在哈希表中寻找键 x - k,若找到,更新答案 ans = max(ans, sum + x - *min_element(it->second.begin(), it->second.end()));
  3. 向哈希表中插入 hash[x].push_back(sum);
  4. 更新前缀和 sum += x。

最后返回答案。

代码:

/** @lc app=leetcode.cn id=3026 lang=cpp** [3026] 最大好子数组和*/// @lc code=start
class Solution
{
public:long long maximumSubarraySum(vector<int> &nums, int k){long long ans = LLONG_MIN, sum = 0;unordered_map<int, vector<long long>> hash;for (int &x : nums){auto it = hash.find(x + k);if (it != hash.end())ans = max(ans, sum + x - *min_element(it->second.begin(), it->second.end()));it = hash.find(x - k);if (it != hash.end())ans = max(ans, sum + x - *min_element(it->second.begin(), it->second.end()));hash[x].push_back(sum);sum += x;}return ans == LLONG_MIN ? 0 : ans;}
};
// @lc code=end

结果:

超时。

在这里插入图片描述

复杂度分析:

时间复杂度:O(n),其中 n 是数组 nums 的元素个数。

空间复杂度:O(n),其中 n 是数组 nums 的元素个数。

优化

我们发现,哈希表中最需要存储元素 x 及其对应的最小的前缀和,这样算出来的子数组元素总和是最大的。

修改:

  1. 更新答案 ans = max(ans, sum + x - it->second);
  2. 每次还有在哈希表中查找 x,如果找不出或者当前前缀和 sum 小于 hash[x],更新 hash[x] = sum。

代码:

/** @lc app=leetcode.cn id=3026 lang=cpp** [3026] 最大好子数组和*/// @lc code=start
class Solution
{
public:long long maximumSubarraySum(vector<int> &nums, int k){long long ans = LLONG_MIN, sum = 0;unordered_map<int, long long> hash;for (int &x : nums){auto it = hash.find(x + k);if (it != hash.end())ans = max(ans, sum + x - it->second);it = hash.find(x - k);if (it != hash.end())ans = max(ans, sum + x - it->second);it = hash.find(x);if (it == hash.end() || sum < it->second)hash[x] = sum;sum += x;}return ans == LLONG_MIN ? 0 : ans;}
};
// @lc code=end

结果:

在这里插入图片描述

复杂度分析:

时间复杂度:O(n),其中 n 是数组 nums 的元素个数。

空间复杂度:O(n),其中 n 是数组 nums 的元素个数。

相关文章:

  • 基于BP算法的SAR成像matlab仿真
  • Sora时代,我们的AI应该何去何从?——关于Sora大模型的思考
  • IIC--集成电路总线
  • C++ 多起点的bfs(五十九)【第六篇】
  • 文生图提示词:天气条件
  • 数据结构之时空复杂度
  • 软件工程师,超过35岁怎么办
  • spring cloud stream
  • MySQL的配置文件my.cnf正常的配置项目
  • 信息安全性测试
  • 【图像分割 2023】BRAU-Net++
  • 【HarmonyOS】hdc 环境变量设置
  • 前端开发:Vue框架与前端部署
  • linux查看系统日志
  • (02)Hive SQL编译成MapReduce任务的过程
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • Android系统模拟器绘制实现概述
  • EventListener原理
  • js
  • nodejs调试方法
  • ReactNativeweexDeviceOne对比
  • 马上搞懂 GeoJSON
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 译自由幺半群
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • #include<初见C语言之指针(5)>
  • #考研#计算机文化知识1(局域网及网络互联)
  • (003)SlickEdit Unity的补全
  • (52)只出现一次的数字III
  • (floyd+补集) poj 3275
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (第27天)Oracle 数据泵转换分区表
  • (非本人原创)我们工作到底是为了什么?​——HP大中华区总裁孙振耀退休感言(r4笔记第60天)...
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • *1 计算机基础和操作系统基础及几大协议
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • ./和../以及/和~之间的区别
  • .gitignore文件—git忽略文件
  • .Net core 6.0 升8.0
  • .NET Core 中插件式开发实现
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NetCore项目nginx发布
  • @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • @Transaction注解失效的几种场景(附有示例代码)
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • [ Linux 长征路第五篇 ] make/Makefile Linux项目自动化创建工具
  • [CSAWQual 2019]Web_Unagi ---不会编程的崽
  • [C语言]——柔性数组
  • [DAU-FI Net开源 | Dual Attention UNet+特征融合+Sobel和Canny等算子解决语义分割痛点]
  • [idea]关于idea开发乱码的配置