Skip to content

Commit 848dfec

Browse files
committed
Time: 36 ms (73.52%), Space: 57.1 MB (84.54%) - LeetHub
1 parent c8d6f4a commit 848dfec

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public long maximumSubarraySum(int[] nums, int k) {
3+
//Sliding window ::
4+
int n = nums.length;
5+
Set<Integer> set = new HashSet<>();
6+
int start =0;
7+
int end =0;
8+
long sum=0, ans =0;
9+
while(end<n){
10+
while (set.contains(nums[end]) || set.size() == k) {
11+
set.remove(nums[start]);
12+
sum -= nums[start++];
13+
}
14+
sum += nums[end];
15+
set.add(nums[end]);
16+
if (set.size() == k) {
17+
ans = Math.max(ans, sum);
18+
}
19+
end++;
20+
}
21+
return ans;
22+
}
23+
}

0 commit comments

Comments
 (0)