Solve two-sum programming assignment fast
parent
8bbb196819
commit
bc5e250fd8
|
@ -97,6 +97,6 @@ fn main() {
|
|||
// c2a1();
|
||||
// c2a2();
|
||||
// c2a3();
|
||||
c2a4();
|
||||
// c3a1();
|
||||
// c2a4();
|
||||
c3a1();
|
||||
}
|
||||
|
|
100
src/two_sum.rs
100
src/two_sum.rs
|
@ -1,5 +1,45 @@
|
|||
use std::collections::HashSet;
|
||||
// use std::iter::FromIterator;
|
||||
|
||||
fn binary_search<T: std::cmp::PartialOrd>(min_i: usize, max_i: usize, xs: &Vec<T>, needle: T) -> usize {
|
||||
if min_i >= max_i {
|
||||
return max_i;
|
||||
}
|
||||
let half_i = min_i + (max_i - min_i) / 2;
|
||||
|
||||
if xs[half_i] == needle {
|
||||
return half_i;
|
||||
}
|
||||
|
||||
if needle < xs[half_i] {
|
||||
binary_search(min_i, half_i - 1, &xs, needle)
|
||||
} else {
|
||||
binary_search(half_i + 1, max_i, xs, needle)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_two_sums(xs: &mut Vec<i64>) -> usize {
|
||||
let mut found: HashSet<i64> = HashSet::new();
|
||||
const T_MIN: i64 = -10000;
|
||||
const T_MAX: i64 = 10000;
|
||||
xs.sort();
|
||||
|
||||
for i in 0..xs.len() {
|
||||
let a: i64 = xs[i];
|
||||
let b_min = T_MIN - a;
|
||||
let b_max = T_MAX - a;
|
||||
let i_min = binary_search(i + 1, xs.len() - 1, &xs, b_min);
|
||||
let i_max = binary_search(i + 1, xs.len() - 1, &xs, b_max);
|
||||
|
||||
for i in i_min..(i_max + 1) {
|
||||
let b = xs[i];
|
||||
let t = a + b;
|
||||
if T_MIN <= t && t <= T_MAX {
|
||||
found.insert(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
found.len()
|
||||
}
|
||||
|
||||
// fn two_sum(t: i64, xs: &Vec<i64>, ys: &HashSet<i64>) -> bool {
|
||||
// for &x in xs {
|
||||
|
@ -11,61 +51,7 @@ use std::collections::HashSet;
|
|||
// false
|
||||
// }
|
||||
|
||||
fn binary_search(min_i: usize, xs: &Vec<i64>, needle: i64) -> usize {
|
||||
let mut width = xs.len() - min_i;
|
||||
let mut i = min_i + width / 2;
|
||||
|
||||
if xs[i] == needle {
|
||||
return i;
|
||||
}
|
||||
|
||||
while width > 1 {
|
||||
|
||||
width = width / 2;
|
||||
|
||||
if xs[i] < needle {
|
||||
i = i + width;
|
||||
} else if xs[i] > needle {
|
||||
i = i - width;
|
||||
|
||||
} else {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
// fn add_two_sums_for_limits(i: usize, xs: &Vec<i64>) -> () {
|
||||
// let t_min = -10000;
|
||||
// let t_max = 10000;
|
||||
// let a: i64 = xs[i];
|
||||
// let b_min = t_min - a;
|
||||
// let b_max = t_max - a;
|
||||
// println!("{:?} {:?}", b_min, b_max);
|
||||
|
||||
// }
|
||||
|
||||
pub fn find_two_sums(xs: &mut Vec<i64>) -> usize {
|
||||
let found: HashSet<i64> = HashSet::new();
|
||||
const T_MIN: i64 = -3;
|
||||
const T_MAX: i64 = 3;
|
||||
xs.sort();
|
||||
for i in 0..1 {
|
||||
// add_two_sums_for_limits(i, &xs);
|
||||
let a: i64 = xs[i];
|
||||
let b_min = T_MIN - a;
|
||||
let b_max = T_MAX - a;
|
||||
println!("a={} b_min={} b_max={}", a, b_min, b_max);
|
||||
let i_min = binary_search(i, &xs, b_min);
|
||||
let i_max = binary_search(i, &xs, b_max);
|
||||
println!("{:?} {:?}", i_min, i_max);
|
||||
}
|
||||
found.len()
|
||||
}
|
||||
|
||||
// pub fn find_two_sums(xs: Vec<i64>) -> usize {
|
||||
|
||||
// pub fn find_two_sums_naiv(xs: Vec<i64>) -> usize {
|
||||
// let mut xs_min: Vec<i64> = xs.to_vec();
|
||||
// xs_min.retain(|&i| i < 0);
|
||||
|
||||
|
@ -84,5 +70,3 @@ pub fn find_two_sums(xs: &mut Vec<i64>) -> usize {
|
|||
// }
|
||||
// r
|
||||
// }
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue