|
| 1 | +//time-complexity : O(n) |
| 2 | +//space-complexity : O(n) |
| 3 | + |
| 4 | +const maxProduct = function (nums) { |
| 5 | + let subarrays = []; |
| 6 | + let subarray = []; |
| 7 | + let zeroCount = 0; |
| 8 | + |
| 9 | + /* -------------------------------------------------------------------------- */ |
| 10 | + /* nonzero subarray 잘라서 subarrays에 넣기 */ |
| 11 | + /* -------------------------------------------------------------------------- */ |
| 12 | + for (let i = 0; i < nums.length; i++) { |
| 13 | + if (nums[i] === 0) { |
| 14 | + if (subarray.length > 0) { |
| 15 | + subarrays.push([...subarray]); |
| 16 | + subarray.length = 0; //각 element의 metadata를 garbage collection에 들어가야 하는 것으로 marking할 뿐이므로 O(1)! |
| 17 | + } |
| 18 | + zeroCount++; |
| 19 | + continue; |
| 20 | + } |
| 21 | + subarray.push(nums[i]); |
| 22 | + } |
| 23 | + if (subarray.length > 0) { |
| 24 | + subarrays.push([...subarray]); |
| 25 | + } |
| 26 | + |
| 27 | + /* -------------------------------------------------------------------------- */ |
| 28 | + /* 각 subarray의 maxProduct들 중 최대값 리턴하기 */ |
| 29 | + /* -------------------------------------------------------------------------- */ |
| 30 | + if (zeroCount === 0) { |
| 31 | + return maxProductNonzero(nums); |
| 32 | + } else { |
| 33 | + let max = 0; |
| 34 | + for (let i = 0; i < subarrays.length; i++) { |
| 35 | + max = Math.max(maxProductNonzero(subarrays[i]), max); |
| 36 | + } |
| 37 | + return max; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +const maxProductNonzero = function (nonZeroNums) { |
| 42 | + if (nonZeroNums.length === 1) return nonZeroNums[0]; //firstNegativeIndex = lastNegativeIndex = 0이 되어 버려서 frontProduct와 backProduct 중 어느 것도 계산이 안 돼버리므로 early return으로 처리. |
| 43 | + |
| 44 | + let firstNegativeIndex = -1; |
| 45 | + let lastNegativeIndex = -1; |
| 46 | + let negativeCount = 0; |
| 47 | + for (let i = 0; i < nonZeroNums.length; i++) { |
| 48 | + if (nonZeroNums[i] < 0 && firstNegativeIndex !== lastNegativeIndex) { |
| 49 | + lastNegativeIndex = i; |
| 50 | + negativeCount++; |
| 51 | + } |
| 52 | + if (nonZeroNums[i] < 0 && firstNegativeIndex === lastNegativeIndex) { |
| 53 | + firstNegativeIndex = i; |
| 54 | + negativeCount++; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (negativeCount === 1) { |
| 59 | + lastNegativeIndex = firstNegativeIndex; |
| 60 | + } |
| 61 | + |
| 62 | + if (negativeCount % 2 === 0) { |
| 63 | + /* -------------------------------------------------------------------------- */ |
| 64 | + /* 음수 개수가 짝수 개면 그냥 전체 곱한 게 최대값임 */ |
| 65 | + /* -------------------------------------------------------------------------- */ |
| 66 | + let product = 1; |
| 67 | + for (let i = 0; i < nonZeroNums.length; i++) { |
| 68 | + product *= nonZeroNums[i]; |
| 69 | + } |
| 70 | + return product; |
| 71 | + } else { |
| 72 | + /* -------------------------------------------------------------------------- */ |
| 73 | + /* 음수 개수가 홀수 개면 처음부터 lastNegativeIndex 직전까지 곱한 수 혹은 */ |
| 74 | + /* firstNegativeIndex부터 끝까지 곱한 수 중 하나가 최대값임 */ |
| 75 | + /* -------------------------------------------------------------------------- */ |
| 76 | + let frontProduct = 1; |
| 77 | + let backProduct = 1; |
| 78 | + for (let i = 0; i < nonZeroNums.length; i++) { |
| 79 | + if (i < lastNegativeIndex) frontProduct *= nonZeroNums[i]; |
| 80 | + if (i > firstNegativeIndex) backProduct *= nonZeroNums[i]; |
| 81 | + } |
| 82 | + return Math.max(frontProduct, backProduct); |
| 83 | + } |
| 84 | +}; |
0 commit comments