|
| 1 | +// https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/ |
| 2 | + |
| 3 | +// p: array of ints |
| 4 | +// r: int |
| 5 | +// f(n) max length with sub arr with +int |
| 6 | + |
| 7 | +// e: |
| 8 | +// [1,-3,5,7,-5,2] => |
| 9 | +// Input: nums = [-1,-2,-3,0,1] |
| 10 | +// 2 |
| 11 | +// Example 1: |
| 12 | +// Input: nums = [1,-2,-3,4] => |
| 13 | +// base cases |
| 14 | +// f(1): 1, 4 |
| 15 | +// f(2): -2*-3, |
| 16 | +// f(3): 1*-2*-3, -2*-3*4 |
| 17 | +// f(4): all |
| 18 | +// Output: 4 |
| 19 | +// Explanation: The array nums already has a positive product of 24. |
| 20 | + |
| 21 | +// Example 2: |
| 22 | +// Input: nums = [0,1,-2,-3,-4] |
| 23 | +// Output: 3 |
| 24 | + |
| 25 | +// Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. |
| 26 | +// Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. |
| 27 | +// recurrent func: max = max(max, indexOf(j) - indexOf(j-i) +1)) |
| 28 | +// bottom up |
| 29 | +// answer: max |
| 30 | +// base cases: |
| 31 | +// [0] |
| 32 | +// [-1] |
| 33 | +// [1] |
| 34 | + |
| 35 | +// 0 1 -2 -3 0 -4 |
| 36 | + |
| 37 | +// 0 1 -2 -3 -4 |
| 38 | +// p 0 1 1 |
| 39 | +// n -1 0 1 |
| 40 | +// p |
| 41 | + |
| 42 | +// 1 -2 2 0 5 1 -2 0 2 1 6 98 4 8 5 0 1 5 5 5 5 5 5 -3 5 5 5 |
| 43 | +// // |
| 44 | + |
| 45 | +// function getMaxLen(nums) { |
| 46 | +// let max = 0, |
| 47 | +// pos = 0, |
| 48 | +// neg = 0; |
| 49 | +// for (let i = 0; i < nums.length; i++) { |
| 50 | +// if (nums[i] == 0) { |
| 51 | +// [pos, neg] = [0, 0]; |
| 52 | +// } else if (nums[i] < 0) { |
| 53 | +// [pos, neg] = [neg > 0 ? neg + 1 : 0, pos + 1]; |
| 54 | +// } else { |
| 55 | +// [pos, neg] = [pos + 1, neg > 0 ? neg + 1 : 0]; |
| 56 | +// } |
| 57 | +// max = Math.max(max, pos); |
| 58 | +// } |
| 59 | +// console.log(max); |
| 60 | + |
| 61 | +// return max; |
| 62 | +// } |
| 63 | + |
| 64 | +function getMaxLen(nums) { |
| 65 | + let max = 0, |
| 66 | + pos = 0, |
| 67 | + neg = 0; |
| 68 | + for (let num of nums) { |
| 69 | + if (num === 0) { |
| 70 | + pos = 0; |
| 71 | + neg = 0; |
| 72 | + } else if (num > 0) { |
| 73 | + [pos, neg] = [pos + 1, pos === 0 ? 0 : neg + 1]; |
| 74 | + // neg = 1 + neg; |
| 75 | + } else { |
| 76 | + // pos = 1 + neg; |
| 77 | + // neg = 1 + pos; |
| 78 | + [pos, neg] = [neg === 0 ? 0 : 1 + neg, 1 + pos]; |
| 79 | + } |
| 80 | + console.log(pos, neg); |
| 81 | + max = Math.max(max, pos); |
| 82 | + } |
| 83 | + console.log(max); |
| 84 | + return max; |
| 85 | +} |
| 86 | +// getMaxLen([5, 1, -2, -5, -3, 5, 2, 0, -4]); // |
| 87 | + |
| 88 | +getMaxLen([0, 1, -2, -3, -4]); // 3 |
| 89 | +// pos 0 1 1 3 3 |
| 90 | +// neg 0 0 2 2 4 |
| 91 | + |
| 92 | +// pos 0 1 2 3 4 |
| 93 | +// neg 0 1 2 3 4 |
| 94 | + |
| 95 | +// getMaxLen([1, -2, -3, 4]); // 4 |
| 96 | +// pos 0 1 1 3 4 |
| 97 | +// neg 0 0 2 2 2 |
| 98 | + |
| 99 | +// getMaxLen([-1, -2, -3, 0, 1]); // 2 |
| 100 | +// pos 0 1 2 3 |
| 101 | +// neg 0 1 2 3 |
| 102 | +// getMaxLen([-16, 0, -5, 2, 2, -13, 11, 8]); //6 |
| 103 | +// pos 0 0 0 0 1 2 4 5 6 |
| 104 | +// neg 0 1 0 1 2 3 3 4 5 |
| 105 | +// [1,2,3,5,-6,4,0,10] |
| 106 | + |
| 107 | +// getMaxLen([-1]); |
| 108 | +// getMaxLen([0, 0, 0, 0, 0]); |
| 109 | + |
| 110 | +// // Time limit exceeded: DP |
| 111 | +// var getMaxLen = function (nums) { |
| 112 | +// let max = -Infinity; |
| 113 | + |
| 114 | +// let max, |
| 115 | +// pos, |
| 116 | +// neg = 0; |
| 117 | + |
| 118 | +// for (let i = 0; i < nums.length; i++) { |
| 119 | +// for (let j = i; j < nums.length; j++) { |
| 120 | +// let product = nums.slice(i, j + 1).reduce((p, c) => p * c); |
| 121 | +// product > 0 && (max = Math.max(max, j - i + 1)); |
| 122 | +// // console.log(i, "xxx", j, "=", product, j - i + 1); |
| 123 | +// } |
| 124 | +// } |
| 125 | + |
| 126 | +// console.log(max === -Infinity ? 0 : max); |
| 127 | +// return max === -Infinity ? 0 : max; |
| 128 | +// }; |
| 129 | + |
| 130 | +// getMaxLen([0, 1, -2, -3, -4]); // 3 |
| 131 | +// getMaxLen([1, -2, -3, 4]); // 4 |
| 132 | +// // getMaxLen([-1, -2, -3, 0, 1]); // 2 |
| 133 | +// getMaxLen([-1]); |
| 134 | +// getMaxLen([0, 0, 0, 0, 0]); |
| 135 | + |
| 136 | +// sol 2: divide |
| 137 | +// var getMaxLen = function (nums) { |
| 138 | +// let max = -Infinity; |
| 139 | +// let indexAfter0 = 0; |
| 140 | + |
| 141 | +// // loop thru nums |
| 142 | +// // if num = 0 => split arr to 2 parts: before & after 0 |
| 143 | +// //// reduce => check each part if + or - |
| 144 | +// ////// => if + => compare to max |
| 145 | +// ////// => if - => split that part into 2 smaller parts: from begin to - (inclusive) & from - (inclusive) to end |
| 146 | +// //////// => compare 2 smaller parts to max |
| 147 | +// // return max |
| 148 | + |
| 149 | +// for (let i = 0; i < nums.length; i++) { |
| 150 | +// if (nums[i] === 0) { |
| 151 | +// let arrBetween0 = nums.slice(indexAfter0, i); |
| 152 | +// let isNeg = arrBetween0.reduce((p, c) => p * c); |
| 153 | +// // console.log(arrBetween0); |
| 154 | + |
| 155 | +// if (isNeg > 0) { |
| 156 | +// max = Math.max(max, i - indexAfter0); |
| 157 | +// } else { |
| 158 | +// let allNegs = arrBetween0.filter((n) => n < 0); |
| 159 | +// let firstNegIndex = arrBetween0.indexOf(allNegs[0]); |
| 160 | +// let lastNegIndex = arrBetween0.indexOf(allNegs[allNegs.length - 1]); |
| 161 | + |
| 162 | +// const firstNegPart = lastNegIndex - firstNegIndex; |
| 163 | +// const lastNegPart = arrBetween0.length - 1 - firstNegIndex; |
| 164 | +// max = Math.max(max, firstNegPart, lastNegPart); |
| 165 | +// // console.log(firstNegPart, lastNegPart, max); |
| 166 | +// } |
| 167 | +// indexAfter0 = i + 1; |
| 168 | +// } |
| 169 | +// } |
| 170 | + |
| 171 | +// if (!nums.includes(0)) { |
| 172 | +// let isNeg = nums.reduce((p, c) => p * c); |
| 173 | + |
| 174 | +// if (isNeg > 0) { |
| 175 | +// max = nums.length; |
| 176 | +// } else { |
| 177 | +// let allNegs = nums.filter((n) => n < 0); |
| 178 | +// let firstNegIndex = nums.indexOf(allNegs[0]); |
| 179 | +// let lastNegIndex = nums.indexOf(allNegs[allNegs.length - 1]); |
| 180 | + |
| 181 | +// max = Math.max(max, firstNegIndex + 1, nums.length - lastNegIndex); |
| 182 | +// // console.log(firstNegIndex, lastNegIndex); |
| 183 | +// } |
| 184 | +// } |
| 185 | + |
| 186 | +// console.log(max); |
| 187 | +// return max; |
| 188 | +// }; |
0 commit comments