Skip to content

Commit d831859

Browse files
authored
feat: add Rust solutions for lc No.2833,2834 (#5164)
1 parent 919767e commit d831859

6 files changed

Lines changed: 89 additions & 3 deletions

File tree

solution/2800-2899/2833.Furthest Point From Origin/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ function furthestDistanceFromOrigin(moves: string): number {
148148
}
149149
```
150150

151+
#### Rust
152+
153+
```rust
154+
impl Solution {
155+
pub fn furthest_distance_from_origin(moves: String) -> i32 {
156+
let l = moves.chars().filter(|&c| c == 'L').count() as i32;
157+
let r = moves.chars().filter(|&c| c == 'R').count() as i32;
158+
let blank = moves.chars().filter(|&c| c == '_').count() as i32;
159+
(l - r).abs() + blank
160+
}
161+
}
162+
```
163+
151164
<!-- tabs:end -->
152165

153166
<!-- solution:end -->

solution/2800-2899/2833.Furthest Point From Origin/README_EN.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ function furthestDistanceFromOrigin(moves: string): number {
146146
}
147147
```
148148

149+
#### Rust
150+
151+
```rust
152+
impl Solution {
153+
pub fn furthest_distance_from_origin(moves: String) -> i32 {
154+
let l = moves.chars().filter(|&c| c == 'L').count() as i32;
155+
let r = moves.chars().filter(|&c| c == 'R').count() as i32;
156+
let blank = moves.chars().filter(|&c| c == '_').count() as i32;
157+
(l - r).abs() + blank
158+
}
159+
}
160+
```
161+
149162
<!-- tabs:end -->
150163

151164
<!-- solution:end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl Solution {
2+
pub fn furthest_distance_from_origin(moves: String) -> i32 {
3+
let l = moves.chars().filter(|&c| c == 'L').count() as i32;
4+
let r = moves.chars().filter(|&c| c == 'R').count() as i32;
5+
let blank = moves.chars().filter(|&c| c == '_').count() as i32;
6+
(l - r).abs() + blank
7+
}
8+
}

solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ tags:
5050
<strong>输入:</strong>n = 3, target = 3
5151
<strong>输出:</strong>8
5252
<strong>解释:</strong>
53-
nums = [1,3,4] 是美丽数组。
54-
- nums 的长度为 n = 3 。
55-
- nums 由两两互不相同的正整数组成。
53+
nums = [1,3,4] 是美丽数组。
54+
- nums 的长度为 n = 3 。
55+
- nums 由两两互不相同的正整数组成。
5656
- 不存在两个不同下标 i 和 j ,使得 nums[i] + nums[j] == 3 。
5757
可以证明 8 是符合条件的美丽数组所可能具备的最小和。</pre>
5858

@@ -170,6 +170,25 @@ function minimumPossibleSum(n: number, target: number): number {
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn minimum_possible_sum(n: i32, target: i32) -> i32 {
178+
const MOD: i64 = 1_000_000_007;
179+
let n = n as i64;
180+
let target = target as i64;
181+
let m = target / 2;
182+
if n <= m {
183+
return ((1 + n) * n / 2 % MOD) as i32;
184+
}
185+
let a = (1 + m) * m / 2 % MOD;
186+
let b = (target + target + n - m - 1) * (n - m) / 2 % MOD;
187+
((a + b) % MOD) as i32
188+
}
189+
}
190+
```
191+
173192
#### C#
174193

175194
```cs

solution/2800-2899/2834.Find the Minimum Possible Sum of a Beautiful Array/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ function minimumPossibleSum(n: number, target: number): number {
169169
}
170170
```
171171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn minimum_possible_sum(n: i32, target: i32) -> i32 {
177+
const MOD: i64 = 1_000_000_007;
178+
let n = n as i64;
179+
let target = target as i64;
180+
let m = target / 2;
181+
if n <= m {
182+
return ((1 + n) * n / 2 % MOD) as i32;
183+
}
184+
let a = (1 + m) * m / 2 % MOD;
185+
let b = (target + target + n - m - 1) * (n - m) / 2 % MOD;
186+
((a + b) % MOD) as i32
187+
}
188+
}
189+
```
190+
172191
#### C#
173192

174193
```cs
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn minimum_possible_sum(n: i32, target: i32) -> i32 {
3+
const MOD: i64 = 1_000_000_007;
4+
let n = n as i64;
5+
let target = target as i64;
6+
let m = target / 2;
7+
if n <= m {
8+
return ((1 + n) * n / 2 % MOD) as i32;
9+
}
10+
let a = (1 + m) * m / 2 % MOD;
11+
let b = (target + target + n - m - 1) * (n - m) / 2 % MOD;
12+
((a + b) % MOD) as i32
13+
}
14+
}

0 commit comments

Comments
 (0)