长度为 K 的不同子数组的最大和。 Leetcode 2461 [关闭]

问题描述 投票:0回答:0

Result is **TIME LIMIT EXCEEDED** . Unable to find the problem. Link of question given below.

在这里尝试应用滑动窗口的概念

链接:https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/ 这是我的代码

class Solution {
    public long maximumSubarraySum(int[] nums, int k) {
       int i=0,j=0;
       int n= nums.length;
        long sum =0;
        long maxsum = 0;

       while(j<n){
         sum = sum + nums[j];
         if(j-i+1 <k){
           j++;
         }
         else if(j-i+1 == k){
           Set <Integer> s = new HashSet<>();
             for(int m=i; m<=j;i++){
               s.add(nums[m]);
             }
             int size = s.size();
             if(size==k){
               maxsum = Math.max(maxsum, sum);
               i++;
               j++;
             }         
         }

       }
        return maxsum;
    }
}
java arrays sliding-window sub-array
© www.soinside.com 2019 - 2024. All rights reserved.