首页 > 其他分享 >LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K

LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K

时间:2024-04-03 11:46:33浏览次数:19  
标签:count 2461 nums Distinct Subarrays sum length subarrays subarray

原题链接在这里:https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/description/

题目:

You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions:

  • The length of the subarray is k, and
  • All the elements of the subarray are distinct.

Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The subarrays of nums with length 3 are:
- [1,5,4] which meets the requirements and has a sum of 10.
- [5,4,2] which meets the requirements and has a sum of 11.
- [4,2,9] which meets the requirements and has a sum of 15.
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions

Example 2:

Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The subarrays of nums with length 3 are:
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.

Constraints:

  • 1 <= k <= nums.length <= 105
  • 1 <= nums[i] <= 105

题解:

Have a map to maintain the num and its frequency.

When a num's frequency changes from 0 to 1, then distinct count++.

When i >= k - 1, then we start to have subarray with length k.

If current distinct count == k, then all the numbers in subarray are distinct. Update the maximum result.

Move the walker, update sum and count.

Time Complexity: O(n). n = nums.length.

Space: O(k).

AC Java:

 1 class Solution {
 2     public long maximumSubarraySum(int[] nums, int k) {
 3         long res = 0;
 4         int n = nums.length;
 5         int count = 0;
 6         HashMap<Integer, Integer> hm = new HashMap<>();
 7         long sum = 0;
 8         for(int i = 0; i < n; i++){
 9             sum += nums[i];
10             hm.put(nums[i], hm.getOrDefault(nums[i], 0) + 1);
11             if(hm.get(nums[i]) == 1){
12                 count++;
13             }
14 
15             if(i >= k - 1){
16                 if(count == k){
17                     res = Math.max(res, sum);
18                 }
19 
20                 sum -= nums[i - k + 1];
21                 hm.put(nums[i - k + 1], hm.get(nums[i - k + 1]) - 1);
22                 if(hm.get(nums[i - k + 1]) == 0){
23                     count--;
24                 }
25             }
26         }
27 
28         return res;
29     }
30 }

 

标签:count,2461,nums,Distinct,Subarrays,sum,length,subarrays,subarray
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18112316

相关文章

  • 【数据库】PostgreSQL中使用`SELECT DISTINCT`和`SUBSTRING`函数实现去重查询
    在PostgreSQL中,我们可以使用SELECTDISTINCT和SUBSTRING函数来实现对某个字段进行去重查询。本文将介绍如何使用这两个函数来实现对resource_version字段的去重查询。1.SELECTDISTINCT语句SELECTDISTINCT语句用于从表中选择不重复的记录。如果没有指定列名,则会选择所有列。在......
  • pg distinct 改写递归优化(德哥的思路)
    德哥的优化思路巨牛逼,这种递归思维真的太吊了,我目前就缺递归思路。 下面SQL1000W行数据,列的选择性很低,只有两个值('1'和'11')都是字符串类型,'1'只有一条数据,'11'有9999999行数据。慢SQL:selectdistinctcolfromtt;......
  • Lambda实现条件去重distinct List
    原文链接:https://blog.csdn.net/qq_39940205/article/details/114269686  _______________________________________________________________________________________________________________我们知道,Java8lambda自带的去重为distinct方法,但是只能过滤整体对象,不......
  • CF1398C Good Subarrays(写给我们萌新团体)
    GoodSubarrays传送门:GoodSubarrays-洛谷|计算机科学教育新生态(luogu.com.cn)思路暴力!!!!!一如既往的暴力!!!复杂度O(n^2)数据n到1e5TLE必定TLE我们可以用一个桶来优化实质上其实还是高中所学的排列组合思想第一步:当然是前缀和了,这边讲给新手写一下,有点冗杂,是高手直接......
  • C1. Good Subarrays (Easy Version)
    找子数组的个数双指针#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintN=2e5+10;inta[N];voidsolve(){ intn; cin>>n; for(inti=1;i<=n;i++)cin>>a[i]; intl=1,r=1; intans=0; while(l<=r){ if(l>n||r>......
  • Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)
    很意思的一道构造题题意:给一个\(n、k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)*n/2\),每个数的范围为\([-1000,1000]\)这种构造题可以考虑就是前一段可以一直用一样的、最小的。我们观察可以发现\(k+k-(n+1)*n/2=(n+1)*n/2\)也就是所有子数组......
  • 【数据库】PostgreSQL中的DISTINCT ON和DISTINCT的区别
    深入理解PostgreSQL中的DISTINCTON和DISTINCT在数据库查询中,我们经常会遇到需要去除重复数据的情况。在PostgreSQL中,我们可以使用DISTINCT和DISTINCTON来实现这个目标。那么,它们之间有什么区别呢?本文将详细介绍这两种方法的用法、区别以及适用场景。DISTINCT的基本用法DISTIN......
  • 【数据库】SQL 错误 [42P10] ERROR SELECT DISTINCT ON expressions must match ini
    SQL错误[42P10],表示在使用SELECTDISTINCTON语句时,表达式必须与初始的ORDERBY表达式匹配。这个错误通常发生在你尝试对不同的列进行去重操作时,而这些列并没有在ORDERBY子句中明确指定。为什么会出现这个错误?当你使用SELECTDISTINCTON语句时,你需要提供一个或多个......
  • MySQL 中的 distinct 和 group by 哪个效率更高?
    先说大致的结论(完整结论在文末):在语义相同,有索引的情况下:groupby和distinct都能使用索引,效率相同。在语义相同,无索引的情况下:distinct效率高于groupby。原因是distinct和 groupby都会进行分组操作,但groupby可能会进行排序,触发filesort,导致sql执行效率低下。基于这个......
  • sql 语句中的DISTINCT以及在count中的使用
    原文链接:https://www.cnblogs.com/tanshuai1001/p/8761378.htmlhttps://baijiahao.baidu.com/s?id=1709966309120511971&wfr=spider&for=pcdistrict必须放在所有字段前面:SELECTDISTINCTstudent,classFROMcourses单字段时按照字段筛选,多字段是以所有字段的值作为key来筛......