-
-
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.2913,2914 (#1904)
* No.2913.Subarrays Distinct Element Sum of Squares I * No.2914.Minimum Number of Changes to Make Binary String Beautiful
- Loading branch information
Showing
14 changed files
with
393 additions
and
12 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
18 changes: 18 additions & 0 deletions
18
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.cpp
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,18 @@ | ||
class Solution { | ||
public: | ||
int sumCounts(vector<int>& nums) { | ||
int ans = 0; | ||
int n = nums.size(); | ||
for (int i = 0; i < n; ++i) { | ||
int s[101]{}; | ||
int cnt = 0; | ||
for (int j = i; j < n; ++j) { | ||
if (++s[nums[j]] == 1) { | ||
++cnt; | ||
} | ||
ans += cnt * cnt; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go
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,14 @@ | ||
func sumCounts(nums []int) (ans int) { | ||
for i := range nums { | ||
s := [101]int{} | ||
cnt := 0 | ||
for _, x := range nums[i:] { | ||
s[x]++ | ||
if s[x] == 1 { | ||
cnt++ | ||
} | ||
ans += cnt * cnt | ||
} | ||
} | ||
return | ||
} |
17 changes: 17 additions & 0 deletions
17
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.java
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,17 @@ | ||
class Solution { | ||
public int sumCounts(List<Integer> nums) { | ||
int ans = 0; | ||
int n = nums.size(); | ||
for (int i = 0; i < n; ++i) { | ||
int[] s = new int[101]; | ||
int cnt = 0; | ||
for (int j = i; j < n; ++j) { | ||
if (++s[nums.get(j)] == 1) { | ||
++cnt; | ||
} | ||
ans += cnt * cnt; | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.py
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,9 @@ | ||
class Solution: | ||
def sumCounts(self, nums: List[int]) -> int: | ||
ans, n = 0, len(nums) | ||
for i in range(n): | ||
s = set() | ||
for j in range(i, n): | ||
s.add(nums[j]) | ||
ans += len(s) * len(s) | ||
return ans |
15 changes: 15 additions & 0 deletions
15
solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.ts
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,15 @@ | ||
function sumCounts(nums: number[]): number { | ||
let ans = 0; | ||
const n = nums.length; | ||
for (let i = 0; i < n; ++i) { | ||
const s: number[] = Array(101).fill(0); | ||
let cnt = 0; | ||
for (const x of nums.slice(i)) { | ||
if (++s[x] === 1) { | ||
++cnt; | ||
} | ||
ans += cnt * cnt; | ||
} | ||
} | ||
return ans; | ||
} |
Oops, something went wrong.