Skip to content

Commit 5211833

Browse files
authored
feat: add solutions to lc problems: No.2913,2914 (doocs#1904)
* No.2913.Subarrays Distinct Element Sum of Squares I * No.2914.Minimum Number of Changes to Make Binary String Beautiful
1 parent 03d8a15 commit 5211833

File tree

14 files changed

+393
-12
lines changed

14 files changed

+393
-12
lines changed

solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README.md

+85-3
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,116 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:枚举**
66+
67+
我们可以枚举子数组的左端点下标 $i$,对于每个 $i$,我们在 $[i, n)$ 的范围内枚举子数组的右端点下标 $j$,并统计 $nums[j]$ 的值,将其加入到集合 $s$ 中,记 $s$ 的大小为 $cnt$,那么 $nums[i..j]$ 的不同计数为 $cnt$,将其平方后加入到答案中。
68+
69+
枚举结束后,返回答案即可。
70+
71+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
72+
6573
<!-- tabs:start -->
6674

6775
### **Python3**
6876

6977
<!-- 这里可写当前语言的特殊实现逻辑 -->
7078

7179
```python
72-
80+
class Solution:
81+
def sumCounts(self, nums: List[int]) -> int:
82+
ans, n = 0, len(nums)
83+
for i in range(n):
84+
s = set()
85+
for j in range(i, n):
86+
s.add(nums[j])
87+
ans += len(s) * len(s)
88+
return ans
7389
```
7490

7591
### **Java**
7692

7793
<!-- 这里可写当前语言的特殊实现逻辑 -->
7894

7995
```java
80-
96+
class Solution {
97+
public int sumCounts(List<Integer> nums) {
98+
int ans = 0;
99+
int n = nums.size();
100+
for (int i = 0; i < n; ++i) {
101+
int[] s = new int[101];
102+
int cnt = 0;
103+
for (int j = i; j < n; ++j) {
104+
if (++s[nums.get(j)] == 1) {
105+
++cnt;
106+
}
107+
ans += cnt * cnt;
108+
}
109+
}
110+
return ans;
111+
}
112+
}
81113
```
82114

83115
### **C++**
84116

85117
```cpp
86-
118+
class Solution {
119+
public:
120+
int sumCounts(vector<int>& nums) {
121+
int ans = 0;
122+
int n = nums.size();
123+
for (int i = 0; i < n; ++i) {
124+
int s[101]{};
125+
int cnt = 0;
126+
for (int j = i; j < n; ++j) {
127+
if (++s[nums[j]] == 1) {
128+
++cnt;
129+
}
130+
ans += cnt * cnt;
131+
}
132+
}
133+
return ans;
134+
}
135+
};
87136
```
88137
89138
### **Go**
90139
91140
```go
141+
func sumCounts(nums []int) (ans int) {
142+
for i := range nums {
143+
s := [101]int{}
144+
cnt := 0
145+
for _, x := range nums[i:] {
146+
s[x]++
147+
if s[x] == 1 {
148+
cnt++
149+
}
150+
ans += cnt * cnt
151+
}
152+
}
153+
return
154+
}
155+
```
92156

157+
### **TypeScript**
158+
159+
```ts
160+
function sumCounts(nums: number[]): number {
161+
let ans = 0;
162+
const n = nums.length;
163+
for (let i = 0; i < n; ++i) {
164+
const s: number[] = Array(101).fill(0);
165+
let cnt = 0;
166+
for (const x of nums.slice(i)) {
167+
if (++s[x] === 1) {
168+
++cnt;
169+
}
170+
ans += cnt * cnt;
171+
}
172+
}
173+
return ans;
174+
}
93175
```
94176

95177
### **...**

solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README_EN.md

+85-3
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,112 @@ The sum of the squares of the distinct counts in all subarrays is equal to 1<sup
5353

5454
## Solutions
5555

56+
**Solution 1: Enumeration**
57+
58+
We can enumerate the left endpoint index $i$ of the subarray, and for each $i$, we enumerate the right endpoint index $j$ in the range $[i, n)$, and calculate the distinct count of $nums[i..j]$ by adding the count of $nums[j]$ to a set $s$, and then taking the square of the size of $s$ as the contribution of $nums[i..j]$ to the answer.
59+
60+
After the enumeration, we return the answer.
61+
62+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
5967

6068
```python
61-
69+
class Solution:
70+
def sumCounts(self, nums: List[int]) -> int:
71+
ans, n = 0, len(nums)
72+
for i in range(n):
73+
s = set()
74+
for j in range(i, n):
75+
s.add(nums[j])
76+
ans += len(s) * len(s)
77+
return ans
6278
```
6379

6480
### **Java**
6581

6682
```java
67-
83+
class Solution {
84+
public int sumCounts(List<Integer> nums) {
85+
int ans = 0;
86+
int n = nums.size();
87+
for (int i = 0; i < n; ++i) {
88+
int[] s = new int[101];
89+
int cnt = 0;
90+
for (int j = i; j < n; ++j) {
91+
if (++s[nums.get(j)] == 1) {
92+
++cnt;
93+
}
94+
ans += cnt * cnt;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
68100
```
69101

70102
### **C++**
71103

72104
```cpp
73-
105+
class Solution {
106+
public:
107+
int sumCounts(vector<int>& nums) {
108+
int ans = 0;
109+
int n = nums.size();
110+
for (int i = 0; i < n; ++i) {
111+
int s[101]{};
112+
int cnt = 0;
113+
for (int j = i; j < n; ++j) {
114+
if (++s[nums[j]] == 1) {
115+
++cnt;
116+
}
117+
ans += cnt * cnt;
118+
}
119+
}
120+
return ans;
121+
}
122+
};
74123
```
75124
76125
### **Go**
77126
78127
```go
128+
func sumCounts(nums []int) (ans int) {
129+
for i := range nums {
130+
s := [101]int{}
131+
cnt := 0
132+
for _, x := range nums[i:] {
133+
s[x]++
134+
if s[x] == 1 {
135+
cnt++
136+
}
137+
ans += cnt * cnt
138+
}
139+
}
140+
return
141+
}
142+
```
79143

144+
### **TypeScript**
145+
146+
```ts
147+
function sumCounts(nums: number[]): number {
148+
let ans = 0;
149+
const n = nums.length;
150+
for (let i = 0; i < n; ++i) {
151+
const s: number[] = Array(101).fill(0);
152+
let cnt = 0;
153+
for (const x of nums.slice(i)) {
154+
if (++s[x] === 1) {
155+
++cnt;
156+
}
157+
ans += cnt * cnt;
158+
}
159+
}
160+
return ans;
161+
}
80162
```
81163

82164
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int sumCounts(vector<int>& nums) {
4+
int ans = 0;
5+
int n = nums.size();
6+
for (int i = 0; i < n; ++i) {
7+
int s[101]{};
8+
int cnt = 0;
9+
for (int j = i; j < n; ++j) {
10+
if (++s[nums[j]] == 1) {
11+
++cnt;
12+
}
13+
ans += cnt * cnt;
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func sumCounts(nums []int) (ans int) {
2+
for i := range nums {
3+
s := [101]int{}
4+
cnt := 0
5+
for _, x := range nums[i:] {
6+
s[x]++
7+
if s[x] == 1 {
8+
cnt++
9+
}
10+
ans += cnt * cnt
11+
}
12+
}
13+
return
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int sumCounts(List<Integer> nums) {
3+
int ans = 0;
4+
int n = nums.size();
5+
for (int i = 0; i < n; ++i) {
6+
int[] s = new int[101];
7+
int cnt = 0;
8+
for (int j = i; j < n; ++j) {
9+
if (++s[nums.get(j)] == 1) {
10+
++cnt;
11+
}
12+
ans += cnt * cnt;
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def sumCounts(self, nums: List[int]) -> int:
3+
ans, n = 0, len(nums)
4+
for i in range(n):
5+
s = set()
6+
for j in range(i, n):
7+
s.add(nums[j])
8+
ans += len(s) * len(s)
9+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function sumCounts(nums: number[]): number {
2+
let ans = 0;
3+
const n = nums.length;
4+
for (let i = 0; i < n; ++i) {
5+
const s: number[] = Array(101).fill(0);
6+
let cnt = 0;
7+
for (const x of nums.slice(i)) {
8+
if (++s[x] === 1) {
9+
++cnt;
10+
}
11+
ans += cnt * cnt;
12+
}
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)