diff --git a/solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md b/solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md index 77e194d77ee6c..c0257d3ab4de6 100644 --- a/solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md +++ b/solution/1700-1799/1781.Sum of Beauty of All Substrings/README.md @@ -67,6 +67,30 @@ class Solution: return ans ``` +```python +class Solution: + def beautySum(self, s: str) -> int: + ans, n = 0, len(s) + for i in range(n): + cnt = Counter() + freq = Counter() + mi = mx = 1 + for j in range(i, n): + freq[cnt[s[j]]] -= 1 + cnt[s[j]] += 1 + freq[cnt[s[j]]] += 1 + + if cnt[s[j]] == 1: + mi = 1 + if freq[mi] == 0: + mi += 1 + if cnt[s[j]] > mx: + mx = cnt[s[j]] + + ans += mx - mi + return ans +``` + ### **Java** @@ -95,6 +119,38 @@ class Solution { } ``` +```java +class Solution { + public int beautySum(String s) { + int n = s.length(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int[] cnt = new int[26]; + Map freq = new HashMap<>(); + int mi = 1, mx = 1; + for (int j = i; j < n; ++j) { + int k = s.charAt(j) - 'a'; + freq.merge(cnt[k], -1, Integer::sum); + ++cnt[k]; + freq.merge(cnt[k], 1, Integer::sum); + + if (cnt[k] == 1) { + mi = 1; + } + if (freq.getOrDefault(mi, 0) == 0) { + ++mi; + } + if (cnt[k] > mx) { + mx = cnt[k]; + } + ans += mx - mi; + } + } + return ans; + } +} +``` + ### **C++** ```cpp @@ -123,6 +179,39 @@ public: }; ``` +```cpp +class Solution { +public: + int beautySum(string s) { + int n = s.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int cnt[26]{}; + unordered_map freq; + int mi = 1, mx = 1; + for (int j = i; j < n; ++j) { + int k = s[j] - 'a'; + --freq[cnt[k]]; + ++cnt[k]; + ++freq[cnt[k]]; + + if (cnt[k] == 1) { + mi = 1; + } + if (freq[mi] == 0) { + ++mi; + } + if (cnt[k] > mx) { + mx = cnt[k]; + } + ans += mx - mi; + } + } + return ans; + } +}; +``` + ### **Go** ```go @@ -149,6 +238,35 @@ func beautySum(s string) (ans int) { } ``` +```go +func beautySum(s string) (ans int) { + n := len(s) + for i := 0; i < n; i++ { + cnt := [26]int{} + freq := map[int]int{} + mi, mx := 1, 1 + for j := i; j < n; j++ { + k := int(s[j] - 'a') + freq[cnt[k]]-- + cnt[k]++ + freq[cnt[k]]++ + + if cnt[k] == 1 { + mi = 1 + } + if freq[mi] == 0 { + mi++ + } + if cnt[k] > mx { + mx = cnt[k] + } + ans += mx - mi + } + } + return +} +``` + ### **JavaScript** ```js @@ -170,6 +288,39 @@ var beautySum = function (s) { }; ``` +```js +/** + * @param {string} s + * @return {number} + */ +var beautySum = function (s) { + const n = s.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const cnt = Array(26).fill(0); + const freq = new Map(); + let [mi, mx] = [1, 1]; + for (let j = i; j < n; ++j) { + const k = s[j].charCodeAt() - 97; + freq.set(cnt[k], (freq.get(cnt[k]) || 0) - 1); + ++cnt[k]; + freq.set(cnt[k], (freq.get(cnt[k]) || 0) + 1); + if (cnt[k] === 1) { + mi = 1; + } + if (freq.get(mi) === 0) { + ++mi; + } + if (cnt[k] > mx) { + mx = cnt[k]; + } + ans += mx - mi; + } + } + return ans; +}; +``` + ### **...** ``` diff --git a/solution/1700-1799/1781.Sum of Beauty of All Substrings/README_EN.md b/solution/1700-1799/1781.Sum of Beauty of All Substrings/README_EN.md index b08ad5d5d76d6..307f99dcf51c4 100644 --- a/solution/1700-1799/1781.Sum of Beauty of All Substrings/README_EN.md +++ b/solution/1700-1799/1781.Sum of Beauty of All Substrings/README_EN.md @@ -37,6 +37,12 @@ ## Solutions +**Solution 1: Enumeration + Counting** + +Enumerate the starting position $i$ of each substring, find all substrings with the character at this starting position as the left endpoint, then calculate the beauty value of each substring, and accumulate it to the answer. + +The time complexity is $O(n^2 \times C)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string, and $C$ is the size of the character set. In this problem, $C = 26$. + ### **Python3** @@ -53,6 +59,30 @@ class Solution: return ans ``` +```python +class Solution: + def beautySum(self, s: str) -> int: + ans, n = 0, len(s) + for i in range(n): + cnt = Counter() + freq = Counter() + mi = mx = 1 + for j in range(i, n): + freq[cnt[s[j]]] -= 1 + cnt[s[j]] += 1 + freq[cnt[s[j]]] += 1 + + if cnt[s[j]] == 1: + mi = 1 + if freq[mi] == 0: + mi += 1 + if cnt[s[j]] > mx: + mx = cnt[s[j]] + + ans += mx - mi + return ans +``` + ### **Java** ```java @@ -79,6 +109,38 @@ class Solution { } ``` +```java +class Solution { + public int beautySum(String s) { + int n = s.length(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int[] cnt = new int[26]; + Map freq = new HashMap<>(); + int mi = 1, mx = 1; + for (int j = i; j < n; ++j) { + int k = s.charAt(j) - 'a'; + freq.merge(cnt[k], -1, Integer::sum); + ++cnt[k]; + freq.merge(cnt[k], 1, Integer::sum); + + if (cnt[k] == 1) { + mi = 1; + } + if (freq.getOrDefault(mi, 0) == 0) { + ++mi; + } + if (cnt[k] > mx) { + mx = cnt[k]; + } + ans += mx - mi; + } + } + return ans; + } +} +``` + ### **C++** ```cpp @@ -107,6 +169,39 @@ public: }; ``` +```cpp +class Solution { +public: + int beautySum(string s) { + int n = s.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + int cnt[26]{}; + unordered_map freq; + int mi = 1, mx = 1; + for (int j = i; j < n; ++j) { + int k = s[j] - 'a'; + --freq[cnt[k]]; + ++cnt[k]; + ++freq[cnt[k]]; + + if (cnt[k] == 1) { + mi = 1; + } + if (freq[mi] == 0) { + ++mi; + } + if (cnt[k] > mx) { + mx = cnt[k]; + } + ans += mx - mi; + } + } + return ans; + } +}; +``` + ### **Go** ```go @@ -133,6 +228,35 @@ func beautySum(s string) (ans int) { } ``` +```go +func beautySum(s string) (ans int) { + n := len(s) + for i := 0; i < n; i++ { + cnt := [26]int{} + freq := map[int]int{} + mi, mx := 1, 1 + for j := i; j < n; j++ { + k := int(s[j] - 'a') + freq[cnt[k]]-- + cnt[k]++ + freq[cnt[k]]++ + + if cnt[k] == 1 { + mi = 1 + } + if freq[mi] == 0 { + mi++ + } + if cnt[k] > mx { + mx = cnt[k] + } + ans += mx - mi + } + } + return +} +``` + ### **JavaScript** ```js @@ -154,6 +278,39 @@ var beautySum = function (s) { }; ``` +```js +/** + * @param {string} s + * @return {number} + */ +var beautySum = function (s) { + const n = s.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const cnt = Array(26).fill(0); + const freq = new Map(); + let [mi, mx] = [1, 1]; + for (let j = i; j < n; ++j) { + const k = s[j].charCodeAt() - 97; + freq.set(cnt[k], (freq.get(cnt[k]) || 0) - 1); + ++cnt[k]; + freq.set(cnt[k], (freq.get(cnt[k]) || 0) + 1); + if (cnt[k] === 1) { + mi = 1; + } + if (freq.get(mi) === 0) { + ++mi; + } + if (cnt[k] > mx) { + mx = cnt[k]; + } + ans += mx - mi; + } + } + return ans; +}; +``` + ### **...** ``` diff --git a/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md b/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md index 600d2a40e84ae..59d740d71cee6 100644 --- a/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md +++ b/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README.md @@ -50,7 +50,7 @@ 因此,只需要判断字符串 $s$ 是否存在 "01" 串即可。 -时间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README_EN.md b/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README_EN.md index 1cd1410af7df5..a57363a24050c 100644 --- a/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README_EN.md +++ b/solution/1700-1799/1784.Check if Binary String Has at Most One Segment of Ones/README_EN.md @@ -32,6 +32,18 @@ ## Solutions +**Solution 1: No '1' After '0'** + +Notice that the string $s$ does not contain leading zeros, which means $s$ starts with '1'. + +If the string $s$ contains the substring "01", then $s$ must be a string like "1...01...", in which case $s$ has at least two consecutive '1' segments, which does not satisfy the problem condition, so we return `false`. + +If the string $s$ does not contain the substring "01", then $s$ can only be a string like "1..1000...", in which case $s$ has only one consecutive '1' segment, which satisfies the problem condition, so we return `true`. + +Therefore, we only need to judge whether the string $s$ contains the substring "01". + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. + ### **Python3** diff --git a/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README.md b/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README.md index da8796f0cca4f..ef10269b14063 100644 --- a/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README.md +++ b/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README.md @@ -52,7 +52,7 @@ 注意,本题中数组元素的数据范围为 $[-10^6, 10^6]$,元素个数最大为 $10^5$,总和 $s$ 以及差值 $d$ 可能会超过 $32$ 位整数的表示范围,因此需要使用 $64$ 位整数。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。 diff --git a/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README_EN.md b/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README_EN.md index 5fd46f899cf5d..b396d4e1d7667 100644 --- a/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README_EN.md +++ b/solution/1700-1799/1785.Minimum Elements to Add to Form a Given Sum/README_EN.md @@ -38,6 +38,16 @@ ## Solutions +**Solution 1: Greedy** + +First, we calculate the sum of the array elements $s$, and then calculate the difference $d$ between $s$ and $goal$. + +The number of elements to be added is the absolute value of $d$ divided by $limit$ and rounded up, that is, $\lceil \frac{|d|}{limit} \rceil$. + +Note that in this problem, the data range of array elements is $[-10^6, 10^6]$, the maximum number of elements is $10^5$, the total sum $s$ and the difference $d$ may exceed the range of 32-bit integers, so we need to use 64-bit integers. + +The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$. + ### **Python3**