Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.2913,2914
Browse files Browse the repository at this point in the history
* No.2913.Subarrays Distinct Element Sum of Squares I
* No.2914.Minimum Number of Changes to Make Binary String Beautiful
  • Loading branch information
yanglbme committed Oct 30, 2023
1 parent 932ae80 commit eeb1157
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,116 @@

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

**方法一:枚举**

我们可以枚举子数组的左端点下标 $i$,对于每个 $i$,我们在 $[i, n)$ 的范围内枚举子数组的右端点下标 $j$,并统计 $nums[j]$ 的值,将其加入到集合 $s$ 中,记 $s$ 的大小为 $cnt$,那么 $nums[i..j]$ 的不同计数为 $cnt$,将其平方后加入到答案中。

枚举结束后,返回答案即可。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

### **Python3**

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

```python

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
```

### **Java**

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

```java

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

### **C++**

```cpp

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;
}
};
```
### **Go**
```go
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
}
```

### **TypeScript**

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

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,112 @@ The sum of the squares of the distinct counts in all subarrays is equal to 1<sup

## Solutions

**Solution 1: Enumeration**

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.

After the enumeration, we return the answer.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**

```python

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
```

### **Java**

```java

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

### **C++**

```cpp

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;
}
};
```
### **Go**
```go
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
}
```

### **TypeScript**

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

### **...**
Expand Down
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;
}
};
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
}
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;
}
}
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
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;
}
Loading

0 comments on commit eeb1157

Please sign in to comment.