|
| 1 | +use crate::eval; |
| 2 | + |
| 3 | +#[test] |
| 4 | +pub fn first_missing_positive() { |
| 5 | + eval( |
| 6 | + r#" |
| 7 | +
|
| 8 | + ### |
| 9 | + Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums. |
| 10 | +
|
| 11 | + You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. |
| 12 | +
|
| 13 | + |
| 14 | +
|
| 15 | + Example 1: |
| 16 | +
|
| 17 | + Input: nums = [1,2,0] |
| 18 | + Output: 3 |
| 19 | + Explanation: The numbers in the range [1,2] are all in the array. |
| 20 | + Example 2: |
| 21 | +
|
| 22 | + Input: nums = [3,4,-1,1] |
| 23 | + Output: 2 |
| 24 | + Explanation: 1 is in the array but 2 is missing. |
| 25 | + Example 3: |
| 26 | +
|
| 27 | + Input: nums = [7,8,9,11,12] |
| 28 | + Output: 1 |
| 29 | + Explanation: The smallest positive integer 1 is missing. |
| 30 | + |
| 31 | +
|
| 32 | + Constraints: |
| 33 | +
|
| 34 | + 1 <= nums.length <= 105 |
| 35 | + -231 <= nums[i] <= 231 - 1 |
| 36 | + ### |
| 37 | +
|
| 38 | + first_missing_positive = fn(nums: [int...]) -> int { |
| 39 | + from 0 to nums.len(), i { |
| 40 | + if nums[i] <= 0 { |
| 41 | + nums[i] = nums.len() + 1 |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | + from 0 to nums.len(), i { |
| 46 | + num_abs = (nums[i]).abs() |
| 47 | +
|
| 48 | + if num_abs - 1 < nums.len() && nums[num_abs - 1] > 0 { |
| 49 | + (nums[num_abs - 1]) *= -1 |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + from 0 to nums.len(), i { |
| 54 | + if nums[i] > 0 { |
| 55 | + return i + 1 |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + return nums.len() + 1 |
| 60 | + } |
| 61 | +
|
| 62 | + test_cases = map[[int...], int] { |
| 63 | + [1,2,0]: 3, |
| 64 | + [3,4,-1,1]: 2, |
| 65 | + [7,8,9,11,12]: 1, |
| 66 | + [50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]: 51, |
| 67 | + [1,2,6,3,5,4]: 7, |
| 68 | + }.pairs() |
| 69 | +
|
| 70 | + print "🔵 Starting testing suite" |
| 71 | +
|
| 72 | + from 0 to test_cases.len(), i { |
| 73 | + [input, expected] = test_cases[i] |
| 74 | +
|
| 75 | + output = first_missing_positive(input) |
| 76 | + |
| 77 | + if output != expected { |
| 78 | + print "🛑 Error in test suite: expected " + expected.to_str() + ", found: " + output.to_str() |
| 79 | + assert false |
| 80 | + } |
| 81 | +
|
| 82 | + print " ☐ Passed test #" + i |
| 83 | + } |
| 84 | +
|
| 85 | + print "✅ Passed " + test_cases.len() + " test cases" |
| 86 | + "# |
| 87 | + ) |
| 88 | + .unwrap(); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +pub fn product_of_array_except_self() { |
| 93 | + eval( |
| 94 | + r#" |
| 95 | + ### |
| 96 | + Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. |
| 97 | +
|
| 98 | + The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. |
| 99 | +
|
| 100 | + You must write an algorithm that runs in O(n) time and without using the division operation. |
| 101 | +
|
| 102 | + |
| 103 | +
|
| 104 | + Example 1: |
| 105 | +
|
| 106 | + Input: nums = [1,2,3,4] |
| 107 | + Output: [24,12,8,6] |
| 108 | + Example 2: |
| 109 | +
|
| 110 | + Input: nums = [-1,1,0,-3,3] |
| 111 | + Output: [0,0,9,0,0] |
| 112 | + |
| 113 | +
|
| 114 | + Constraints: |
| 115 | +
|
| 116 | + 2 <= nums.length <= 105 |
| 117 | + -30 <= nums[i] <= 30 |
| 118 | + The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. |
| 119 | + |
| 120 | +
|
| 121 | + Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) |
| 122 | + ### |
| 123 | +
|
| 124 | + product_of_array_except_self = fn(nums: [int...]) -> [int...] { |
| 125 | + result: [int...] = [] |
| 126 | + result.ensure_inner_capacity(nums.len()) |
| 127 | +
|
| 128 | + left_prefixes: [int?...] = [nil, nums[0]] |
| 129 | +
|
| 130 | + # walk from left to right |
| 131 | + from 1 to nums.len() - 1, i { |
| 132 | + left_prefixes.push(left_prefixes[i] * nums[i]) |
| 133 | + } |
| 134 | +
|
| 135 | + right_prefixes: [int?...] = [nil, nums[nums.len() - 1]] |
| 136 | +
|
| 137 | + # walk from right to left |
| 138 | + from 1 to nums.len() - 1, i { |
| 139 | + right_prefixes.push(right_prefixes[i] * nums[nums.len() - i - 1]) |
| 140 | + } |
| 141 | +
|
| 142 | + # aggregate |
| 143 | + from 0 to nums.len(), i { |
| 144 | + left_product = (left_prefixes[i]) or 1 |
| 145 | + right_product = (right_prefixes[nums.len() - i - 1]) or 1 |
| 146 | + result.push(left_product * right_product) |
| 147 | + } |
| 148 | +
|
| 149 | + return result |
| 150 | + } |
| 151 | +
|
| 152 | + test_cases = map[[int...], [int...]] { |
| 153 | + [1,2,3,4]: [24,12,8,6], |
| 154 | + [-1,1,0,-3,3]: [0,0,9,0,0], |
| 155 | + [1, 2, 3, 4]: [24, 12, 8, 6], |
| 156 | + [2, 3, 4, 5]: [60, 40, 30, 24], |
| 157 | + [1, 1, 1, 1]: [1, 1, 1, 1], |
| 158 | + [10, 3, 5, 6, 2]: [180, 600, 360, 300, 900], |
| 159 | + [-1, 1, 0, -3, 3]: [0, 0, 9, 0, 0], |
| 160 | + [2, -2, 3, -3]: [18, -18, 12, -12], |
| 161 | + [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], |
| 162 | + [-5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]: [-1, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5], |
| 163 | + }.pairs() |
| 164 | +
|
| 165 | + print "🔵 Starting testing suite" |
| 166 | +
|
| 167 | + from 0 to test_cases.len(), i { |
| 168 | + [input, expected] = test_cases[i] |
| 169 | +
|
| 170 | + output = product_of_array_except_self(input) |
| 171 | + |
| 172 | + if output != expected { |
| 173 | + print "🛑 Error in test suite: expected " + expected.to_str() + ", found: " + output.to_str() |
| 174 | + assert false |
| 175 | + } |
| 176 | +
|
| 177 | + print " ☐ Passed test #" + i |
| 178 | + } |
| 179 | +
|
| 180 | + print "✅ Passed " + test_cases.len() + " test cases" |
| 181 | + "#, |
| 182 | + ) |
| 183 | + .unwrap() |
| 184 | +} |
0 commit comments