-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0259_3sum_smaller.rs
More file actions
47 lines (40 loc) · 1.24 KB
/
s0259_3sum_smaller.rs
File metadata and controls
47 lines (40 loc) · 1.24 KB
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
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(n^2) O(1)
pub fn three_sum_smaller(mut nums: Vec<i32>, target: i32) -> i32 {
let mut ans = 0;
if nums.len() < 3 {
return ans;
}
nums.sort();
// [1, 2, 3, 5, 8]
// ↑ ↑
// left right
// How many pairs with one of the index = left that satisfy the condition?
// You can tell by the difference between right and left which is 33,
// namely (1,2), (1,3), and (1,5). Therefore, we move left one step to its right
for i in 0..nums.len() {
let (mut lptr, mut rptr) = (i + 1, nums.len() - 1);
while lptr < rptr {
if nums[i] + nums[lptr] + nums[rptr] < target {
ans += (rptr - lptr) as i32;
lptr += 1;
} else {
rptr -= 1;
}
}
}
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_259() {
assert_eq!(Solution::three_sum_smaller(vec![-2, 0, 1, 3], 2), 2);
assert_eq!(Solution::three_sum_smaller(vec![], 0), 0);
assert_eq!(Solution::three_sum_smaller(vec![0], 0), 0);
}
}