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

LeetCode第1695题 - 删除子数组的最大得分

题目

解答

class Solution {public int maximumUniqueSubarray(int[] nums) {int left = 0, right = 0;int result = Integer.MIN_VALUE;int sum = 0;Set<Integer> set = new HashSet<>();while (right < nums.length) {int v1 = nums[right];if (set.add(v1)) {++right;sum += v1;continue;}result = Math.max(result, sum);if (left < nums.length) {set.remove(nums[left]);sum -= nums[left];++left;}}result = Math.max(result, sum);return result;}
}

测试用例

	@Testpublic void test001() {int result = s.maximumUniqueSubarray(new int[]{4, 2, 4, 5, 6});int expected = 17;Assert.assertTrue(String.valueOf(result), result == expected);}@Testpublic void test002() {int result = s.maximumUniqueSubarray(new int[]{5, 2, 1, 2, 5, 2, 1, 2, 5});int expected = 8;Assert.assertTrue(String.valueOf(result), result == expected);}@Testpublic void test003() {int result = s.maximumUniqueSubarray(new int[]{2, 4, 5, 6});int expected = 17;Assert.assertTrue(String.valueOf(result), result == expected);}@Testpublic void test004() {int result = s.maximumUniqueSubarray(new int[]{4, 2, 5, 6});int expected = 17;Assert.assertTrue(String.valueOf(result), result == expected);}@Testpublic void test005() {int result = s.maximumUniqueSubarray(new int[]{4});int expected = 4;Assert.assertTrue(String.valueOf(result), result == expected);}@Testpublic void test006() {int result = s.maximumUniqueSubarray(new int[]{4, 5});int expected = 9;Assert.assertTrue(String.valueOf(result), result == expected);}@Testpublic void test007() {int result = s.maximumUniqueSubarray(new int[]{4, 4});int expected = 4;Assert.assertTrue(String.valueOf(result), result == expected);}

总结
使用滑动窗口的套路来解题。

http://www.xdnf.cn/news/1346833.html

相关文章:

  • CSS自定义属性(CSS变量)
  • Jenkins发布spring项目踩坑——nohup java -jar发布后显示成功,但实际jps查询并未运行
  • kubernetes中pod的管理及优化
  • Python打卡Day49 CBAM注意力
  • Apache Ozone 2.0.0集群部署
  • 微信原生下载互联网oss资源保存到本地
  • CCleaner v1.2.3.4 中文解锁注册版,系统优化,隐私保护,极速清理
  • Unreal Engine Class System
  • 图数据库(neo4j)基础: 分类/标签 节点 关系 属性
  • 蓝牙部分解析和代码建构
  • set_disable_timing应用举例
  • OpenCV 图像边缘检测
  • 从“配置化思维”到“前端效率革命”:xiangjsoncraft 如何用 JSON 简化页面开发?
  • k8s 简介及部署方法以及各方面应用
  • 子类(派生类)使用父类(基类)的成员
  • 快速了解神经网络
  • AI赋能体育训练突破:AI动作捕捉矫正精准、战术分析系统提效率,运动员破瓶颈新路径
  • 数据结构之深入探索归并排序
  • go 常见面试题
  • NLP学习之Transformer(2)
  • 网络编程6(JVM)
  • 保护 PDF 格式:禁止转换为其他格式文件
  • html基本元素
  • C#_接口设计:角色与契约的分离
  • HTML5详篇
  • 自定义单线通信协议解析
  • Yapi中通过MongoDB修改管理员密码与新增管理员
  • 【Java后端】 Spring Boot 集成 Redis 全攻略
  • 软件设计师——计算机网络学习笔记
  • 华为网路设备学习-29(BGP协议 四)路由策略-实验