3 Incheckningar 286c947da4 ... c2b70f1840

Upphovsman SHA1 Meddelande Datum
  Zhang Li c2b70f1840 feat: compare_version_number 11 månader sedan
  Zhang Li 8ad662dabe feat: maximum_gap 11 månader sedan
  Zhang Li 4b900ce769 feat: find_peak_element 11 månader sedan

+ 55 - 0
src/solutions/array/maximum_gap.rs

@@ -0,0 +1,55 @@
+struct Solution;
+impl Solution {
+    pub fn maximum_gap(nums: Vec<i32>) -> i32 {
+        let max = nums.iter().max().unwrap();
+        let min = nums.iter().min().unwrap();
+        if max == min { return 0; }
+        let n = nums.len();
+
+        let mut gap = (max - min) / (n as i32 -1);
+        if (max - min) % (n as i32 -1) != 0 {
+            gap += 1;
+        }
+        let mut min_bucket = vec![i32::MAX; n];
+        let mut max_bucket = vec![i32::MIN; n];
+        for i in 0..n {
+            let idx = ((nums[i] - min) / gap) as usize;
+            min_bucket[idx] = min_bucket[idx].min(nums[i]);
+            max_bucket[idx] = max_bucket[idx].max(nums[i]);
+        }
+
+        // println!("max_bucket: {:?}", max_bucket);
+        // println!("min_bucket: {:?}", min_bucket);
+
+        let mut res = i32::MIN;
+        let mut prev_max = i32::MIN;
+        for i in 0..n {
+            if min_bucket[i] == i32::MAX {
+                continue;
+            }
+
+            if prev_max == i32::MIN {
+                prev_max = max_bucket[i];
+                continue;
+            }
+
+            res = res.max(min_bucket[i] - prev_max);
+            prev_max = max_bucket[i];
+        }
+        res
+    }
+}
+
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_maximum_gap() {
+        let nums = vec![3,6,9,1,10];
+        let res = Solution::maximum_gap(nums);
+        assert_eq!(res, 3);
+    }
+}

+ 1 - 0
src/solutions/array/mod.rs

@@ -15,3 +15,4 @@ mod longest_consecutive_sequence;
 mod longest_palindromic_substring;
 mod single_number;
 mod single_number2;
+mod maximum_gap;

+ 53 - 0
src/solutions/binary_search/find_peak_element.rs

@@ -0,0 +1,53 @@
+struct Solution;
+
+impl Solution {
+    pub fn find_peak_element(nums: Vec<i32>) -> i32 {
+        let n = nums.len();
+        if n == 1 { return 0; }
+        Self::bs(&nums, n, 0, n-1) as i32
+    }
+
+    fn bs(nums: &Vec<i32>, n: usize, left: usize, right: usize) -> usize {
+        if left == right { return  left; }
+        if left + 1 == right {
+            if nums[left] < nums[right] { return right}
+            else { return left; }
+        }
+        let mid = (left + right) / 2;
+        if Self::is_peak(nums, mid, n) { return mid; }
+        if nums[mid] < nums[mid+1] {
+            Self::bs(nums, n,mid+1, right)
+        } else {
+            Self::bs(nums, n,left, mid-1)
+        }
+
+    }
+
+    fn is_peak(nums: &Vec<i32>, idx: usize, n: usize) -> bool {
+        match idx {
+            0 if nums[0] < nums[1] => true,
+            _ if idx + 1 == n && nums[n-1] > nums[n-2] => true,
+            _ if nums[idx-1] < nums[idx] && nums[idx] > nums[idx+1] => true,
+            _ => false
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_find_peak_element() {
+        let nums = vec![1,2,1,3,5,6,4];
+        let res = Solution::find_peak_element(nums);
+        assert_eq!(res, 5);
+    }
+
+    #[test]
+    fn test_find_peak_element2() {
+        let nums = vec![1,2,3,1];
+        let res = Solution::find_peak_element(nums);
+        assert_eq!(res, 2);
+    }
+}

+ 3 - 1
src/solutions/binary_search/mod.rs

@@ -8,4 +8,6 @@ mod median_of_two_sorted_arrays;
 mod binary_tree_preorder_traversal;
 mod binary_tree_postorder_traversal;
 mod find_minimum_in_rotated_array;
-mod find_minimum_in_rotated_array2;
+mod find_minimum_in_rotated_array2;
+mod find_peak_element;
+mod two_sum;

+ 68 - 0
src/solutions/binary_search/two_sum.rs

@@ -0,0 +1,68 @@
+struct Solution;
+impl Solution {
+    pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
+        let n = numbers.len();
+        for i in 0..n-1 {
+            let elem = target - numbers[i];
+
+            if let Some(e) = Self::binary_search(&numbers, elem, i+1, n-1) {
+                return vec![i as i32+1, e];
+            }
+        }
+
+        vec![]
+
+    }
+
+    fn binary_search(numbers: &Vec<i32>, elem: i32, left: usize, right: usize) -> Option<i32> {
+        if numbers[left] > elem {
+            return None;
+        }
+        let mut left = left;
+        let mut right = right;
+        while left <= right {
+            let mid = (left + right) / 2;
+            if numbers[mid] == elem {
+                return Some(1 + mid as i32);
+            }
+
+            if numbers[mid] > elem {
+                if mid == 0 {
+                    return None;
+                }
+                right = mid - 1;
+                continue;
+            }
+            if numbers[mid] < elem {
+                left = mid + 1;
+                continue;
+            }
+
+        }
+
+        None
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_two_sum() {
+        let numbers = vec![2,7,11,15];
+        let target = 9;
+        let res = Solution::two_sum(numbers, target);
+        assert_eq!(res, vec![1,2]);
+    }
+
+    #[test]
+    fn test_two_sum2() {
+        let numbers = vec![5,25,75];
+        let target = 100;
+        let res = Solution::two_sum(numbers, target);
+        assert_eq!(res, vec![2,3]);
+    }
+
+}

+ 31 - 0
src/solutions/str/compare_version_numbers.rs

@@ -0,0 +1,31 @@
+struct Solution;
+
+impl Solution {
+    pub fn compare_version(version1: String, version2: String) -> i32 {
+        let mut version1 = version1.split(".").into_iter().map(|x| x.parse().unwrap());
+        let mut version2= version2.split(".").into_iter().map(|x| x.parse().unwrap());
+        loop {
+            match (version1.next(), version2.next()) {
+                (Some(version1), version2) if version1 > version2.unwrap_or(0) => return 1,
+                (version1, Some(version2)) if version1.unwrap_or(0) < version2 => return -1,
+                (None, None) => return 0,
+                (_, _) => continue,
+            }
+        }
+
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_compare_version() {
+        let version1 = String::from("1.01");
+        let version2 = String::from("1.001");
+        let res = Solution::compare_version(version1, version2);
+        assert_eq!(res, 0);
+    }
+}

+ 2 - 1
src/solutions/str/mod.rs

@@ -3,4 +3,5 @@ mod strstr;
 mod minimum_window_substring;
 mod string_to_integer;
 mod valid_palindrome;
-mod reverse_words_in_a_string;
+mod reverse_words_in_a_string;
+mod compare_version_numbers;