Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6823f28

Browse files
feat: add rust solution to lc problem: No.2300 (#4377)
No.2300.Successful Pairs of Spells and Potions
1 parent 303a9cc commit 6823f28

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ function successfulPairs(spells: number[], potions: number[], success: number):
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn successful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
178+
potions.sort();
179+
let m = potions.len();
180+
181+
spells.into_iter().map(|v| {
182+
let i = potions.binary_search_by(|&p| {
183+
let prod = (p as i64) * (v as i64);
184+
if prod >= success {
185+
std::cmp::Ordering::Greater
186+
} else {
187+
std::cmp::Ordering::Less
188+
}
189+
}).unwrap_or_else(|x| x);
190+
(m - i) as i32
191+
}).collect()
192+
}
193+
}
194+
```
195+
173196
<!-- tabs:end -->
174197

175198
<!-- solution:end -->

‎solution/2300-2399/2300.Successful Pairs of Spells and Potions/README_EN.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ function successfulPairs(spells: number[], potions: number[], success: number):
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn successful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
178+
potions.sort();
179+
let m = potions.len();
180+
181+
spells.into_iter().map(|v| {
182+
let i = potions.binary_search_by(|&p| {
183+
let prod = (p as i64) * (v as i64);
184+
if prod >= success {
185+
std::cmp::Ordering::Greater
186+
} else {
187+
std::cmp::Ordering::Less
188+
}
189+
}).unwrap_or_else(|x| x);
190+
(m - i) as i32
191+
}).collect()
192+
}
193+
}
194+
```
195+
173196
<!-- tabs:end -->
174197

175198
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn successful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
3+
potions.sort();
4+
let m = potions.len();
5+
6+
spells
7+
.into_iter()
8+
.map(|v| {
9+
let i = potions
10+
.binary_search_by(|&p| {
11+
let prod = (p as i64) * (v as i64);
12+
if prod >= success {
13+
std::cmp::Ordering::Greater
14+
} else {
15+
std::cmp::Ordering::Less
16+
}
17+
})
18+
.unwrap_or_else(|x| x);
19+
(m - i) as i32
20+
})
21+
.collect()
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /