-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problems: No.2461,2841
* No.2461.Maximum Sum of Distinct Subarrays With Length K * No.2841.Maximum Sum of Almost Unique Subarray
- Loading branch information
Showing
13 changed files
with
288 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
solution/2400-2499/2461.Maximum Sum of Distinct Subarrays With Length K/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
public class Solution { | ||
public long MaximumSubarraySum(int[] nums, int k) { | ||
int n = nums.Length; | ||
Dictionary<int, int> cnt = new Dictionary<int, int>(k); | ||
long s = 0; | ||
|
||
for (int i = 0; i < k; ++i) { | ||
if (!cnt.ContainsKey(nums[i])) { | ||
cnt[nums[i]] = 1; | ||
} | ||
else { | ||
cnt[nums[i]]++; | ||
} | ||
s += nums[i]; | ||
} | ||
|
||
long ans = cnt.Count == k ? s : 0; | ||
|
||
for (int i = k; i < n; ++i) { | ||
if (!cnt.ContainsKey(nums[i])) { | ||
cnt[nums[i]] = 1; | ||
} | ||
else { | ||
cnt[nums[i]]++; | ||
} | ||
if (--cnt[nums[i - k]] == 0) { | ||
cnt.Remove(nums[i - k]); | ||
} | ||
|
||
s += nums[i] - nums[i - k]; | ||
|
||
if (cnt.Count == k) { | ||
ans = Math.Max(ans, s); | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.