-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsuccessful-pairs-of-spells-and-potions.rs
62 lines (60 loc) · 2.29 KB
/
successful-pairs-of-spells-and-potions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 2300. Successful Pairs of Spells and Potions
// 🟠 Medium
//
// https://leetcode.com/problems/successful-pairs-of-spells-and-potions/
//
// Tags: Array - Two Pointers - Binary Search - Sorting
struct Solution;
impl Solution {
/// Sort the potions array, then iterate over the spells, for each spell,
/// find the index of the first potion that we can combine with it to
/// satisfy the success value.
///
/// Time complexity: O(n*log(n)+m*log(n)) - Where m is the number of spells
/// and n is the number of potions. We sort the potions array at nlog(n)
/// cost, then iterate over the spells, for each spell, we binary search
/// the number of potions that can be combined with the spell to result in
/// a successful combination.
/// Space complexity: O(m+n) - We make a copy of the potions array n and
/// sort it, the result array has size m.
///
/// Runtime 55 ms Beats 88.89%
/// Memory 4 MB Beats 55.56%
pub fn successful_pairs(spells: Vec<i32>, potions: Vec<i32>, success: i64) -> Vec<i32> {
// Sort the potions to be able to binary search the boundary.
let mut potions = potions;
potions.sort_unstable();
let n = potions.len();
let mut res = Vec::with_capacity(spells.len());
for spell_force in spells {
// Need to cast both before multiplying to avoid overflow.
let spell_force = spell_force as i64;
// Binary search the boundary.
let (mut l, mut r) = (0, n);
while l < r {
let mid = (l + r) / 2;
let combined_force = spell_force * potions[mid] as i64;
if combined_force < success {
l = mid + 1;
} else {
r = mid;
}
}
// The number of successful combinations for this spell.
res.push((n - l) as i32);
}
res
}
}
// Tests.
fn main() {
let tests = [
(vec![3, 1, 2], vec![8], 16, vec![1, 0, 1]),
(vec![3, 1, 2], vec![8, 5, 8], 16, vec![2, 0, 2]),
(vec![5, 1, 3], vec![1, 2, 3, 4, 5], 7, vec![4, 0, 3]),
];
for t in tests {
assert_eq!(Solution::successful_pairs(t.0, t.1, t.2), t.3);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}