Skip to content

[pull] main from doocs:main #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ function minimumCost(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut ans = 0;
let n = s.len();
let s = s.as_bytes();
for i in 1..n {
if s[i] != s[i - 1] {
ans += i.min(n - i);
}
}
ans as i64
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ function minimumCost(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut ans = 0;
let n = s.len();
let s = s.as_bytes();
for i in 1..n {
if s[i] != s[i - 1] {
ans += i.min(n - i);
}
}
ans as i64
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn minimum_cost(s: String) -> i64 {
let mut ans = 0;
let n = s.len();
let s = s.as_bytes();
for i in 1..n {
if s[i] != s[i - 1] {
ans += i.min(n - i);
}
}
ans as i64
}
}
15 changes: 12 additions & 3 deletions solution/2700-2799/2716.Minimize String Length/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ tags:

题目实际上可以转化为求字符串中不同字符的个数,因此,我们只需要统计字符串中不同字符的个数即可。

时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串的长度;而 $C$ 是字符集的大小,本题中字符集为小写英文字母,因此 $C=26$。
时间复杂度 $O(n)$,其中 $n$ 是字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,这里是小写英文字母,因此 $|\Sigma|=26$。

<!-- tabs:start -->

Expand Down Expand Up @@ -104,8 +104,7 @@ class Solution {
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> ss(s.begin(), s.end());
return ss.size();
return unordered_set<char>(s.begin(), s.end()).size();
}
};
```
Expand Down Expand Up @@ -143,6 +142,16 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MinimizedStringLength(string s) {
return new HashSet<char>(s).Count;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
17 changes: 13 additions & 4 deletions solution/2700-2799/2716.Minimize String Length/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ tags:

### Solution 1: Hash Table

The problem can actually be transformed into finding the number of different characters in the string. Therefore, we only need to count the number of different characters in the string.
The problem can actually be transformed into finding the number of distinct characters in the string. Therefore, we only need to count the number of distinct characters in the string.

The time complexity is $O(n)$, 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, the character set is lowercase English letters, so $C=26$.
The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. In this case, it's lowercase English letters, so $|\Sigma|=26$.

<!-- tabs:start -->

Expand Down Expand Up @@ -134,8 +134,7 @@ class Solution {
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> ss(s.begin(), s.end());
return ss.size();
return unordered_set<char>(s.begin(), s.end()).size();
}
};
```
Expand Down Expand Up @@ -173,6 +172,16 @@ impl Solution {
}
```

#### C#

```cs
public class Solution {
public int MinimizedStringLength(string s) {
return new HashSet<char>(s).Count;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
5 changes: 2 additions & 3 deletions solution/2700-2799/2716.Minimize String Length/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Solution {
public:
int minimizedStringLength(string s) {
unordered_set<char> ss(s.begin(), s.end());
return ss.size();
return unordered_set<char>(s.begin(), s.end()).size();
}
};
};
5 changes: 5 additions & 0 deletions solution/2700-2799/2716.Minimize String Length/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Solution {
public int MinimizedStringLength(string s) {
return new HashSet<char>(s).Count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<strong>输入:</strong>nums = [1,10,3,4,19]
<strong>输出:</strong>133
<strong>解释:</strong>下标三元组 (1, 2, 4) 的值是 (nums[1] - nums[2]) * nums[4] = 133 。
可以证明不存在值大于 133 的有序下标三元组。
可以证明不存在值大于 133 的有序下标三元组。
</pre>

<p><strong class="example">示例 3:</strong></p>
Expand Down Expand Up @@ -69,7 +69,11 @@ tags:

### 方法一:维护前缀最大值和最大差值

我们可以用两个变量 $mx$ 和 $mx\_diff$ 分别维护前缀最大值和最大差值。遍历数组时,更新这两个变量,答案为所有 $mx\_diff \times nums[i]$ 的最大值。
我们用两个变量 $\textit{mx}$ 和 $\textit{mxDiff}$ 分别维护前缀最大值和最大差值,用一个变量 $\textit{ans}$ 维护答案。初始时,这些变量都为 $0$。

接下来,我们枚举数组的每个元素 $x$ 作为 $\textit{nums}[k]$,首先更新答案 $\textit{ans} = \max(\textit{ans}, \textit{mxDiff} \times x)$,然后我们更新最大差值 $\textit{mxDiff} = \max(\textit{mxDiff}, \textit{mx} - x)$,最后更新前缀最大值 $\textit{mx} = \max(\textit{mx}, x)$。

枚举完所有元素后,返回答案 $\textit{ans}$。

时间复杂度 $O(n)$,其中 $n$ 是数组长度。空间复杂度 $O(1)$。

Expand All @@ -81,10 +85,10 @@ tags:
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
ans = mx = mx_diff = 0
for num in nums:
ans = max(ans, mx_diff * num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx - num)
for x in nums:
ans = max(ans, mx_diff * x)
mx_diff = max(mx_diff, mx - x)
mx = max(mx, x)
return ans
```

Expand All @@ -93,14 +97,12 @@ class Solution:
```java
class Solution {
public long maximumTripletValue(int[] nums) {
long max, maxDiff, ans;
max = 0;
maxDiff = 0;
ans = 0;
for (int num : nums) {
ans = Math.max(ans, num * maxDiff);
max = Math.max(max, num);
maxDiff = Math.max(maxDiff, max - num);
long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
Expand All @@ -113,12 +115,12 @@ class Solution {
class Solution {
public:
long long maximumTripletValue(vector<int>& nums) {
long long ans = 0;
int mx = 0, mx_diff = 0;
for (int num : nums) {
ans = max(ans, 1LL * mx_diff * num);
mx = max(mx, num);
mx_diff = max(mx_diff, mx - num);
long long ans = 0, mxDiff = 0;
int mx = 0;
for (int x : nums) {
ans = max(ans, mxDiff * x);
mxDiff = max(mxDiff, 1LL * mx - x);
mx = max(mx, x);
}
return ans;
}
Expand All @@ -129,11 +131,11 @@ public:

```go
func maximumTripletValue(nums []int) int64 {
ans, mx, mx_diff := 0, 0, 0
for _, num := range nums {
ans = max(ans, mx_diff*num)
mx = max(mx, num)
mx_diff = max(mx_diff, mx-num)
ans, mx, mxDiff := 0, 0, 0
for _, x := range nums {
ans = max(ans, mxDiff*x)
mxDiff = max(mxDiff, mx-x)
mx = max(mx, x)
}
return int64(ans)
}
Expand All @@ -143,16 +145,36 @@ func maximumTripletValue(nums []int) int64 {

```ts
function maximumTripletValue(nums: number[]): number {
let [ans, mx, mx_diff] = [0, 0, 0];
for (const num of nums) {
ans = Math.max(ans, mx_diff * num);
mx = Math.max(mx, num);
mx_diff = Math.max(mx_diff, mx - num);
let [ans, mx, mxDiff] = [0, 0, 0];
for (const x of nums) {
ans = Math.max(ans, mxDiff * x);
mxDiff = Math.max(mxDiff, mx - x);
mx = Math.max(mx, x);
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
let mut ans: i64 = 0;
let mut mx: i32 = 0;
let mut mx_diff: i32 = 0;

for &x in &nums {
ans = ans.max(mx_diff as i64 * x as i64);
mx_diff = mx_diff.max(mx - x);
mx = mx.max(x);
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading