diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README.md b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README.md index 74df06f8ac071..809f86da4d805 100644 --- a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README.md +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README.md @@ -62,6 +62,14 @@ +**方法一:枚举** + +我们可以枚举子数组的左端点下标 $i$,对于每个 $i$,我们在 $[i, n)$ 的范围内枚举子数组的右端点下标 $j$,并统计 $nums[j]$ 的值,将其加入到集合 $s$ 中,记 $s$ 的大小为 $cnt$,那么 $nums[i..j]$ 的不同计数为 $cnt$,将其平方后加入到答案中。 + +枚举结束后,返回答案即可。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。 + ### **Python3** @@ -69,7 +77,15 @@ ```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** @@ -77,19 +93,85 @@ ```java - +class Solution { + public int sumCounts(List 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& 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; +} ``` ### **...** diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README_EN.md b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README_EN.md index db319da3a4143..6d35a45f57e73 100644 --- a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README_EN.md +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/README_EN.md @@ -53,30 +53,112 @@ The sum of the squares of the distinct counts in all subarrays is equal to 1 ### **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 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& 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; +} ``` ### **...** diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.cpp b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.cpp new file mode 100644 index 0000000000000..5e59e40435c04 --- /dev/null +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int sumCounts(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go new file mode 100644 index 0000000000000..263dc63058cc8 --- /dev/null +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.java b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.java new file mode 100644 index 0000000000000..c2b72d1199bfe --- /dev/null +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int sumCounts(List 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; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.py b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.py new file mode 100644 index 0000000000000..9c8720ab45025 --- /dev/null +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.py @@ -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 diff --git a/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.ts b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.ts new file mode 100644 index 0000000000000..98d79c8832372 --- /dev/null +++ b/solution/2900-2999/2913.Subarrays Distinct Element Sum of Squares I/Solution.ts @@ -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; +} diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README.md b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README.md index 3e456c39dcfca..7d17255da6c08 100644 --- a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README.md +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README.md @@ -63,6 +63,14 @@ +**方法一:计数** + +我们只需要遍历字符串 $s$ 的所有奇数下标 $1, 3, 5, \cdots$,如果当前奇数下标与前一个下标的字符不同,即 $s[i] \ne s[i - 1]$,那么就需要修改当前字符,使得 $s[i] = s[i - 1]$。因此,此时答案需要加 $1$。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -70,7 +78,9 @@ ```python - +class Solution: + def minChanges(self, s: str) -> int: + return sum(s[i] != s[i - 1] for i in range(1, len(s), 2)) ``` ### **Java** @@ -78,19 +88,60 @@ ```java - +class Solution { + public int minChanges(String s) { + int ans = 0; + for (int i = 1; i < s.length(); i += 2) { + if (s.charAt(i) != s.charAt(i - 1)) { + ++ans; + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minChanges(string s) { + int ans = 0; + int n = s.size(); + for (int i = 1; i < n; i += 2) { + ans += s[i] != s[i - 1]; + } + return ans; + } +}; ``` ### **Go** ```go +func minChanges(s string) (ans int) { + for i := 1; i < len(s); i += 2 { + if s[i] != s[i-1] { + ans++ + } + } + return +} +``` +### **TypeScript** + +```ts +function minChanges(s: string): number { + let ans = 0; + for (let i = 1; i < s.length; i += 2) { + if (s[i] !== s[i - 1]) { + ++ans; + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README_EN.md b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README_EN.md index 85a086dc7af46..1e3af94b77977 100644 --- a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README_EN.md +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/README_EN.md @@ -57,30 +57,81 @@ It can be proven that 1 is the minimum number of changes needed to make the stri ## Solutions +**Solution 1: Counting** + +We only need to traverse all odd indices $1, 3, 5, \cdots$ of the string $s$. If the current odd index is different from the previous index, i.e., $s[i] \ne s[i - 1]$, we need to modify the current character so that $s[i] = s[i - 1]$. Therefore, the answer needs to be incremented by $1$. + +After the traversal, we return the answer. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def minChanges(self, s: str) -> int: + return sum(s[i] != s[i - 1] for i in range(1, len(s), 2)) ``` ### **Java** ```java - +class Solution { + public int minChanges(String s) { + int ans = 0; + for (int i = 1; i < s.length(); i += 2) { + if (s.charAt(i) != s.charAt(i - 1)) { + ++ans; + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minChanges(string s) { + int ans = 0; + int n = s.size(); + for (int i = 1; i < n; i += 2) { + ans += s[i] != s[i - 1]; + } + return ans; + } +}; ``` ### **Go** ```go +func minChanges(s string) (ans int) { + for i := 1; i < len(s); i += 2 { + if s[i] != s[i-1] { + ans++ + } + } + return +} +``` +### **TypeScript** + +```ts +function minChanges(s: string): number { + let ans = 0; + for (let i = 1; i < s.length; i += 2) { + if (s[i] !== s[i - 1]) { + ++ans; + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.cpp b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.cpp new file mode 100644 index 0000000000000..2e28922e7c939 --- /dev/null +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int minChanges(string s) { + int ans = 0; + int n = s.size(); + for (int i = 1; i < n; i += 2) { + ans += s[i] != s[i - 1]; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go new file mode 100644 index 0000000000000..2e5eff143b5fb --- /dev/null +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.go @@ -0,0 +1,8 @@ +func minChanges(s string) (ans int) { + for i := 1; i < len(s); i += 2 { + if s[i] != s[i-1] { + ans++ + } + } + return +} \ No newline at end of file diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.java b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.java new file mode 100644 index 0000000000000..3064ac3a0b425 --- /dev/null +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public int minChanges(String s) { + int ans = 0; + for (int i = 1; i < s.length(); i += 2) { + if (s.charAt(i) != s.charAt(i - 1)) { + ++ans; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.py b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.py new file mode 100644 index 0000000000000..cbf348a9a4cf2 --- /dev/null +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def minChanges(self, s: str) -> int: + return sum(s[i] != s[i - 1] for i in range(1, len(s), 2)) diff --git a/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.ts b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.ts new file mode 100644 index 0000000000000..5d76149314451 --- /dev/null +++ b/solution/2900-2999/2914.Minimum Number of Changes to Make Binary String Beautiful/Solution.ts @@ -0,0 +1,9 @@ +function minChanges(s: string): number { + let ans = 0; + for (let i = 1; i < s.length; i += 2) { + if (s[i] !== s[i - 1]) { + ++ans; + } + } + return ans; +}