|
| 1 | +use std::cmp::{max, min}; |
| 2 | +use std::convert::TryInto; |
| 3 | + |
| 4 | +/** |
| 5 | + * [167] Two Sum II - Input Array Is Sorted |
| 6 | + * |
| 7 | + * Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 < numbers.length. |
| 8 | + * Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. |
| 9 | + * The tests are generated such that there is exactly one solution. You may not use the same element twice. |
| 10 | + * Your solution must use only constant extra space. |
| 11 | + * |
| 12 | + * <strong class="example">Example 1: |
| 13 | + * |
| 14 | + * Input: numbers = [<u>2</u>,<u>7</u>,11,15], target = 9 |
| 15 | + * Output: [1,2] |
| 16 | + * Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. |
| 17 | + * |
| 18 | + * <strong class="example">Example 2: |
| 19 | + * |
| 20 | + * Input: numbers = [<u>2</u>,3,<u>4</u>], target = 6 |
| 21 | + * Output: [1,3] |
| 22 | + * Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. |
| 23 | + * |
| 24 | + * <strong class="example">Example 3: |
| 25 | + * |
| 26 | + * Input: numbers = [<u>-1</u>,<u>0</u>], target = -1 |
| 27 | + * Output: [1,2] |
| 28 | + * Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. |
| 29 | + * |
| 30 | + * |
| 31 | + * Constraints: |
| 32 | + * |
| 33 | + * 2 <= numbers.length <= 3 * 10^4 |
| 34 | + * -1000 <= numbers[i] <= 1000 |
| 35 | + * numbers is sorted in non-decreasing order. |
| 36 | + * -1000 <= target <= 1000 |
| 37 | + * The tests are generated such that there is exactly one solution. |
| 38 | + * |
| 39 | + */ |
| 40 | +pub struct Solution {} |
| 41 | + |
| 42 | +// problem: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ |
| 43 | +// discuss: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/discuss/?currentPage=1&orderBy=most_votes&query= |
| 44 | + |
| 45 | +// submission codes start here |
| 46 | + |
| 47 | +impl Solution { |
| 48 | + |
| 49 | + fn binary_find_index(numbers: &Vec<i32>, target: i32) -> Option<usize> { |
| 50 | + let mut mindex = 0; |
| 51 | + let mut maxdex = numbers.len() - 1; |
| 52 | + while mindex <= maxdex { |
| 53 | + let potential_index = (maxdex + mindex) / 2; |
| 54 | + if numbers[potential_index] == target { |
| 55 | + return Some(potential_index); |
| 56 | + } else if numbers[potential_index] < target { |
| 57 | + mindex = potential_index + 1; |
| 58 | + } else { |
| 59 | + maxdex = potential_index - 1; |
| 60 | + } |
| 61 | + } |
| 62 | + return None; |
| 63 | + } |
| 64 | + |
| 65 | + pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> { |
| 66 | + for i in 0..numbers.len() { |
| 67 | + let n = numbers[i]; |
| 68 | + let potential_match = Solution::binary_find_index(&numbers, target - n); |
| 69 | + match potential_match { |
| 70 | + Some (i2) if i != i2 => { |
| 71 | + let res1: i32 = i.try_into().unwrap(); |
| 72 | + let res2: i32 = i2.try_into().unwrap(); |
| 73 | + |
| 74 | + return vec![min(res1,res2) + 1, max(res1,res2) + 1]; |
| 75 | + }, |
| 76 | + _ => continue, |
| 77 | + }; |
| 78 | + } |
| 79 | + panic!("Oh no, no solution") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// submission codes end |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_1() { |
| 91 | + let input_list = vec![2,7,11,15]; |
| 92 | + let target = 9; |
| 93 | + |
| 94 | + let res = Solution::two_sum(input_list, target); |
| 95 | + |
| 96 | + assert_eq!(vec![1,2], res); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_2() { |
| 101 | + let input_list = vec![2,3,4]; |
| 102 | + let target = 6; |
| 103 | + |
| 104 | + let res = Solution::two_sum(input_list, target); |
| 105 | + |
| 106 | + assert_eq!(vec![1,3], res); |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_3() { |
| 111 | + let input_list = vec![-1,0]; |
| 112 | + let target = -1; |
| 113 | + |
| 114 | + let res = Solution::two_sum(input_list, target); |
| 115 | + |
| 116 | + assert_eq!(vec![1,2], res); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_4() { |
| 121 | + let input_list = vec![1,2,3,4,4,9,56,90]; |
| 122 | + let target = 8; |
| 123 | + |
| 124 | + let res = Solution::two_sum(input_list, target); |
| 125 | + |
| 126 | + assert_eq!(vec![4,5], res); |
| 127 | + } |
| 128 | +} |
0 commit comments