Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.2461,2841
Browse files Browse the repository at this point in the history
* No.2461.Maximum Sum of Distinct Subarrays With Length K
* No.2841.Maximum Sum of Almost Unique Subarray
  • Loading branch information
yanglbme committed Apr 9, 2024
1 parent 05495d1 commit c517424
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ class Solution:
ans = s if len(cnt) == k else 0
for i in range(k, len(nums)):
cnt[nums[i]] += 1
s += nums[i]
cnt[nums[i - k]] -= 1
s -= nums[i - k]
if cnt[nums[i - k]] == 0:
del cnt[nums[i - k]]
cnt.pop(nums[i - k])
s += nums[i] - nums[i - k]
if len(cnt) == k:
ans = max(ans, s)
return ans
Expand All @@ -93,11 +92,10 @@ class Solution {
long ans = cnt.size() == k ? s : 0;
for (int i = k; i < n; ++i) {
cnt.merge(nums[i], 1, Integer::sum);
s += nums[i];
if (cnt.merge(nums[i - k], -1, Integer::sum) == 0) {
cnt.remove(nums[i - k]);
}
s -= nums[i - k];
s += nums[i] - nums[i - k];
if (cnt.size() == k) {
ans = Math.max(ans, s);
}
Expand All @@ -116,18 +114,16 @@ public:
unordered_map<int, ll> cnt;
ll s = 0;
for (int i = 0; i < k; ++i) {
cnt[nums[i]]++;
++cnt[nums[i]];
s += nums[i];
}
ll ans = cnt.size() == k ? s : 0;
for (int i = k; i < n; ++i) {
cnt[nums[i]]++;
s += nums[i];
cnt[nums[i - k]]--;
s -= nums[i - k];
if (cnt[nums[i - k]] == 0) {
++cnt[nums[i]];
if (--cnt[nums[i - k]] == 0) {
cnt.erase(nums[i - k]);
}
s += nums[i] - nums[i - k];
if (cnt.size() == k) {
ans = max(ans, s);
}
Expand All @@ -151,12 +147,11 @@ func maximumSubarraySum(nums []int, k int) (ans int64) {
}
for i := k; i < n; i++ {
cnt[nums[i]]++
s += int64(nums[i])
cnt[nums[i-k]]--
s -= int64(nums[i-k])
if cnt[nums[i-k]] == 0 {
delete(cnt, nums[i-k])
}
s += int64(nums[i] - nums[i-k])
if len(cnt) == k && ans < s {
ans = s
}
Expand All @@ -177,12 +172,11 @@ function maximumSubarraySum(nums: number[], k: number): number {
let ans = cnt.size === k ? s : 0;
for (let i = k; i < n; ++i) {
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
s += nums[i];
cnt.set(nums[i - k], cnt.get(nums[i - k])! - 1);
s -= nums[i - k];
if (cnt.get(nums[i - k]) === 0) {
cnt.delete(nums[i - k]);
}
s += nums[i] - nums[i - k];
if (cnt.size === k) {
ans = Math.max(ans, s);
}
Expand All @@ -191,6 +185,48 @@ function maximumSubarraySum(nums: number[], k: number): number {
}
```

```cs
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;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ class Solution:
ans = s if len(cnt) == k else 0
for i in range(k, len(nums)):
cnt[nums[i]] += 1
s += nums[i]
cnt[nums[i - k]] -= 1
s -= nums[i - k]
if cnt[nums[i - k]] == 0:
del cnt[nums[i - k]]
cnt.pop(nums[i - k])
s += nums[i] - nums[i - k]
if len(cnt) == k:
ans = max(ans, s)
return ans
Expand All @@ -91,11 +90,10 @@ class Solution {
long ans = cnt.size() == k ? s : 0;
for (int i = k; i < n; ++i) {
cnt.merge(nums[i], 1, Integer::sum);
s += nums[i];
if (cnt.merge(nums[i - k], -1, Integer::sum) == 0) {
cnt.remove(nums[i - k]);
}
s -= nums[i - k];
s += nums[i] - nums[i - k];
if (cnt.size() == k) {
ans = Math.max(ans, s);
}
Expand All @@ -114,18 +112,16 @@ public:
unordered_map<int, ll> cnt;
ll s = 0;
for (int i = 0; i < k; ++i) {
cnt[nums[i]]++;
++cnt[nums[i]];
s += nums[i];
}
ll ans = cnt.size() == k ? s : 0;
for (int i = k; i < n; ++i) {
cnt[nums[i]]++;
s += nums[i];
cnt[nums[i - k]]--;
s -= nums[i - k];
if (cnt[nums[i - k]] == 0) {
++cnt[nums[i]];
if (--cnt[nums[i - k]] == 0) {
cnt.erase(nums[i - k]);
}
s += nums[i] - nums[i - k];
if (cnt.size() == k) {
ans = max(ans, s);
}
Expand All @@ -149,12 +145,11 @@ func maximumSubarraySum(nums []int, k int) (ans int64) {
}
for i := k; i < n; i++ {
cnt[nums[i]]++
s += int64(nums[i])
cnt[nums[i-k]]--
s -= int64(nums[i-k])
if cnt[nums[i-k]] == 0 {
delete(cnt, nums[i-k])
}
s += int64(nums[i] - nums[i-k])
if len(cnt) == k && ans < s {
ans = s
}
Expand All @@ -175,12 +170,11 @@ function maximumSubarraySum(nums: number[], k: number): number {
let ans = cnt.size === k ? s : 0;
for (let i = k; i < n; ++i) {
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
s += nums[i];
cnt.set(nums[i - k], cnt.get(nums[i - k])! - 1);
s -= nums[i - k];
if (cnt.get(nums[i - k]) === 0) {
cnt.delete(nums[i - k]);
}
s += nums[i] - nums[i - k];
if (cnt.size === k) {
ans = Math.max(ans, s);
}
Expand All @@ -189,6 +183,48 @@ function maximumSubarraySum(nums: number[], k: number): number {
}
```

```cs
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;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ class Solution {
unordered_map<int, ll> cnt;
ll s = 0;
for (int i = 0; i < k; ++i) {
cnt[nums[i]]++;
++cnt[nums[i]];
s += nums[i];
}
ll ans = cnt.size() == k ? s : 0;
for (int i = k; i < n; ++i) {
cnt[nums[i]]++;
s += nums[i];
cnt[nums[i - k]]--;
s -= nums[i - k];
if (cnt[nums[i - k]] == 0) {
++cnt[nums[i]];
if (--cnt[nums[i - k]] == 0) {
cnt.erase(nums[i - k]);
}
s += nums[i] - nums[i - k];
if (cnt.size() == k) {
ans = max(ans, s);
}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ func maximumSubarraySum(nums []int, k int) (ans int64) {
}
for i := k; i < n; i++ {
cnt[nums[i]]++
s += int64(nums[i])
cnt[nums[i-k]]--
s -= int64(nums[i-k])
if cnt[nums[i-k]] == 0 {
delete(cnt, nums[i-k])
}
s += int64(nums[i] - nums[i-k])
if len(cnt) == k && ans < s {
ans = s
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ public long maximumSubarraySum(int[] nums, int k) {
long ans = cnt.size() == k ? s : 0;
for (int i = k; i < n; ++i) {
cnt.merge(nums[i], 1, Integer::sum);
s += nums[i];
if (cnt.merge(nums[i - k], -1, Integer::sum) == 0) {
cnt.remove(nums[i - k]);
}
s -= nums[i - k];
s += nums[i] - nums[i - k];
if (cnt.size() == k) {
ans = Math.max(ans, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ def maximumSubarraySum(self, nums: List[int], k: int) -> int:
ans = s if len(cnt) == k else 0
for i in range(k, len(nums)):
cnt[nums[i]] += 1
s += nums[i]
cnt[nums[i - k]] -= 1
s -= nums[i - k]
if cnt[nums[i - k]] == 0:
del cnt[nums[i - k]]
cnt.pop(nums[i - k])
s += nums[i] - nums[i - k]
if len(cnt) == k:
ans = max(ans, s)
return ans
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ function maximumSubarraySum(nums: number[], k: number): number {
let ans = cnt.size === k ? s : 0;
for (let i = k; i < n; ++i) {
cnt.set(nums[i], (cnt.get(nums[i]) ?? 0) + 1);
s += nums[i];
cnt.set(nums[i - k], cnt.get(nums[i - k])! - 1);
s -= nums[i - k];
if (cnt.get(nums[i - k]) === 0) {
cnt.delete(nums[i - k]);
}
s += nums[i] - nums[i - k];
if (cnt.size === k) {
ans = Math.max(ans, s);
}
Expand Down
Loading

0 comments on commit c517424

Please sign in to comment.