|
| 1 | +/** |
| 2 | + * 2117. Abbreviating the Product of a Range |
| 3 | + * https://leetcode.com/problems/abbreviating-the-product-of-a-range/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given two positive integers left and right with left <= right. Calculate the product of |
| 7 | + * all integers in the inclusive range [left, right]. |
| 8 | + * |
| 9 | + * Since the product may be very large, you will abbreviate it following these steps: |
| 10 | + * 1. Count all trailing zeros in the product and remove them. Let us denote this count as C. |
| 11 | + * - For example, there are 3 trailing zeros in 1000, and there are 0 trailing zeros in 546. |
| 12 | + * 2. Denote the remaining number of digits in the product as d. If d > 10, then express the product |
| 13 | + * as <pre>...<suf> where <pre> denotes the first 5 digits of the product, and <suf> denotes the |
| 14 | + * last 5 digits of the product after removing all trailing zeros. If d <= 10, we keep it |
| 15 | + * unchanged. |
| 16 | + * - For example, we express 1234567654321 as 12345...54321, but 1234567 is represented as |
| 17 | + * 1234567. |
| 18 | + * 3. Finally, represent the product as a string "<pre>...<suf>eC". |
| 19 | + * - For example, 12345678987600000 will be represented as "12345...89876e5". |
| 20 | + * |
| 21 | + * Return a string denoting the abbreviated product of all integers in the inclusive range [left, |
| 22 | + * right]. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number} left |
| 27 | + * @param {number} right |
| 28 | + * @return {string} |
| 29 | + */ |
| 30 | +var abbreviateProduct = function(left, right) { |
| 31 | + let zeros = 0; |
| 32 | + let count2 = 0; |
| 33 | + let count5 = 0; |
| 34 | + |
| 35 | + for (let i = left; i <= right; i++) { |
| 36 | + let n = i; |
| 37 | + while (n % 2 === 0) { |
| 38 | + count2++; |
| 39 | + n = Math.floor(n / 2); |
| 40 | + } |
| 41 | + n = i; |
| 42 | + while (n % 5 === 0) { |
| 43 | + count5++; |
| 44 | + n = Math.floor(n / 5); |
| 45 | + } |
| 46 | + } |
| 47 | + zeros = Math.min(count2, count5); |
| 48 | + |
| 49 | + let digits = 0; |
| 50 | + for (let i = left; i <= right; i++) { |
| 51 | + digits += Math.log10(i); |
| 52 | + } |
| 53 | + digits = Math.floor(digits) + 1; |
| 54 | + |
| 55 | + if (digits - zeros <= 10) { |
| 56 | + let product = 1n; |
| 57 | + for (let i = left; i <= right; i++) { |
| 58 | + product *= BigInt(i); |
| 59 | + } |
| 60 | + for (let i = 0; i < zeros; i++) { |
| 61 | + product /= 10n; |
| 62 | + } |
| 63 | + return product.toString() + 'e' + zeros; |
| 64 | + } |
| 65 | + |
| 66 | + let prefix = 1; |
| 67 | + for (let i = left; i <= right; i++) { |
| 68 | + prefix *= i; |
| 69 | + while (prefix >= 1e10) { |
| 70 | + prefix /= 10; |
| 71 | + } |
| 72 | + } |
| 73 | + prefix = prefix.toString().slice(0, 5); |
| 74 | + |
| 75 | + let suffix = 1n; |
| 76 | + for (let i = right; i >= left; i--) { |
| 77 | + suffix = (suffix * BigInt(i)); |
| 78 | + while (suffix % 10n === 0n) { |
| 79 | + suffix /= 10n; |
| 80 | + } |
| 81 | + suffix = suffix % (10n ** 15n); |
| 82 | + } |
| 83 | + |
| 84 | + suffix = suffix.toString(); |
| 85 | + while (suffix.length < 5) { |
| 86 | + suffix = '0' + suffix; |
| 87 | + } |
| 88 | + suffix = suffix.slice(-5); |
| 89 | + |
| 90 | + return prefix + '...' + suffix + 'e' + zeros; |
| 91 | +}; |
0 commit comments