Skip to content

Commit c7b3571

Browse files
committed
feat: add solutions to lc problem: No.2300
1 parent a1ad054 commit c7b3571

File tree

5 files changed

+63
-89
lines changed

5 files changed

+63
-89
lines changed

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,14 @@ class Solution {
120120
class Solution {
121121
public:
122122
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
123-
sort(potions.begin(), potions.end());
123+
ranges::sort(potions);
124+
const int m = potions.size();
124125
vector<int> ans;
125-
int m = potions.size();
126-
for (int& v : spells) {
127-
int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin();
128-
ans.push_back(m - i);
126+
ans.reserve(spells.size());
127+
128+
for (int v : spells) {
129+
auto it = ranges::lower_bound(potions, static_cast<double>(success) / v);
130+
ans.push_back(m - static_cast<int>(it - potions.begin()));
129131
}
130132
return ans;
131133
}
@@ -153,19 +155,13 @@ function successfulPairs(spells: number[], potions: number[], success: number):
153155
potions.sort((a, b) => a - b);
154156
const m = potions.length;
155157
const ans: number[] = [];
158+
156159
for (const v of spells) {
157-
let left = 0;
158-
let right = m;
159-
while (left < right) {
160-
const mid = (left + right) >> 1;
161-
if (v * potions[mid] >= success) {
162-
right = mid;
163-
} else {
164-
left = mid + 1;
165-
}
166-
}
167-
ans.push(m - left);
160+
const targetPotion = success / v;
161+
const idx = _.sortedIndexBy(potions, targetPotion, p => p);
162+
ans.push(m - idx);
168163
}
164+
169165
return ans;
170166
}
171167
```
@@ -177,18 +173,15 @@ impl Solution {
177173
pub fn successful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
178174
potions.sort();
179175
let m = potions.len();
176+
let mut ans = Vec::with_capacity(spells.len());
180177

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()
178+
for &v in &spells {
179+
let target = (success + v as i64 - 1) / v as i64;
180+
let idx = potions.partition_point(|&p| (p as i64) < target);
181+
ans.push((m - idx) as i32);
182+
}
183+
184+
ans
192185
}
193186
}
194187
```

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Thus, [4,0,3] is returned.
4747
<strong>Output:</strong> [2,0,2]
4848
<strong>Explanation:</strong>
4949
- 0<sup>th</sup> spell: 3 * [8,5,8] = [<u><strong>24</strong></u>,15,<u><strong>24</strong></u>]. 2 pairs are successful.
50-
- 1<sup>st</sup> spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
51-
- 2<sup>nd</sup> spell: 2 * [8,5,8] = [<strong><u>16</u></strong>,10,<u><strong>16</strong></u>]. 2 pairs are successful.
50+
- 1<sup>st</sup> spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
51+
- 2<sup>nd</sup> spell: 2 * [8,5,8] = [<strong><u>16</u></strong>,10,<u><strong>16</strong></u>]. 2 pairs are successful.
5252
Thus, [2,0,2] is returned.
5353
</pre>
5454

@@ -120,12 +120,14 @@ class Solution {
120120
class Solution {
121121
public:
122122
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
123-
sort(potions.begin(), potions.end());
123+
ranges::sort(potions);
124+
const int m = potions.size();
124125
vector<int> ans;
125-
int m = potions.size();
126-
for (int& v : spells) {
127-
int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin();
128-
ans.push_back(m - i);
126+
ans.reserve(spells.size());
127+
128+
for (int v : spells) {
129+
auto it = ranges::lower_bound(potions, static_cast<double>(success) / v);
130+
ans.push_back(m - static_cast<int>(it - potions.begin()));
129131
}
130132
return ans;
131133
}
@@ -153,19 +155,13 @@ function successfulPairs(spells: number[], potions: number[], success: number):
153155
potions.sort((a, b) => a - b);
154156
const m = potions.length;
155157
const ans: number[] = [];
158+
156159
for (const v of spells) {
157-
let left = 0;
158-
let right = m;
159-
while (left < right) {
160-
const mid = (left + right) >> 1;
161-
if (v * potions[mid] >= success) {
162-
right = mid;
163-
} else {
164-
left = mid + 1;
165-
}
166-
}
167-
ans.push(m - left);
160+
const targetPotion = success / v;
161+
const idx = _.sortedIndexBy(potions, targetPotion, p => p);
162+
ans.push(m - idx);
168163
}
164+
169165
return ans;
170166
}
171167
```
@@ -177,18 +173,15 @@ impl Solution {
177173
pub fn successful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
178174
potions.sort();
179175
let m = potions.len();
176+
let mut ans = Vec::with_capacity(spells.len());
180177

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()
178+
for &v in &spells {
179+
let target = (success + v as i64 - 1) / v as i64;
180+
let idx = potions.partition_point(|&p| (p as i64) < target);
181+
ans.push((m - idx) as i32);
182+
}
183+
184+
ans
192185
}
193186
}
194187
```
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
class Solution {
22
public:
33
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
4-
sort(potions.begin(), potions.end());
4+
ranges::sort(potions);
5+
const int m = potions.size();
56
vector<int> ans;
6-
int m = potions.size();
7-
for (int& v : spells) {
8-
int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin();
9-
ans.push_back(m - i);
7+
ans.reserve(spells.size());
8+
9+
for (int v : spells) {
10+
auto it = ranges::lower_bound(potions, static_cast<double>(success) / v);
11+
ans.push_back(m - static_cast<int>(it - potions.begin()));
1012
}
1113
return ans;
1214
}
13-
};
15+
};

solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@ impl Solution {
22
pub fn successful_pairs(spells: Vec<i32>, mut potions: Vec<i32>, success: i64) -> Vec<i32> {
33
potions.sort();
44
let m = potions.len();
5+
let mut ans = Vec::with_capacity(spells.len());
56

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()
7+
for &v in &spells {
8+
let target = (success + v as i64 - 1) / v as i64;
9+
let idx = potions.partition_point(|&p| (p as i64) < target);
10+
ans.push((m - idx) as i32);
11+
}
12+
13+
ans
2214
}
2315
}

solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ function successfulPairs(spells: number[], potions: number[], success: number):
22
potions.sort((a, b) => a - b);
33
const m = potions.length;
44
const ans: number[] = [];
5+
56
for (const v of spells) {
6-
let left = 0;
7-
let right = m;
8-
while (left < right) {
9-
const mid = (left + right) >> 1;
10-
if (v * potions[mid] >= success) {
11-
right = mid;
12-
} else {
13-
left = mid + 1;
14-
}
15-
}
16-
ans.push(m - left);
7+
const targetPotion = success / v;
8+
const idx = _.sortedIndexBy(potions, targetPotion, p => p);
9+
ans.push(m - idx);
1710
}
11+
1812
return ans;
1913
}

0 commit comments

Comments
 (0)